Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +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 | |
| 5 | /** |
| 6 | * Functions in this script parse DevTools frontend .js and module.json files, |
| 7 | * collect localizable strings, check if frontend strings are in .grd/.grdp |
| 8 | * files and report error if present. |
| 9 | */ |
| 10 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 11 | const path = require('path'); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 12 | const localizationUtils = require('./localization_utils'); |
| 13 | const escodegen = localizationUtils.escodegen; |
| 14 | const esprimaTypes = localizationUtils.esprimaTypes; |
| 15 | const esprima = localizationUtils.esprima; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 16 | const extensionStringKeys = ['category', 'destination', 'title', 'title-mac']; |
| 17 | |
| 18 | // Format of frontendStrings |
| 19 | // { IDS_md5-hash => { |
| 20 | // string: string, |
| 21 | // code: string, |
Mandy Chen | 1e9d87b | 2019-09-18 17:18:15 +0000 | [diff] [blame] | 22 | // isShared: boolean, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 23 | // filepath: string, |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 24 | // grdpPath: string, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 25 | // location: { |
| 26 | // start: { |
| 27 | // line: number, (1-based) |
| 28 | // column: number (0-based) |
| 29 | // }, |
| 30 | // end: { |
| 31 | // line: number, |
| 32 | // column: number |
| 33 | // } |
| 34 | // }, |
| 35 | // arguments: string[] |
| 36 | // } |
| 37 | // } |
| 38 | const frontendStrings = new Map(); |
| 39 | |
| 40 | // Format |
| 41 | // { |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 42 | // IDS_KEY => a list of { |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 43 | // actualIDSKey: string, // the IDS key in the message tag |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 44 | // description: string, |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 45 | // grdpPath: string, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 46 | // location: { |
| 47 | // start: { |
| 48 | // line: number |
| 49 | // }, |
| 50 | // end: { |
| 51 | // line: number |
| 52 | // } |
| 53 | // } |
| 54 | // } |
| 55 | // } |
| 56 | const IDSkeys = new Map(); |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 57 | const fileToGRDPMap = new Map(); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 58 | |
| 59 | const devtoolsFrontendPath = path.resolve(__dirname, '..', '..', 'front_end'); |
| 60 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 61 | async function parseLocalizableResourceMaps() { |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 62 | const grdpToFiles = new Map(); |
| 63 | const dirs = await localizationUtils.getChildDirectoriesFromDirectory(devtoolsFrontendPath); |
| 64 | const grdpToFilesPromises = dirs.map(dir => { |
| 65 | const files = []; |
| 66 | grdpToFiles.set(path.resolve(dir, `${path.basename(dir)}_strings.grdp`), files); |
| 67 | return localizationUtils.getFilesFromDirectory(dir, files, ['.js', 'module.json']); |
| 68 | }); |
| 69 | await Promise.all(grdpToFilesPromises); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 70 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 71 | const promises = []; |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 72 | for (const [grdpPath, files] of grdpToFiles) { |
| 73 | files.forEach(file => fileToGRDPMap.set(file, grdpPath)); |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 74 | promises.push(parseLocalizableStrings(files)); |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 75 | } |
| 76 | await Promise.all(promises); |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 77 | // Parse grd(p) files after frontend strings are processed so we know |
| 78 | // what to add or remove based on frontend strings |
| 79 | await parseIDSKeys(localizationUtils.GRD_PATH); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Mandy Chen | 7a8829b | 2019-06-25 22:13:07 +0000 | [diff] [blame] | 83 | * The following functions parse localizable strings (wrapped in Common.UIString, |
| 84 | * Common.UIStringFormat, UI.formatLocalized or ls``) from devtools frontend files. |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 85 | */ |
| 86 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 87 | async function parseLocalizableStrings(devtoolsFiles) { |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 88 | const promises = devtoolsFiles.map(filePath => parseLocalizableStringsFromFile(filePath)); |
| 89 | await Promise.all(promises); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | async function parseLocalizableStringsFromFile(filePath) { |
| 93 | const fileContent = await localizationUtils.parseFileContent(filePath); |
| 94 | if (path.basename(filePath) === 'module.json') |
| 95 | return parseLocalizableStringFromModuleJson(fileContent, filePath); |
| 96 | |
Mandy Chen | 436efc7 | 2019-09-18 17:43:40 +0000 | [diff] [blame^] | 97 | let ast; |
| 98 | try { |
| 99 | ast = esprima.parseModule(fileContent, {loc: true}); |
| 100 | } catch (e) { |
| 101 | throw new Error( |
| 102 | `DevTools localization parser failed:\n${localizationUtils.getRelativeFilePathFromSrc(filePath)}: ${ |
| 103 | e.message}` + |
| 104 | `\nThis error is likely due to unsupported JavaScript features.` + |
| 105 | ` Such features are not supported by eslint either and will cause presubmit to fail.` + |
| 106 | ` Please update the code and use official JavaScript features.`); |
| 107 | } |
| 108 | for (const node of ast.body) { |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 109 | parseLocalizableStringFromNode(node, filePath); |
Mandy Chen | 436efc7 | 2019-09-18 17:43:40 +0000 | [diff] [blame^] | 110 | } |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | function parseLocalizableStringFromModuleJson(fileContent, filePath) { |
| 114 | const fileJSON = JSON.parse(fileContent); |
| 115 | if (!fileJSON.extensions) |
| 116 | return; |
| 117 | |
| 118 | for (const extension of fileJSON.extensions) { |
| 119 | for (const key in extension) { |
| 120 | if (extensionStringKeys.includes(key)) { |
| 121 | addString(extension[key], extension[key], filePath); |
| 122 | } else if (key === 'device') { |
| 123 | addString(extension.device.title, extension.device.title, filePath); |
| 124 | } else if (key === 'options') { |
| 125 | for (const option of extension.options) { |
| 126 | addString(option.title, option.title, filePath); |
| 127 | if (option.text !== undefined) |
| 128 | addString(option.text, option.text, filePath); |
| 129 | } |
Mandy Chen | 609679b | 2019-09-10 16:04:08 +0000 | [diff] [blame] | 130 | } else if (key === 'defaultValue' && Array.isArray(extension[key])) { |
| 131 | for (const defaultVal of extension[key]) { |
| 132 | if (defaultVal.title) |
| 133 | addString(defaultVal.title, defaultVal.title, filePath); |
| 134 | } |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | function parseLocalizableStringFromNode(node, filePath) { |
| 141 | if (!node) |
| 142 | return; |
| 143 | |
| 144 | if (Array.isArray(node)) { |
| 145 | for (const child of node) |
| 146 | parseLocalizableStringFromNode(child, filePath); |
| 147 | |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | const keys = Object.keys(node); |
| 152 | const objKeys = keys.filter(key => key !== 'loc' && typeof node[key] === 'object'); |
| 153 | if (objKeys.length === 0) { |
| 154 | // base case: all values are non-objects -> node is a leaf |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | const locCase = localizationUtils.getLocalizationCase(node); |
| 159 | switch (locCase) { |
| 160 | case 'Common.UIString': |
Mandy Chen | 7a8829b | 2019-06-25 22:13:07 +0000 | [diff] [blame] | 161 | case 'Common.UIStringFormat': |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 162 | handleCommonUIString(node, filePath); |
| 163 | break; |
| 164 | case 'UI.formatLocalized': |
| 165 | if (node.arguments !== undefined && node.arguments[1] !== undefined && node.arguments[1].elements !== undefined) |
| 166 | handleCommonUIString(node, filePath, node.arguments[1].elements); |
| 167 | break; |
| 168 | case 'Tagged Template': |
| 169 | handleTemplateLiteral(node.quasi, escodegen.generate(node), filePath); |
| 170 | break; |
| 171 | case null: |
| 172 | break; |
| 173 | default: |
| 174 | throw new Error( |
| 175 | `${filePath}${localizationUtils.getLocationMessage(node.loc)}: unexpected localization case for node: ${ |
| 176 | escodegen.generate(node)}`); |
| 177 | } |
| 178 | |
| 179 | for (const key of objKeys) { |
| 180 | // recursively parse all the child nodes |
| 181 | parseLocalizableStringFromNode(node[key], filePath); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | function handleCommonUIString(node, filePath, argumentNodes) { |
| 186 | if (argumentNodes === undefined) |
| 187 | argumentNodes = node.arguments.slice(1); |
| 188 | const firstArgType = node.arguments[0].type; |
| 189 | switch (firstArgType) { |
| 190 | case esprimaTypes.LITERAL: |
| 191 | const message = node.arguments[0].value; |
| 192 | addString(message, escodegen.generate(node), filePath, node.loc, argumentNodes); |
| 193 | break; |
| 194 | case esprimaTypes.TEMP_LITERAL: |
| 195 | handleTemplateLiteral(node.arguments[0], escodegen.generate(node), filePath, argumentNodes); |
| 196 | break; |
| 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | function handleTemplateLiteral(node, code, filePath, argumentNodes) { |
| 203 | if (node.expressions.length === 0) { |
| 204 | // template literal does not contain any variables, parse the value |
| 205 | addString(node.quasis[0].value.cooked, code, filePath, node.loc, argumentNodes); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | argumentNodes = node.expressions; |
| 210 | let processedMsg = ''; |
| 211 | for (let i = 0; i < node.quasis.length; i++) { |
| 212 | processedMsg += node.quasis[i].value.cooked; |
| 213 | if (i < node.expressions.length) { |
| 214 | // add placeholder for variable so that |
| 215 | // the ph tag gets generated |
| 216 | processedMsg += '%s'; |
| 217 | } |
| 218 | } |
| 219 | addString(processedMsg, code, filePath, node.loc, argumentNodes); |
| 220 | } |
| 221 | |
| 222 | function addString(str, code, filePath, location, argumentNodes) { |
Mandy Chen | 1e9d87b | 2019-09-18 17:18:15 +0000 | [diff] [blame] | 223 | const ids = localizationUtils.getIDSKey(str); |
| 224 | |
| 225 | // In the case of duplicates, the corresponding grdp message should be added |
| 226 | // to the shared strings file only if the duplicate strings span across different |
| 227 | // grdp files |
| 228 | const existingString = frontendStrings.get(ids); |
| 229 | if (existingString) { |
| 230 | if (!existingString.isShared && existingString.grdpPath !== fileToGRDPMap.get(filePath)) { |
| 231 | existingString.isShared = true; |
| 232 | existingString.grdpPath = localizationUtils.SHARED_STRINGS_PATH; |
| 233 | } |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | const currentString = |
| 238 | {string: str, code: code, isShared: false, filepath: filePath, grdpPath: fileToGRDPMap.get(filePath)}; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 239 | if (location) |
| 240 | currentString.location = location; |
| 241 | if (argumentNodes && argumentNodes.length > 0) |
| 242 | currentString.arguments = argumentNodes.map(argNode => escodegen.generate(argNode)); |
| 243 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 244 | frontendStrings.set(ids, currentString); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * The following functions parse <message>s and their IDS keys from |
| 249 | * devtools frontend grdp files. |
| 250 | */ |
| 251 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 252 | async function parseIDSKeys(grdFilePath) { |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 253 | // NOTE: this function assumes that no <message> tags are present in the parent |
| 254 | const grdpFilePaths = await parseGRDFile(grdFilePath); |
| 255 | await parseGRDPFiles(grdpFilePaths); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | async function parseGRDFile(grdFilePath) { |
| 259 | const fileContent = await localizationUtils.parseFileContent(grdFilePath); |
| 260 | const grdFileDir = path.dirname(grdFilePath); |
| 261 | const partFileRegex = /<part file="(.*?)"/g; |
| 262 | |
| 263 | let match; |
| 264 | const grdpFilePaths = new Set(); |
| 265 | while ((match = partFileRegex.exec(fileContent)) !== null) { |
| 266 | if (match.index === partFileRegex.lastIndex) |
| 267 | partFileRegex.lastIndex++; |
| 268 | // match[0]: full match |
| 269 | // match[1]: part file path |
| 270 | grdpFilePaths.add(path.resolve(grdFileDir, match[1])); |
| 271 | } |
| 272 | return grdpFilePaths; |
| 273 | } |
| 274 | |
| 275 | function parseGRDPFiles(grdpFilePaths) { |
| 276 | const promises = Array.from(grdpFilePaths, grdpFilePath => parseGRDPFile(grdpFilePath)); |
| 277 | return Promise.all(promises); |
| 278 | } |
| 279 | |
| 280 | function trimGrdpPlaceholder(placeholder) { |
| 281 | const exampleRegex = new RegExp('<ex>.*?<\/ex>', 'gms'); |
| 282 | // $1s<ex>my example</ex> -> $1s |
| 283 | return placeholder.replace(exampleRegex, '').trim(); |
| 284 | } |
| 285 | |
| 286 | function convertToFrontendPlaceholders(message) { |
| 287 | // <ph name="phname">$1s<ex>my example</ex></ph> and <ph name="phname2">$2.3f</ph> |
| 288 | // match[0]: <ph name="phname1">$1s</ph> |
| 289 | // match[1]: $1s<ex>my example</ex> |
| 290 | let placeholderRegex = new RegExp('<ph[^>]*>(.*?)<\/ph>', 'gms'); |
| 291 | let match; |
| 292 | while ((match = placeholderRegex.exec(message)) !== null) { |
| 293 | const placeholder = match[0]; |
| 294 | const placeholderValue = trimGrdpPlaceholder(match[1]); |
| 295 | const newPlaceholderValue = placeholderValue.replace(/\$[1-9]/, '%'); |
| 296 | message = |
| 297 | message.substring(0, match.index) + newPlaceholderValue + message.substring(match.index + placeholder.length); |
| 298 | // Modified the message, so search from the beginning of the string again. |
| 299 | placeholderRegex.lastIndex = 0; |
| 300 | } |
| 301 | return message; |
| 302 | } |
| 303 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 304 | async function parseGRDPFile(filePath) { |
| 305 | const fileContent = await localizationUtils.parseFileContent(filePath); |
| 306 | |
Mandy Chen | 7855263 | 2019-06-12 00:55:43 +0000 | [diff] [blame] | 307 | function stripWhitespacePadding(message) { |
| 308 | let match = message.match(/^'''/); |
| 309 | if (match) |
| 310 | message = message.substring(3); |
| 311 | match = message.match(/(.*?)'''$/); |
| 312 | if (match) |
| 313 | message = match[1]; |
| 314 | return message; |
| 315 | } |
| 316 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 317 | // Example: |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 318 | // <message name="IDS_DEVTOOLS_md5_hash" desc="Description of this message"> |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 319 | // Message text here with optional placeholders <ph name="phname">$1s</ph> |
| 320 | // </message> |
| 321 | // match[0]: the entire '<message>...</message>' block. |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 322 | // match[1]: 'IDS_DEVTOOLS_md5_hash' |
| 323 | // match[2]: 'Description of this message' |
| 324 | // match[3]: ' Message text here with optional placeholders <ph name="phname">$1s</ph>\n ' |
| 325 | const messageRegex = new RegExp('<message[^>]*name="([^"]*)"[^>]*desc="([^"]*)"[^>]*>\s*\n(.*?)<\/message>', 'gms'); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 326 | let match; |
| 327 | while ((match = messageRegex.exec(fileContent)) !== null) { |
Mandy Chen | d97200b | 2019-07-29 21:13:39 +0000 | [diff] [blame] | 328 | const line = localizationUtils.lineNumberOfIndex(fileContent, match.index); |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 329 | const actualIDSKey = match[1]; |
| 330 | const description = match[2]; |
| 331 | let message = match[3]; |
Mandy Chen | 7855263 | 2019-06-12 00:55:43 +0000 | [diff] [blame] | 332 | message = convertToFrontendPlaceholders(message.trim()); |
| 333 | message = stripWhitespacePadding(message); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 334 | message = localizationUtils.sanitizeStringIntoFrontendFormat(message); |
| 335 | |
| 336 | const ids = localizationUtils.getIDSKey(message); |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 337 | addMessage(ids, actualIDSKey, filePath, line, description); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 341 | function addMessage(expectedIDSKey, actualIDSKey, grdpPath, line, description) { |
| 342 | if (!IDSkeys.has(expectedIDSKey)) |
| 343 | IDSkeys.set(expectedIDSKey, []); |
| 344 | |
| 345 | IDSkeys.get(expectedIDSKey).push({actualIDSKey, grdpPath, location: {start: {line}, end: {line}}, description}); |
| 346 | } |
| 347 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 348 | /** |
| 349 | * The following functions compare frontend localizable strings |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 350 | * with grdp <message>s and report error of resources to add, |
| 351 | * remove or modify. |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 352 | */ |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 353 | async function getAndReportResourcesToAdd() { |
| 354 | const keysToAddToGRD = getMessagesToAdd(); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 355 | if (keysToAddToGRD.size === 0) |
| 356 | return; |
| 357 | |
| 358 | let errorStr = 'The following frontend string(s) need to be added to GRD/GRDP file(s).\n'; |
| 359 | errorStr += 'Please refer to auto-generated message(s) below and modify as needed.\n\n'; |
| 360 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 361 | // Example error message: |
| 362 | // third_party/blink/renderer/devtools/front_end/network/NetworkDataGridNode.js Line 973: ls`(disk cache)` |
| 363 | // Add a new message tag for this string to third_party\blink\renderer\devtools\front_end\network\network_strings.grdp |
| 364 | // <message name="IDS_DEVTOOLS_ad86890fb40822a3b12627efaca4ecd7" desc="Fill in the description."> |
| 365 | // (disk cache) |
| 366 | // </message> |
| 367 | for (const [key, stringObj] of keysToAddToGRD) { |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 368 | errorStr += `${localizationUtils.getRelativeFilePathFromSrc(stringObj.filepath)}${ |
| 369 | localizationUtils.getLocationMessage(stringObj.location)}: ${stringObj.code}\n`; |
| 370 | errorStr += `Add a new message tag for this string to ${ |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 371 | localizationUtils.getRelativeFilePathFromSrc(fileToGRDPMap.get(stringObj.filepath))}\n\n`; |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 372 | errorStr += localizationUtils.createGrdpMessage(key, stringObj); |
| 373 | } |
| 374 | return errorStr; |
| 375 | } |
| 376 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 377 | function getAndReportResourcesToRemove() { |
| 378 | const keysToRemoveFromGRD = getMessagesToRemove(); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 379 | if (keysToRemoveFromGRD.size === 0) |
| 380 | return; |
| 381 | |
| 382 | let errorStr = |
| 383 | '\nThe message(s) associated with the following IDS key(s) should be removed from its GRD/GRDP file(s):\n'; |
| 384 | // Example error message: |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 385 | // third_party/blink/renderer/devtools/front_end/accessibility/accessibility_strings.grdp Line 300: IDS_DEVTOOLS_c9bbad3047af039c14d0e7ec957bb867 |
| 386 | for (const [ids, messages] of keysToRemoveFromGRD) { |
| 387 | messages.forEach( |
| 388 | message => errorStr += `${localizationUtils.getRelativeFilePathFromSrc(message.grdpPath)}${ |
| 389 | localizationUtils.getLocationMessage(message.location)}: ${ids}\n\n`); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 390 | } |
| 391 | return errorStr; |
| 392 | } |
| 393 | |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 394 | function getAndReportIDSKeysToModify() { |
| 395 | const messagesToModify = getIDSKeysToModify(); |
| 396 | if (messagesToModify.size === 0) |
| 397 | return; |
| 398 | |
| 399 | let errorStr = '\nThe following GRD/GRDP message(s) do not have the correct IDS key.\n'; |
| 400 | errorStr += 'Please update the key(s) by changing the "name" value.\n\n'; |
| 401 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 402 | for (const [expectedIDSKey, messages] of messagesToModify) { |
| 403 | messages.forEach( |
| 404 | message => errorStr += `${localizationUtils.getRelativeFilePathFromSrc(message.grdpPath)}${ |
| 405 | localizationUtils.getLocationMessage( |
| 406 | message.location)}:\n${message.actualIDSKey} --> ${expectedIDSKey}\n\n`); |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 407 | } |
| 408 | return errorStr; |
| 409 | } |
| 410 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 411 | function getMessagesToAdd() { |
| 412 | // If a message with ids key exists in grdpPath |
| 413 | function messageExists(ids, grdpPath) { |
| 414 | const messages = IDSkeys.get(ids); |
| 415 | return messages.some(message => message.grdpPath === grdpPath); |
| 416 | } |
| 417 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 418 | const difference = []; |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 419 | for (const [ids, frontendString] of frontendStrings) { |
| 420 | if (!IDSkeys.has(ids) || !messageExists(ids, frontendString.grdpPath)) |
| 421 | difference.push([ids, frontendString]); |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 422 | } |
| 423 | return new Map(difference.sort()); |
| 424 | } |
| 425 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 426 | // Return a map from the expected IDS key to a list of messages |
| 427 | // whose actual IDS keys need to be modified. |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 428 | function getIDSKeysToModify() { |
| 429 | const messagesToModify = new Map(); |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 430 | for (const [expectedIDSKey, messages] of IDSkeys) { |
| 431 | for (const message of messages) { |
| 432 | if (expectedIDSKey !== message.actualIDSKey) { |
| 433 | if (messagesToModify.has(expectedIDSKey)) |
| 434 | messagesToModify.get(expectedIDSKey).push(message); |
| 435 | else |
| 436 | messagesToModify.set(expectedIDSKey, [message]); |
| 437 | } |
| 438 | } |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 439 | } |
| 440 | return messagesToModify; |
| 441 | } |
| 442 | |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 443 | function getMessagesToRemove() { |
| 444 | const difference = new Map(); |
| 445 | for (const [ids, messages] of IDSkeys) { |
| 446 | if (!frontendStrings.has(ids)) { |
| 447 | difference.set(ids, messages); |
| 448 | continue; |
| 449 | } |
| 450 | |
| 451 | const expectedGrdpPath = frontendStrings.get(ids).grdpPath; |
| 452 | const messagesInGrdp = []; |
| 453 | const messagesToRemove = []; |
| 454 | messages.forEach(message => { |
| 455 | if (message.grdpPath !== expectedGrdpPath) |
| 456 | messagesToRemove.push(message); |
| 457 | else |
| 458 | messagesInGrdp.push(message); |
| 459 | }); |
| 460 | |
| 461 | if (messagesToRemove.length === 0 && messagesInGrdp.length === 1) |
| 462 | continue; |
| 463 | |
| 464 | if (messagesInGrdp.length > 1) { |
| 465 | // If there are more than one messages with ids in the |
| 466 | // expected grdp file, keep one with the longest |
| 467 | // description and delete all the other messages |
| 468 | const longestDescription = getLongestDescription(messagesInGrdp); |
| 469 | let foundMessageToKeep = false; |
| 470 | for (const message of messagesInGrdp) { |
| 471 | if (message.description === longestDescription && !foundMessageToKeep) { |
| 472 | foundMessageToKeep = true; |
| 473 | continue; |
| 474 | } |
| 475 | messagesToRemove.push(message); |
| 476 | } |
| 477 | } |
| 478 | difference.set(ids, messagesToRemove); |
| 479 | } |
| 480 | return difference; |
| 481 | } |
| 482 | |
| 483 | function getLongestDescription(messages) { |
| 484 | let longestDescription = ''; |
| 485 | messages.forEach(message => { |
| 486 | if (message.description.length > longestDescription.length) |
| 487 | longestDescription = message.description; |
| 488 | }); |
| 489 | return longestDescription; |
| 490 | } |
| 491 | |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 492 | module.exports = { |
Mandy Chen | c94d52a | 2019-06-11 22:51:53 +0000 | [diff] [blame] | 493 | frontendStrings, |
| 494 | IDSkeys, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 495 | parseLocalizableResourceMaps, |
Mandy Chen | 81d4fc4 | 2019-07-11 23:12:02 +0000 | [diff] [blame] | 496 | getAndReportIDSKeysToModify, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 497 | getAndReportResourcesToAdd, |
| 498 | getAndReportResourcesToRemove, |
Mandy Chen | 4a7ad05 | 2019-07-16 16:09:29 +0000 | [diff] [blame] | 499 | getIDSKeysToModify, |
| 500 | getLongestDescription, |
| 501 | getMessagesToAdd, |
| 502 | getMessagesToRemove, |
Lorne Mitchell | c56ff2d | 2019-05-28 23:35:03 +0000 | [diff] [blame] | 503 | }; |