|
1 |
| -/** |
2 |
| - * Copyright (c) Facebook, Inc. and its affiliates. |
3 |
| - * |
4 |
| - * This source code is licensed under the MIT license found in the |
5 |
| - * LICENSE file in the root directory of this source tree. |
6 |
| - */ |
7 | 1 | 'use strict';
|
8 | 2 |
|
9 |
| -const parser = require('@babel/parser'); |
10 | 3 | const fs = require('fs');
|
11 | 4 | const path = require('path');
|
12 |
| -const traverse = require('@babel/traverse').default; |
13 |
| -const {evalStringConcat} = require('../shared/evalToString'); |
14 |
| -const invertObject = require('./invertObject'); |
| 5 | +const {execSync} = require('child_process'); |
15 | 6 |
|
16 |
| -const babylonOptions = { |
17 |
| - sourceType: 'module', |
18 |
| - // As a parser, babylon has its own options and we can't directly |
19 |
| - // import/require a babel preset. It should be kept **the same** as |
20 |
| - // the `babel-plugin-syntax-*` ones specified in |
21 |
| - // https://github.com/facebook/fbjs/blob/master/packages/babel-preset-fbjs/configure.js |
22 |
| - plugins: [ |
23 |
| - 'classProperties', |
24 |
| - 'flow', |
25 |
| - 'jsx', |
26 |
| - 'trailingFunctionCommas', |
27 |
| - 'objectRestSpread', |
28 |
| - ], |
29 |
| -}; |
30 |
| - |
31 |
| -module.exports = function(opts) { |
32 |
| - if (!opts || !('errorMapFilePath' in opts)) { |
33 |
| - throw new Error( |
34 |
| - 'Missing options. Ensure you pass an object with `errorMapFilePath`.' |
35 |
| - ); |
| 7 | +async function main() { |
| 8 | + const originalJSON = JSON.parse( |
| 9 | + fs.readFileSync(path.resolve(__dirname, '../error-codes/codes.json')) |
| 10 | + ); |
| 11 | + const existingMessages = new Set(); |
| 12 | + const codes = Object.keys(originalJSON); |
| 13 | + let nextCode = 0; |
| 14 | + for (let i = 0; i < codes.length; i++) { |
| 15 | + const codeStr = codes[i]; |
| 16 | + const message = originalJSON[codeStr]; |
| 17 | + const code = parseInt(codeStr, 10); |
| 18 | + existingMessages.add(message); |
| 19 | + if (code >= nextCode) { |
| 20 | + nextCode = code + 1; |
| 21 | + } |
36 | 22 | }
|
37 | 23 |
|
38 |
| - const errorMapFilePath = opts.errorMapFilePath; |
39 |
| - let existingErrorMap; |
| 24 | + console.log('Searching `build` directory for unminified errors...\n'); |
| 25 | + |
| 26 | + let out; |
40 | 27 | try {
|
41 |
| - // Using `fs.readFileSync` instead of `require` here, because `require()` |
42 |
| - // calls are cached, and the cache map is not properly invalidated after |
43 |
| - // file changes. |
44 |
| - existingErrorMap = JSON.parse( |
45 |
| - fs.readFileSync( |
46 |
| - path.join(__dirname, path.basename(errorMapFilePath)), |
47 |
| - 'utf8' |
48 |
| - ) |
49 |
| - ); |
| 28 | + out = execSync( |
| 29 | + "git --no-pager grep -n --untracked --no-exclude-standard '/*! <expected-error-format>' -- build" |
| 30 | + ).toString(); |
50 | 31 | } catch (e) {
|
51 |
| - existingErrorMap = {}; |
52 |
| - } |
53 |
| - |
54 |
| - const allErrorIDs = Object.keys(existingErrorMap); |
55 |
| - let currentID; |
56 |
| - |
57 |
| - if (allErrorIDs.length === 0) { |
58 |
| - // Map is empty |
59 |
| - currentID = 0; |
60 |
| - } else { |
61 |
| - currentID = Math.max.apply(null, allErrorIDs) + 1; |
| 32 | + if (e.status === 1 && e.stdout.toString() === '') { |
| 33 | + // No unminified errors found. |
| 34 | + return; |
| 35 | + } |
| 36 | + throw e; |
62 | 37 | }
|
63 | 38 |
|
64 |
| - // Here we invert the map object in memory for faster error code lookup |
65 |
| - existingErrorMap = invertObject(existingErrorMap); |
66 |
| - |
67 |
| - function transform(source) { |
68 |
| - const ast = parser.parse(source, babylonOptions); |
69 |
| - |
70 |
| - traverse(ast, { |
71 |
| - CallExpression: { |
72 |
| - exit(astPath) { |
73 |
| - if (astPath.get('callee').isIdentifier({name: 'invariant'})) { |
74 |
| - const node = astPath.node; |
| 39 | + let newJSON = null; |
| 40 | + const regex = /\<expected-error-format\>"(.+?)"\<\/expected-error-format\>/g; |
| 41 | + do { |
| 42 | + const match = regex.exec(out); |
| 43 | + if (match === null) { |
| 44 | + break; |
| 45 | + } else { |
| 46 | + const message = match[1].trim(); |
| 47 | + if (existingMessages.has(message)) { |
| 48 | + // This probably means you ran the script twice. |
| 49 | + continue; |
| 50 | + } |
| 51 | + existingMessages.add(message); |
75 | 52 |
|
76 |
| - // error messages can be concatenated (`+`) at runtime, so here's a |
77 |
| - // trivial partial evaluator that interprets the literal value |
78 |
| - const errorMsgLiteral = evalStringConcat(node.arguments[1]); |
79 |
| - addToErrorMap(errorMsgLiteral); |
80 |
| - } |
81 |
| - }, |
82 |
| - }, |
83 |
| - }); |
84 |
| - } |
85 |
| - |
86 |
| - function addToErrorMap(errorMsgLiteral) { |
87 |
| - if (existingErrorMap.hasOwnProperty(errorMsgLiteral)) { |
88 |
| - return; |
| 53 | + // Add to json map |
| 54 | + if (newJSON === null) { |
| 55 | + newJSON = Object.assign({}, originalJSON); |
| 56 | + } |
| 57 | + console.log(`"${nextCode}": "${message}"`); |
| 58 | + newJSON[nextCode] = message; |
| 59 | + nextCode += 1; |
89 | 60 | }
|
90 |
| - existingErrorMap[errorMsgLiteral] = '' + currentID++; |
91 |
| - } |
| 61 | + } while (true); |
92 | 62 |
|
93 |
| - function flush(cb) { |
| 63 | + if (newJSON) { |
94 | 64 | fs.writeFileSync(
|
95 |
| - errorMapFilePath, |
96 |
| - JSON.stringify(invertObject(existingErrorMap), null, 2) + '\n', |
97 |
| - 'utf-8' |
| 65 | + path.resolve(__dirname, '../error-codes/codes.json'), |
| 66 | + JSON.stringify(newJSON, null, 2) |
98 | 67 | );
|
99 | 68 | }
|
| 69 | +} |
100 | 70 |
|
101 |
| - return function extractErrors(source) { |
102 |
| - transform(source); |
103 |
| - flush(); |
104 |
| - }; |
105 |
| -}; |
| 71 | +main().catch(error => { |
| 72 | + console.error(error); |
| 73 | + process.exit(1); |
| 74 | +}); |
0 commit comments