Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | 'use strict'; |
| 5 | |
| 6 | // Description: Scans for localizability violations in the DevTools front-end. |
| 7 | // Audits all Common.UIString(), UI.formatLocalized(), and ls`` calls and |
| 8 | // checks for misuses of concatenation and conditionals. It also looks for |
| 9 | // specific arguments to functions that are expected to be a localized string. |
| 10 | // Since the check scans for common error patterns, it might misidentify something. |
| 11 | // In this case, add it to the excluded errors at the top of the script. |
| 12 | |
| 13 | const path = require('path'); |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 14 | const localizationUtils = require('./localization_utils/localization_utils'); |
| 15 | const esprimaTypes = localizationUtils.esprimaTypes; |
| 16 | const escodegen = localizationUtils.escodegen; |
| 17 | const esprima = localizationUtils.esprima; |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 18 | |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 19 | // Exclude known errors |
| 20 | const excludeErrors = [ |
| 21 | 'Common.UIString(view.title())', 'Common.UIString(setting.title() || \'\')', 'Common.UIString(option.text)', |
| 22 | 'Common.UIString(experiment.title)', 'Common.UIString(phase.message)', |
| 23 | 'Common.UIString(Help.latestReleaseNote().header)', 'Common.UIString(conditions.title)', |
| 24 | 'Common.UIString(extension.title())', 'Common.UIString(this._currentValueLabel, value)' |
| 25 | ]; |
| 26 | |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 27 | const usage = `Usage: node ${path.basename(process.argv[0])} [-a | <.js file path>*] |
| 28 | |
| 29 | -a: If present, check all devtools frontend .js files |
| 30 | <.js file path>*: List of .js files with absolute paths separated by a space |
| 31 | `; |
| 32 | |
| 33 | async function main() { |
| 34 | if (process.argv.length < 3 || process.argv[2] === '--help') { |
| 35 | console.log(usage); |
| 36 | process.exit(0); |
| 37 | } |
| 38 | |
| 39 | const errors = []; |
| 40 | |
| 41 | try { |
| 42 | let filePaths = []; |
| 43 | if (process.argv[2] === '-a') { |
| 44 | const frontendPath = path.resolve(__dirname, '..', 'front_end'); |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 45 | await localizationUtils.getFilesFromDirectory(frontendPath, filePaths, ['.js']); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 46 | } else { |
| 47 | filePaths = process.argv.slice(2); |
| 48 | } |
| 49 | |
| 50 | const promises = []; |
| 51 | for (const filePath of filePaths) |
| 52 | promises.push(auditFileForLocalizability(filePath, errors)); |
| 53 | |
| 54 | await Promise.all(promises); |
| 55 | } catch (err) { |
| 56 | console.log(err); |
| 57 | process.exit(1); |
| 58 | } |
| 59 | |
| 60 | if (errors.length > 0) { |
| 61 | console.log(`DevTools localization checker detected errors!\n${errors.join('\n')}`); |
| 62 | process.exit(1); |
| 63 | } |
| 64 | console.log('DevTools localization checker passed'); |
| 65 | } |
| 66 | |
| 67 | main(); |
| 68 | |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 69 | function includesConditionalExpression(listOfElements) { |
| 70 | return listOfElements.filter(ele => ele !== undefined && ele.type === esprimaTypes.COND_EXPR).length > 0; |
| 71 | } |
| 72 | |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 73 | function addError(error, errors) { |
| 74 | if (!errors.includes(error)) |
| 75 | errors.push(error); |
| 76 | } |
| 77 | |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 78 | function buildConcatenatedNodesList(node, nodes) { |
| 79 | if (!node) |
| 80 | return; |
| 81 | if (node.left === undefined && node.right === undefined) { |
| 82 | nodes.push(node); |
| 83 | return; |
| 84 | } |
| 85 | buildConcatenatedNodesList(node.left, nodes); |
| 86 | buildConcatenatedNodesList(node.right, nodes); |
| 87 | } |
| 88 | |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Recursively check if there is concatenation to localization call. |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 91 | * Concatenation is allowed between localized strings and non-alphabetic strings. |
| 92 | * It is not allowed between a localized string and a word. |
| 93 | * Example (allowed): ls`Status Code` + ": " |
| 94 | * Example (disallowed): ls`Status` + " Code" + ": " |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 95 | */ |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 96 | function checkConcatenation(parentNode, node, filePath, errors) { |
| 97 | function isWord(node) { |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 98 | return (node.type === esprimaTypes.LITERAL && !!node.value.match(/[a-z]/i)); |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 99 | } |
| 100 | function isConcatenation(node) { |
| 101 | return (node !== undefined && node.type === esprimaTypes.BI_EXPR && node.operator === '+'); |
| 102 | } |
| 103 | |
| 104 | if (isConcatenation(parentNode)) |
| 105 | return; |
| 106 | |
| 107 | if (isConcatenation(node)) { |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 108 | const concatenatedNodes = []; |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 109 | buildConcatenatedNodesList(node, concatenatedNodes); |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 110 | const hasLocalizationCall = |
| 111 | !!concatenatedNodes.find(currentNode => localizationUtils.isLocalizationCall(currentNode)); |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 112 | if (hasLocalizationCall) { |
| 113 | const hasAlphabeticLiteral = !!concatenatedNodes.find(currentNode => isWord(currentNode)); |
| 114 | if (hasAlphabeticLiteral) { |
| 115 | const code = escodegen.generate(node); |
| 116 | addError( |
| 117 | `${filePath}${ |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 118 | localizationUtils.getLocationMessage( |
| 119 | node.loc)}: string concatenation should be changed to variable substitution with ls: ${code}`, |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 120 | errors); |
| 121 | } |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 127 | * Check if an argument of a function is localized. |
| 128 | */ |
| 129 | function checkFunctionArgument(functionName, argumentIndex, node, filePath, errors) { |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 130 | if (node !== undefined && node.type === esprimaTypes.CALL_EXPR && |
| 131 | localizationUtils.verifyFunctionCallee(node.callee, functionName) && node.arguments !== undefined && |
| 132 | node.arguments.length > argumentIndex) { |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 133 | const arg = node.arguments[argumentIndex]; |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 134 | // No need to localize empty strings. |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 135 | if (arg.type === esprimaTypes.LITERAL && arg.value === '') |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 136 | return; |
| 137 | |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 138 | if (!localizationUtils.isLocalizationCall(arg)) { |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 139 | let order = ''; |
| 140 | switch (argumentIndex) { |
| 141 | case 0: |
| 142 | order = 'first'; |
| 143 | break; |
| 144 | case 1: |
| 145 | order = 'second'; |
| 146 | break; |
| 147 | case 2: |
| 148 | order = 'third'; |
| 149 | break; |
| 150 | default: |
| 151 | order = `${argumentIndex + 1}th`; |
| 152 | } |
| 153 | addError( |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 154 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: ${order} argument to ${ |
| 155 | functionName}() should be localized: ${escodegen.generate(node)}`, |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 156 | errors); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Check esprima node object that represents the AST of code |
| 163 | * to see if there is any localization error. |
| 164 | */ |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 165 | function analyzeNode(parentNode, node, filePath, errors) { |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 166 | if (node === undefined || node === null) |
| 167 | return; |
| 168 | |
| 169 | if (node instanceof Array) { |
| 170 | for (const child of node) |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 171 | analyzeNode(node, child, filePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 172 | |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | const keys = Object.keys(node); |
| 177 | const objKeys = keys.filter(key => { |
| 178 | return typeof node[key] === 'object' && key !== 'loc'; |
| 179 | }); |
| 180 | if (objKeys.length === 0) { |
| 181 | // base case: all values are non-objects -> node is a leaf |
| 182 | return; |
| 183 | } |
| 184 | |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 185 | const locCase = localizationUtils.getLocalizationCase(node); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 186 | const code = escodegen.generate(node); |
| 187 | switch (locCase) { |
| 188 | case 'Common.UIString': |
| 189 | case 'UI.formatLocalized': |
| 190 | const firstArgType = node.arguments[0].type; |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 191 | if (firstArgType !== esprimaTypes.LITERAL && firstArgType !== esprimaTypes.TEMP_LITERAL && |
| 192 | firstArgType !== esprimaTypes.IDENTIFIER && !excludeErrors.includes(code)) { |
| 193 | addError( |
| 194 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: first argument to call should be a string: ${ |
| 195 | code}`, |
| 196 | errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 197 | } |
| 198 | if (includesConditionalExpression(node.arguments.slice(1))) { |
| 199 | addError( |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 200 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: conditional(s) found in ${ |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 201 | code}. Please extract conditional(s) out of the localization call.`, |
| 202 | errors); |
| 203 | } |
| 204 | break; |
| 205 | case 'Tagged Template': |
| 206 | if (includesConditionalExpression(node.quasi.expressions)) { |
| 207 | addError( |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 208 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: conditional(s) found in ${ |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 209 | code}. Please extract conditional(s) out of the localization call.`, |
| 210 | errors); |
| 211 | } |
| 212 | break; |
| 213 | default: |
| 214 | // String concatenation to localization call(s) should be changed |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 215 | checkConcatenation(parentNode, node, filePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | |
| 219 | for (const key of objKeys) { |
| 220 | // recursively parse all the child nodes |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 221 | analyzeNode(node, node[key], filePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 225 | async function auditFileForLocalizability(filePath, errors) { |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 226 | const fileContent = await localizationUtils.parseFileContent(filePath); |
| 227 | const ast = esprima.parse(fileContent, {loc: true}); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 228 | |
Lorne Mitchell | 072e6f8 | 2019-05-22 21:58:44 +0000 | [diff] [blame^] | 229 | const relativeFilePath = localizationUtils.getRelativeFilePathFromSrc(filePath); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 230 | for (const node of ast.body) |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 231 | analyzeNode(undefined, node, relativeFilePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 232 | } |