blob: 30102a702b1d7c5d9f05c99c5ac5eb414f2432df [file] [log] [blame]
Mandy Chen465b4f72019-03-21 22:52:54 +00001// 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
13const path = require('path');
Lorne Mitchell072e6f82019-05-22 21:58:44 +000014const localizationUtils = require('./localization_utils/localization_utils');
15const esprimaTypes = localizationUtils.esprimaTypes;
16const escodegen = localizationUtils.escodegen;
17const esprima = localizationUtils.esprima;
Mandy Chen465b4f72019-03-21 22:52:54 +000018
Mandy Chen465b4f72019-03-21 22:52:54 +000019// Exclude known errors
20const 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 Chen465b4f72019-03-21 22:52:54 +000027const 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
33async 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 Mitchell072e6f82019-05-22 21:58:44 +000045 await localizationUtils.getFilesFromDirectory(frontendPath, filePaths, ['.js']);
Mandy Chen465b4f72019-03-21 22:52:54 +000046 } 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
67main();
68
Mandy Chen465b4f72019-03-21 22:52:54 +000069function includesConditionalExpression(listOfElements) {
70 return listOfElements.filter(ele => ele !== undefined && ele.type === esprimaTypes.COND_EXPR).length > 0;
71}
72
Mandy Chen465b4f72019-03-21 22:52:54 +000073function addError(error, errors) {
74 if (!errors.includes(error))
75 errors.push(error);
76}
77
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +000078function 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 Chen465b4f72019-03-21 22:52:54 +000089/**
90 * Recursively check if there is concatenation to localization call.
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +000091 * 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 Chen465b4f72019-03-21 22:52:54 +000095 */
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +000096function checkConcatenation(parentNode, node, filePath, errors) {
97 function isWord(node) {
Lorne Mitchell072e6f82019-05-22 21:58:44 +000098 return (node.type === esprimaTypes.LITERAL && !!node.value.match(/[a-z]/i));
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +000099 }
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 Mitchell072e6f82019-05-22 21:58:44 +0000108 const concatenatedNodes = [];
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000109 buildConcatenatedNodesList(node, concatenatedNodes);
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000110 const hasLocalizationCall =
111 !!concatenatedNodes.find(currentNode => localizationUtils.isLocalizationCall(currentNode));
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000112 if (hasLocalizationCall) {
113 const hasAlphabeticLiteral = !!concatenatedNodes.find(currentNode => isWord(currentNode));
114 if (hasAlphabeticLiteral) {
115 const code = escodegen.generate(node);
116 addError(
117 `${filePath}${
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000118 localizationUtils.getLocationMessage(
119 node.loc)}: string concatenation should be changed to variable substitution with ls: ${code}`,
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000120 errors);
121 }
Mandy Chen465b4f72019-03-21 22:52:54 +0000122 }
123 }
124}
125
126/**
Mandy Chen465b4f72019-03-21 22:52:54 +0000127 * Check if an argument of a function is localized.
128 */
129function checkFunctionArgument(functionName, argumentIndex, node, filePath, errors) {
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000130 if (node !== undefined && node.type === esprimaTypes.CALL_EXPR &&
131 localizationUtils.verifyFunctionCallee(node.callee, functionName) && node.arguments !== undefined &&
132 node.arguments.length > argumentIndex) {
Mandy Chen465b4f72019-03-21 22:52:54 +0000133 const arg = node.arguments[argumentIndex];
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000134 // No need to localize empty strings.
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000135 if (arg.type === esprimaTypes.LITERAL && arg.value === '')
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000136 return;
137
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000138 if (!localizationUtils.isLocalizationCall(arg)) {
Mandy Chen465b4f72019-03-21 22:52:54 +0000139 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 Mitchell072e6f82019-05-22 21:58:44 +0000154 `${filePath}${localizationUtils.getLocationMessage(node.loc)}: ${order} argument to ${
155 functionName}() should be localized: ${escodegen.generate(node)}`,
Mandy Chen465b4f72019-03-21 22:52:54 +0000156 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 Mitchell7aa2c6c2019-04-03 03:50:10 +0000165function analyzeNode(parentNode, node, filePath, errors) {
Mandy Chen465b4f72019-03-21 22:52:54 +0000166 if (node === undefined || node === null)
167 return;
168
169 if (node instanceof Array) {
170 for (const child of node)
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000171 analyzeNode(node, child, filePath, errors);
Mandy Chen465b4f72019-03-21 22:52:54 +0000172
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 Mitchell072e6f82019-05-22 21:58:44 +0000185 const locCase = localizationUtils.getLocalizationCase(node);
Mandy Chen465b4f72019-03-21 22:52:54 +0000186 const code = escodegen.generate(node);
187 switch (locCase) {
188 case 'Common.UIString':
189 case 'UI.formatLocalized':
190 const firstArgType = node.arguments[0].type;
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000191 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 Chen465b4f72019-03-21 22:52:54 +0000197 }
198 if (includesConditionalExpression(node.arguments.slice(1))) {
199 addError(
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000200 `${filePath}${localizationUtils.getLocationMessage(node.loc)}: conditional(s) found in ${
Mandy Chen465b4f72019-03-21 22:52:54 +0000201 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 Mitchell072e6f82019-05-22 21:58:44 +0000208 `${filePath}${localizationUtils.getLocationMessage(node.loc)}: conditional(s) found in ${
Mandy Chen465b4f72019-03-21 22:52:54 +0000209 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 Mitchell7aa2c6c2019-04-03 03:50:10 +0000215 checkConcatenation(parentNode, node, filePath, errors);
Mandy Chen465b4f72019-03-21 22:52:54 +0000216 break;
217 }
218
219 for (const key of objKeys) {
220 // recursively parse all the child nodes
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000221 analyzeNode(node, node[key], filePath, errors);
Mandy Chen465b4f72019-03-21 22:52:54 +0000222 }
223}
224
Mandy Chen465b4f72019-03-21 22:52:54 +0000225async function auditFileForLocalizability(filePath, errors) {
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000226 const fileContent = await localizationUtils.parseFileContent(filePath);
227 const ast = esprima.parse(fileContent, {loc: true});
Mandy Chen465b4f72019-03-21 22:52:54 +0000228
Lorne Mitchell072e6f82019-05-22 21:58:44 +0000229 const relativeFilePath = localizationUtils.getRelativeFilePathFromSrc(filePath);
Mandy Chen465b4f72019-03-21 22:52:54 +0000230 for (const node of ast.body)
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000231 analyzeNode(undefined, node, relativeFilePath, errors);
Mandy Chen465b4f72019-03-21 22:52:54 +0000232}