blob: 75e9711c5e24567206ab6a695d5cef47334c43aa [file] [log] [blame]
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +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
5const fs = require('fs');
6const md5 = require('./md5');
7const {promisify} = require('util');
8const path = require('path');
9const readFileAsync = promisify(fs.readFile);
10const readDirAsync = promisify(fs.readdir);
11const statAsync = promisify(fs.stat);
Mandy Chen5128cc62019-09-23 16:46:00 +000012const writeFileAsync = promisify(fs.writeFile);
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +000013
14const esprimaTypes = {
15 BI_EXPR: 'BinaryExpression',
16 CALL_EXPR: 'CallExpression',
17 COND_EXPR: 'ConditionalExpression',
18 IDENTIFIER: 'Identifier',
19 LITERAL: 'Literal',
20 MEMBER_EXPR: 'MemberExpression',
Mandy Chen7a8829b2019-06-25 22:13:07 +000021 NEW_EXPR: 'NewExpression',
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +000022 TAGGED_TEMP_EXPR: 'TaggedTemplateExpression',
23 TEMP_LITERAL: 'TemplateLiteral'
24};
25
Paul Irishe7b977e2019-09-25 12:23:38 +000026const excludeFiles = ['Tests.js'];
27const excludeDirs = ['test_runner', 'Images', 'langpacks', 'node_modules', 'lighthouse'];
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +000028const cppSpecialCharactersMap = {
29 '"': '\\"',
30 '\\': '\\\\',
31 '\n': '\\n'
32};
33const IDSPrefix = 'IDS_DEVTOOLS_';
34
Yang Guo4fd355c2019-09-19 10:59:03 +020035const SRC_PATH = path.resolve(__dirname, '..', '..');
36const GRD_PATH = path.resolve(SRC_PATH, 'front_end', 'langpacks', 'devtools_ui_strings.grd');
Mandy Chen1e9d87b2019-09-18 17:18:15 +000037const SHARED_STRINGS_PATH = path.resolve(__dirname, '..', '..', 'front_end', 'langpacks', 'shared_strings.grdp');
Yang Guo4fd355c2019-09-19 10:59:03 +020038const NODE_MODULES_PATH = path.resolve(SRC_PATH, 'node_modules');
39const escodegen = require(path.resolve(NODE_MODULES_PATH, 'escodegen'));
40const esprima = require(path.resolve(NODE_MODULES_PATH, 'esprima'));
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +000041
42function getRelativeFilePathFromSrc(filePath) {
43 return path.relative(SRC_PATH, filePath);
44}
45
46function shouldParseDirectory(directoryName) {
47 return !excludeDirs.some(dir => directoryName.includes(dir));
48}
49
50/**
51 * @filepath can be partial path or full path, as long as it contains the file name.
52 */
53function shouldParseFile(filepath) {
54 return !excludeFiles.includes(path.basename(filepath));
55}
56
57async function parseFileContent(filePath) {
Mandy Chen167e7ad2019-11-01 15:50:37 -070058 let fileContent = await readFileAsync(filePath);
59 fileContent = fileContent.toString();
60 // normalize line ending to LF
61 fileContent = fileContent.replace(/\r\n/g, '\n');
62 return fileContent;
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +000063}
64
65function isNodeCallOnObject(node, objectName, propertyName) {
66 return node !== undefined && node.type === esprimaTypes.CALL_EXPR &&
67 verifyCallExpressionCallee(node.callee, objectName, propertyName);
68}
69
70function isNodeCommonUIStringCall(node) {
71 return isNodeCallOnObject(node, 'Common', 'UIString');
72}
73
Mandy Chen7a8829b2019-06-25 22:13:07 +000074function isNodeCommonUIStringFormat(node) {
75 return node && node.type === esprimaTypes.NEW_EXPR &&
76 verifyCallExpressionCallee(node.callee, 'Common', 'UIStringFormat');
77}
78
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +000079function isNodeUIformatLocalized(node) {
80 return isNodeCallOnObject(node, 'UI', 'formatLocalized');
81}
82
83function isNodelsTaggedTemplateExpression(node) {
84 return node !== undefined && node.type === esprimaTypes.TAGGED_TEMP_EXPR && verifyIdentifier(node.tag, 'ls') &&
85 node.quasi !== undefined && node.quasi.type !== undefined && node.quasi.type === esprimaTypes.TEMP_LITERAL;
86}
87
88/**
89 * Verify callee of objectName.propertyName(), e.g. Common.UIString().
90 */
91function verifyCallExpressionCallee(callee, objectName, propertyName) {
92 return callee !== undefined && callee.type === esprimaTypes.MEMBER_EXPR && callee.computed === false &&
93 verifyIdentifier(callee.object, objectName) && verifyIdentifier(callee.property, propertyName);
94}
95
96function verifyIdentifier(node, name) {
97 return node !== undefined && node.type === esprimaTypes.IDENTIFIER && node.name === name;
98}
99
100function getLocalizationCase(node) {
101 if (isNodeCommonUIStringCall(node))
102 return 'Common.UIString';
Mandy Chen7a8829b2019-06-25 22:13:07 +0000103 else if (isNodeCommonUIStringFormat(node))
104 return 'Common.UIStringFormat';
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000105 else if (isNodelsTaggedTemplateExpression(node))
106 return 'Tagged Template';
107 else if (isNodeUIformatLocalized(node))
108 return 'UI.formatLocalized';
109 else
110 return null;
111}
112
113function isLocalizationCall(node) {
114 return isNodeCommonUIStringCall(node) || isNodelsTaggedTemplateExpression(node) || isNodeUIformatLocalized(node);
115}
116
117/**
118 * Verify if callee is functionName() or object.functionName().
119 */
120function verifyFunctionCallee(callee, functionName) {
121 return callee !== undefined &&
122 ((callee.type === esprimaTypes.IDENTIFIER && callee.name === functionName) ||
123 (callee.type === esprimaTypes.MEMBER_EXPR && verifyIdentifier(callee.property, functionName)));
124}
125
126function getLocationMessage(location) {
127 if (location !== undefined && location.start !== undefined && location.end !== undefined &&
128 location.start.line !== undefined && location.end.line !== undefined) {
129 const startLine = location.start.line;
130 const endLine = location.end.line;
131 if (startLine === endLine)
132 return ` Line ${startLine}`;
133 else
134 return ` Line ${location.start.line}-${location.end.line}`;
135 }
136 return '';
137}
138
139function sanitizeStringIntoGRDFormat(str) {
140 return str.replace(/&/g, '&')
141 .replace(/</g, '&lt;')
142 .replace(/>/g, '&gt;')
143 .replace(/"/g, '&quot;')
144 .replace(/'/g, '&apos;')
145}
146
147function sanitizeStringIntoFrontendFormat(str) {
148 return str.replace(/&apos;/g, '\'')
149 .replace(/&quot;/g, '"')
150 .replace(/&gt;/g, '>')
151 .replace(/&lt;/g, '<')
152 .replace(/&amp;/g, '&');
153}
154
155function sanitizeString(str, specialCharactersMap) {
156 let sanitizedStr = '';
157 for (let i = 0; i < str.length; i++) {
158 let currChar = str.charAt(i);
159 if (specialCharactersMap[currChar] !== undefined)
160 currChar = specialCharactersMap[currChar];
161
162 sanitizedStr += currChar;
163 }
164 return sanitizedStr;
165}
166
167function sanitizeStringIntoCppFormat(str) {
168 return sanitizeString(str, cppSpecialCharactersMap);
169}
170
171async function getFilesFromItem(itemPath, filePaths, acceptedFileEndings) {
172 const stat = await statAsync(itemPath);
173 if (stat.isDirectory() && shouldParseDirectory(itemPath))
174 return await getFilesFromDirectory(itemPath, filePaths, acceptedFileEndings);
175
176 const hasAcceptedEnding =
177 acceptedFileEndings.some(acceptedEnding => itemPath.toLowerCase().endsWith(acceptedEnding.toLowerCase()));
178 if (hasAcceptedEnding && shouldParseFile(itemPath))
179 filePaths.push(itemPath);
180}
181
182async function getFilesFromDirectory(directoryPath, filePaths, acceptedFileEndings) {
183 const itemNames = await readDirAsync(directoryPath);
184 const promises = [];
185 for (const itemName of itemNames) {
186 const itemPath = path.resolve(directoryPath, itemName);
187 promises.push(getFilesFromItem(itemPath, filePaths, acceptedFileEndings));
188 }
189 return Promise.all(promises);
190}
191
192async function getChildDirectoriesFromDirectory(directoryPath) {
193 const dirPaths = [];
194 const itemNames = await readDirAsync(directoryPath);
195 for (const itemName of itemNames) {
196 const itemPath = path.resolve(directoryPath, itemName);
197 const stat = await statAsync(itemPath);
198 if (stat.isDirectory() && shouldParseDirectory(itemName))
199 dirPaths.push(itemPath);
200 }
201 return dirPaths;
202}
203
Mandy Chen78552632019-06-12 00:55:43 +0000204/**
205 * Pad leading / trailing whitespace with ''' so that the whitespace is preserved. See
206 * https://www.chromium.org/developers/tools-we-use-in-chromium/grit/grit-users-guide.
207 */
208function padWhitespace(str) {
209 if (str.match(/^\s+/))
210 str = `'''${str}`;
211 if (str.match(/\s+$/))
212 str = `${str}'''`;
213 return str;
214}
215
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000216function modifyStringIntoGRDFormat(str, args) {
217 let sanitizedStr = sanitizeStringIntoGRDFormat(str);
Mandy Chen78552632019-06-12 00:55:43 +0000218 sanitizedStr = padWhitespace(sanitizedStr);
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000219
220 const phRegex = /%d|%f|%s|%.[0-9]f/gm;
221 if (!str.match(phRegex))
222 return sanitizedStr;
223
224 let phNames;
225 if (args !== undefined)
226 phNames = args.map(arg => arg.replace(/[^a-zA-Z]/gm, '_').toUpperCase());
227 else
228 phNames = ['PH1', 'PH2', 'PH3', 'PH4', 'PH5', 'PH6', 'PH7', 'PH8', 'PH9'];
229
230 // It replaces all placeholders with <ph> tags.
231 let match;
232 let count = 1;
233 while ((match = phRegex.exec(sanitizedStr)) !== null) {
234 // This is necessary to avoid infinite loops with zero-width matches
235 if (match.index === phRegex.lastIndex)
236 phRegex.lastIndex++;
237
238 // match[0]: the placeholder (e.g. %d, %s, %.2f, etc.)
239 const ph = match[0];
240 // e.g. $1s, $1d, $1.2f
241 const newPh = `$${count}` + ph.substr(1);
242
243 const i = sanitizedStr.indexOf(ph);
244 sanitizedStr = `${sanitizedStr.substring(0, i)}<ph name="${phNames[count - 1]}">${newPh}</ph>${
245 sanitizedStr.substring(i + ph.length)}`;
246 count++;
247 }
248 return sanitizedStr;
249}
250
251function createGrdpMessage(ids, stringObj) {
Mandy Chenc94d52a2019-06-11 22:51:53 +0000252 let message = ` <message name="${ids}" desc="${stringObj.description || ''}">\n`;
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000253 message += ` ${modifyStringIntoGRDFormat(stringObj.string, stringObj.arguments)}\n`;
254 message += ' </message>\n';
255 return message;
256}
257
258function getIDSKey(str) {
Mandy Chen5128cc62019-09-23 16:46:00 +0000259 return `${IDSPrefix}${md5(str)}`;
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000260}
261
Mandy Chend97200b2019-07-29 21:13:39 +0000262// Get line number in the file of a character at given index
263function lineNumberOfIndex(str, index) {
264 const stringToIndex = str.substr(0, index);
265 return stringToIndex.split('\n').length;
266}
267
Mandy Chen5128cc62019-09-23 16:46:00 +0000268// Relative file path from grdp file with back slash replaced with forward slash
269function getRelativeGrdpPath(grdpPath) {
270 return path.relative(path.dirname(GRD_PATH), grdpPath).split(path.sep).join('/');
271}
272
273function getAbsoluteGrdpPath(relativeGrdpFilePath) {
274 return path.resolve(path.dirname(GRD_PATH), relativeGrdpFilePath);
275}
276
277// Create a <part> entry, given absolute path of a grdp file
278function createPartFileEntry(grdpFilePath) {
279 const relativeGrdpFilePath = getRelativeGrdpPath(grdpFilePath);
280 return ` <part file="${relativeGrdpFilePath}" />\n`;
281}
282
283// grdpFilePaths are sorted and are absolute file paths
284async function addChildGRDPFilePathsToGRD(grdpFilePaths) {
285 const grdFileContent = await parseFileContent(GRD_PATH);
286 const grdLines = grdFileContent.split('\n');
287
288 let newGrdFileContent = '';
289 for (let i = 0; i < grdLines.length; i++) {
290 const grdLine = grdLines[i];
291 // match[0]: full match
292 // match[1]: relative grdp file path
293 const match = grdLine.match(/<part file="(.*?)"/);
294 if (match) {
295 const grdpFilePathsRemaining = [];
296 for (const grdpFilePath of grdpFilePaths) {
297 if (grdpFilePath < getAbsoluteGrdpPath(match[1]))
298 newGrdFileContent += createPartFileEntry(grdpFilePath);
299 else
300 grdpFilePathsRemaining.push(grdpFilePath);
301 }
302 grdpFilePaths = grdpFilePathsRemaining;
303 } else if (grdLine.includes('</messages>')) {
304 for (const grdpFilePath of grdpFilePaths)
305 newGrdFileContent += createPartFileEntry(grdpFilePath);
306 }
307 newGrdFileContent += grdLine;
308 if (i < grdLines.length - 1)
309 newGrdFileContent += '\n';
310 }
311 return writeFileAsync(GRD_PATH, newGrdFileContent);
312}
313
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000314module.exports = {
Mandy Chen5128cc62019-09-23 16:46:00 +0000315 addChildGRDPFilePathsToGRD,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000316 createGrdpMessage,
Mandy Chen5128cc62019-09-23 16:46:00 +0000317 createPartFileEntry,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000318 escodegen,
319 esprima,
320 esprimaTypes,
Mandy Chen5128cc62019-09-23 16:46:00 +0000321 getAbsoluteGrdpPath,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000322 getChildDirectoriesFromDirectory,
323 getFilesFromDirectory,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000324 getIDSKey,
325 getLocalizationCase,
326 getLocationMessage,
327 getRelativeFilePathFromSrc,
Mandy Chen5128cc62019-09-23 16:46:00 +0000328 getRelativeGrdpPath,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000329 GRD_PATH,
330 IDSPrefix,
331 isLocalizationCall,
Mandy Chend97200b2019-07-29 21:13:39 +0000332 lineNumberOfIndex,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000333 modifyStringIntoGRDFormat,
334 parseFileContent,
Mandy Chen1e9d87b2019-09-18 17:18:15 +0000335 SHARED_STRINGS_PATH,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000336 sanitizeStringIntoCppFormat,
337 sanitizeStringIntoFrontendFormat,
Paul Irishe7b977e2019-09-25 12:23:38 +0000338 shouldParseDirectory,
Lorne Mitchellc56ff2d2019-05-28 23:35:03 +0000339 verifyFunctionCallee
Yang Guo4fd355c2019-09-19 10:59:03 +0200340};