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 | c56ff2d | 2019-05-28 23:35:03 +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 | c56ff2d | 2019-05-28 23:35:03 +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. |
Mandy Chen | 31930c4 | 2019-06-05 00:47:08 +0000 | [diff] [blame^] | 91 | * Concatenation is allowed between localized strings and strings that |
| 92 | * don't contain letters. |
| 93 | * Example (allowed): ls`Status code: ${statusCode}` |
| 94 | * Example (allowed): ls`Status code` + ': ' |
| 95 | * Example (disallowed): ls`Status code: ` + statusCode |
| 96 | * Example (disallowed): ls`Status ` + 'code' |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 97 | */ |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 98 | function checkConcatenation(parentNode, node, filePath, errors) { |
Mandy Chen | 31930c4 | 2019-06-05 00:47:08 +0000 | [diff] [blame^] | 99 | function isConcatenationDisallowed(node) { |
| 100 | if (node.type !== esprimaTypes.LITERAL && node.type !== esprimaTypes.TEMP_LITERAL) |
| 101 | return true; |
| 102 | |
| 103 | let value; |
| 104 | if (node.type === esprimaTypes.LITERAL) |
| 105 | value = node.value; |
| 106 | else if (node.type === esprimaTypes.TEMP_LITERAL && node.expressions.length === 0) |
| 107 | value = node.quasis[0].value.cooked; |
| 108 | |
| 109 | if (!value || typeof value !== 'string') |
| 110 | return true; |
| 111 | |
| 112 | return value.match(/[a-z]/i) !== null; |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 113 | } |
Mandy Chen | 31930c4 | 2019-06-05 00:47:08 +0000 | [diff] [blame^] | 114 | |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 115 | function isConcatenation(node) { |
| 116 | return (node !== undefined && node.type === esprimaTypes.BI_EXPR && node.operator === '+'); |
| 117 | } |
| 118 | |
| 119 | if (isConcatenation(parentNode)) |
| 120 | return; |
| 121 | |
| 122 | if (isConcatenation(node)) { |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 123 | const concatenatedNodes = []; |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 124 | buildConcatenatedNodesList(node, concatenatedNodes); |
Mandy Chen | 31930c4 | 2019-06-05 00:47:08 +0000 | [diff] [blame^] | 125 | const nonLocalizationCalls = concatenatedNodes.filter(node => !localizationUtils.isLocalizationCall(node)); |
| 126 | const hasLocalizationCall = nonLocalizationCalls.length !== concatenatedNodes.length; |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 127 | if (hasLocalizationCall) { |
Mandy Chen | 31930c4 | 2019-06-05 00:47:08 +0000 | [diff] [blame^] | 128 | // concatenation with localization call |
| 129 | const hasConcatenationViolation = nonLocalizationCalls.some(isConcatenationDisallowed); |
| 130 | if (hasConcatenationViolation) { |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 131 | const code = escodegen.generate(node); |
| 132 | addError( |
| 133 | `${filePath}${ |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 134 | localizationUtils.getLocationMessage( |
| 135 | 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] | 136 | errors); |
| 137 | } |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 143 | * Check if an argument of a function is localized. |
| 144 | */ |
| 145 | function checkFunctionArgument(functionName, argumentIndex, node, filePath, errors) { |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 146 | if (node !== undefined && node.type === esprimaTypes.CALL_EXPR && |
| 147 | localizationUtils.verifyFunctionCallee(node.callee, functionName) && node.arguments !== undefined && |
| 148 | node.arguments.length > argumentIndex) { |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 149 | const arg = node.arguments[argumentIndex]; |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 150 | // No need to localize empty strings. |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 151 | if (arg.type === esprimaTypes.LITERAL && arg.value === '') |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 152 | return; |
| 153 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 154 | if (!localizationUtils.isLocalizationCall(arg)) { |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 155 | let order = ''; |
| 156 | switch (argumentIndex) { |
| 157 | case 0: |
| 158 | order = 'first'; |
| 159 | break; |
| 160 | case 1: |
| 161 | order = 'second'; |
| 162 | break; |
| 163 | case 2: |
| 164 | order = 'third'; |
| 165 | break; |
| 166 | default: |
| 167 | order = `${argumentIndex + 1}th`; |
| 168 | } |
| 169 | addError( |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 170 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: ${order} argument to ${ |
| 171 | functionName}() should be localized: ${escodegen.generate(node)}`, |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 172 | errors); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Check esprima node object that represents the AST of code |
| 179 | * to see if there is any localization error. |
| 180 | */ |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 181 | function analyzeNode(parentNode, node, filePath, errors) { |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 182 | if (node === undefined || node === null) |
| 183 | return; |
| 184 | |
| 185 | if (node instanceof Array) { |
| 186 | for (const child of node) |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 187 | analyzeNode(node, child, filePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 188 | |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | const keys = Object.keys(node); |
| 193 | const objKeys = keys.filter(key => { |
| 194 | return typeof node[key] === 'object' && key !== 'loc'; |
| 195 | }); |
| 196 | if (objKeys.length === 0) { |
| 197 | // base case: all values are non-objects -> node is a leaf |
| 198 | return; |
| 199 | } |
| 200 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 201 | const locCase = localizationUtils.getLocalizationCase(node); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 202 | const code = escodegen.generate(node); |
| 203 | switch (locCase) { |
| 204 | case 'Common.UIString': |
| 205 | case 'UI.formatLocalized': |
| 206 | const firstArgType = node.arguments[0].type; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 207 | if (firstArgType !== esprimaTypes.LITERAL && firstArgType !== esprimaTypes.TEMP_LITERAL && |
| 208 | firstArgType !== esprimaTypes.IDENTIFIER && !excludeErrors.includes(code)) { |
| 209 | addError( |
| 210 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: first argument to call should be a string: ${ |
| 211 | code}`, |
| 212 | errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 213 | } |
| 214 | if (includesConditionalExpression(node.arguments.slice(1))) { |
| 215 | addError( |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 216 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: conditional(s) found in ${ |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 217 | code}. Please extract conditional(s) out of the localization call.`, |
| 218 | errors); |
| 219 | } |
| 220 | break; |
| 221 | case 'Tagged Template': |
| 222 | if (includesConditionalExpression(node.quasi.expressions)) { |
| 223 | addError( |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 224 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: conditional(s) found in ${ |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 225 | code}. Please extract conditional(s) out of the localization call.`, |
| 226 | errors); |
| 227 | } |
| 228 | break; |
| 229 | default: |
| 230 | // String concatenation to localization call(s) should be changed |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 231 | checkConcatenation(parentNode, node, filePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | |
| 235 | for (const key of objKeys) { |
| 236 | // recursively parse all the child nodes |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 237 | analyzeNode(node, node[key], filePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Krishna Govind | 897c867 | 2019-05-23 20:33:45 +0000 | [diff] [blame] | 241 | async function auditFileForLocalizability(filePath, errors) { |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 242 | const fileContent = await localizationUtils.parseFileContent(filePath); |
| 243 | const ast = esprima.parse(fileContent, {loc: true}); |
Krishna Govind | 897c867 | 2019-05-23 20:33:45 +0000 | [diff] [blame] | 244 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 245 | const relativeFilePath = localizationUtils.getRelativeFilePathFromSrc(filePath); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 246 | for (const node of ast.body) |
Lorne Mitchell | 7aa2c6c | 2019-04-03 03:50:10 +0000 | [diff] [blame] | 247 | analyzeNode(undefined, node, relativeFilePath, errors); |
Mandy Chen | 465b4f7 | 2019-03-21 22:52:54 +0000 | [diff] [blame] | 248 | } |