From bd5714ea0c099cea2c3122ab01358e7c231caca9 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sun, 20 Sep 2020 23:18:39 -0400 Subject: [PATCH 1/4] license: add FB License header to all extractErrors code - this plugin is largely borrowed from the React monorepo, consistently add FB's License headers to each file, not just to extractErrors.ts - also add comments pointing to the exact file and commit that each file was copied from - most of these are slightly out of date now: - evalToString is missing a line - extractErrors needs to be updated to Babel 7 - babylon -> @babel/parser, babel-traverse -> @babel/traverse - transformErrorMessages has several new constructs added :/... - keeping a pseudo-fork up-to-date is pretty tedious and not very maintainble (it's also broken per the tests I've added) - would be better if we could ask FB to split out theirs as a separate package... --- src/errors/evalToString.ts | 8 ++++++++ src/errors/extractErrors.ts | 1 + src/errors/invertObject.ts | 9 +++++++++ src/errors/transformErrorMessages.ts | 8 ++++++++ 4 files changed, 26 insertions(+) diff --git a/src/errors/evalToString.ts b/src/errors/evalToString.ts index da9e68192..c55b3b879 100644 --- a/src/errors/evalToString.ts +++ b/src/errors/evalToString.ts @@ -1,3 +1,11 @@ +// largely borrowed from https://github.com/facebook/react/blob/8b2d3783e58d1acea53428a10d2035a8399060fe/scripts/shared/evalToString.js +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + export function evalToString(ast: any): string { switch (ast.type) { case 'StringLiteral': diff --git a/src/errors/extractErrors.ts b/src/errors/extractErrors.ts index 815cd18f0..d54db908f 100644 --- a/src/errors/extractErrors.ts +++ b/src/errors/extractErrors.ts @@ -1,3 +1,4 @@ +// largely borrowed from https://github.com/facebook/react/blob/8b2d3783e58d1acea53428a10d2035a8399060fe/scripts/error-codes/extract-errors.js /** * Copyright (c) Facebook, Inc. and its affiliates. * diff --git a/src/errors/invertObject.ts b/src/errors/invertObject.ts index 23fd54f53..19dc7e5cd 100644 --- a/src/errors/invertObject.ts +++ b/src/errors/invertObject.ts @@ -1,3 +1,12 @@ +// largely borrowed from https://github.com/facebook/react/blob/8b2d3783e58d1acea53428a10d2035a8399060fe/scripts/error-codes/invertObject.js + +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + /** * turns * { 'MUCH ERROR': '0', 'SUCH WRONG': '1' } diff --git a/src/errors/transformErrorMessages.ts b/src/errors/transformErrorMessages.ts index 08d144234..acea7088a 100644 --- a/src/errors/transformErrorMessages.ts +++ b/src/errors/transformErrorMessages.ts @@ -1,3 +1,11 @@ +// largely borrowed from https://github.com/facebook/react/blob/2c8832075b05009bd261df02171bf9888ac76350/scripts/error-codes/transform-error-messages.js +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + import fs from 'fs'; import { invertObject } from './invertObject'; import { evalToString } from './evalToString'; From 3d5c8586b57ec399adc5f71c85f65c345dd7e70e Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sun, 20 Sep 2020 23:27:28 -0400 Subject: [PATCH 2/4] clean: bad whitespace and Flow types in extractErrors - whitespace errors in extractErrors - unnecessary Flow comments in invertObject when we're using TS - also inconsistent, it looks like they've been removed and replaced with TS elsewhere - here TS types were added but Flow was also left in --- src/errors/extractErrors.ts | 2 +- src/errors/invertObject.ts | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/errors/extractErrors.ts b/src/errors/extractErrors.ts index d54db908f..15b41ffa2 100644 --- a/src/errors/extractErrors.ts +++ b/src/errors/extractErrors.ts @@ -112,7 +112,7 @@ function ErrorDev(message) { return error; } -export default ErrorDev; +export default ErrorDev; `, 'utf-8' ); diff --git a/src/errors/invertObject.ts b/src/errors/invertObject.ts index 19dc7e5cd..13959e9e9 100644 --- a/src/errors/invertObject.ts +++ b/src/errors/invertObject.ts @@ -16,9 +16,7 @@ type Dict = { [key: string]: any }; -export function invertObject( - targetObj: Dict /* : ErrorMap */ -) /* : ErrorMap */ { +export function invertObject(targetObj: Dict) { const result: Dict = {}; const mapKeys = Object.keys(targetObj); From a3b450d53187d857f496bd4863330fb7a7a884c6 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sun, 20 Sep 2020 23:32:53 -0400 Subject: [PATCH 3/4] deps: update extractErrors Babel plugins to Babel 7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - this was using Babel 6's Babylon and babel-traverse, which meant two different versions of Babel were being used and a lot of unnecessary deps were in the tree - now there should no longer be any Babel 6 deps in the tree - also transformErrorMessages was already using Babel 7 (`@babel/helper-module-imports`) while extractErrors was on Babel 6 just for extra inconsistency ๐Ÿ˜ฌ - these two files seemed to be updated at the same time to Babel 7 as far as I can tell though ๐Ÿคจ - `@babel/parser` ships its own typings now, so no need to use the `declare module` workaround for it anymore - but did have to add a type-cast as its usage doesn't entirely fit the type ๐Ÿ˜• - this also helps remove an old version of core-js v2, the addition of which would give a deprecation warning on install: - "npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3." --- package.json | 4 +- src/env.d.ts | 3 +- src/errors/extractErrors.ts | 12 +++--- yarn.lock | 81 +------------------------------------ 4 files changed, 10 insertions(+), 90 deletions(-) diff --git a/package.json b/package.json index 4e31bbcaa..0a70dbc7c 100644 --- a/package.json +++ b/package.json @@ -43,8 +43,10 @@ "dependencies": { "@babel/core": "^7.4.4", "@babel/helper-module-imports": "^7.0.0", + "@babel/parser": "^7.11.5", "@babel/plugin-proposal-class-properties": "^7.4.4", "@babel/preset-env": "^7.11.0", + "@babel/traverse": "^7.11.5", "@rollup/plugin-babel": "^5.1.0", "@rollup/plugin-commonjs": "^11.0.0", "@rollup/plugin-json": "^4.0.0", @@ -61,8 +63,6 @@ "babel-plugin-macros": "^2.6.1", "babel-plugin-polyfill-regenerator": "^0.0.4", "babel-plugin-transform-rename-import": "^2.3.0", - "babel-traverse": "^6.26.0", - "babylon": "^6.18.0", "camelcase": "^6.0.0", "chalk": "^4.0.0", "enquirer": "^2.3.4", diff --git a/src/env.d.ts b/src/env.d.ts index fe32705b6..9cd3924d8 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -12,8 +12,7 @@ declare module '@babel/core' { // Rollup plugins declare module 'rollup-plugin-terser'; -declare module 'babel-traverse'; -declare module 'babylon'; +declare module '@babel/traverse'; declare module '@babel/helper-module-imports'; declare module 'lodash.merge'; diff --git a/src/errors/extractErrors.ts b/src/errors/extractErrors.ts index 15b41ffa2..9e6ca168e 100644 --- a/src/errors/extractErrors.ts +++ b/src/errors/extractErrors.ts @@ -6,17 +6,17 @@ * LICENSE file in the root directory of this source tree. */ import fs from 'fs-extra'; -import * as babylon from 'babylon'; -import traverse from 'babel-traverse'; +import { parse, ParserOptions } from '@babel/parser'; +import traverse from '@babel/traverse'; import { invertObject } from './invertObject'; import { evalToString } from './evalToString'; import { paths } from '../constants'; import { safeVariableName } from '../utils'; import { pascalCase } from 'pascal-case'; -const babylonOptions = { +const babelParserOptions: ParserOptions = { sourceType: 'module', - // As a parser, babylon has its own options and we can't directly + // As a parser, @babel/parser has its own options and we can't directly // import/require a babel preset. It should be kept **the same** as // the `babel-plugin-syntax-*` ones specified in // https://github.com/facebook/fbjs/blob/master/packages/babel-preset-fbjs/configure.js @@ -27,7 +27,7 @@ const babylonOptions = { 'trailingFunctionCommas', 'objectRestSpread', ], -}; +} as ParserOptions; // workaround for trailingFunctionCommas syntax export async function extractErrors(opts: any) { if (!opts || !('errorMapFilePath' in opts)) { @@ -65,7 +65,7 @@ export async function extractErrors(opts: any) { existingErrorMap = invertObject(existingErrorMap); function transform(source: string) { - const ast = babylon.parse(source, babylonOptions); + const ast = parse(source, babelParserOptions); traverse(ast, { CallExpression: { diff --git a/yarn.lock b/yarn.lock index 227cb54d1..1c1671c73 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1808,15 +1808,6 @@ axobject-query@^2.0.2: "@babel/runtime" "^7.7.4" "@babel/runtime-corejs3" "^7.7.4" -babel-code-frame@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" - integrity sha1-Y/1D99weO7fONZR9uP42mj9Yx0s= - dependencies: - chalk "^1.1.3" - esutils "^2.0.2" - js-tokens "^3.0.2" - babel-eslint@^10.0.3: version "10.0.3" resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-10.0.3.tgz#81a2c669be0f205e19462fed2482d33e4687a88a" @@ -1842,13 +1833,6 @@ babel-jest@^25.3.0: chalk "^3.0.0" slash "^3.0.0" -babel-messages@^6.23.0: - version "6.23.0" - resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e" - integrity sha1-8830cDhYA1sqKVHG7F7fbGLyYw4= - dependencies: - babel-runtime "^6.22.0" - babel-plugin-annotate-pure-calls@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/babel-plugin-annotate-pure-calls/-/babel-plugin-annotate-pure-calls-0.4.0.tgz#78aa00fd878c4fcde4d49f3da397fcf5defbcce8" @@ -1949,44 +1933,6 @@ babel-preset-jest@^25.3.0: babel-plugin-jest-hoist "^25.2.6" babel-preset-current-node-syntax "^0.1.2" -babel-runtime@^6.22.0, babel-runtime@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" - integrity sha1-llxwWGaOgrVde/4E/yM3vItWR/4= - dependencies: - core-js "^2.4.0" - regenerator-runtime "^0.11.0" - -babel-traverse@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee" - integrity sha1-RqnL1+3MYsjlwGTi0tjQ9ANXZu4= - dependencies: - babel-code-frame "^6.26.0" - babel-messages "^6.23.0" - babel-runtime "^6.26.0" - babel-types "^6.26.0" - babylon "^6.18.0" - debug "^2.6.8" - globals "^9.18.0" - invariant "^2.2.2" - lodash "^4.17.4" - -babel-types@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497" - integrity sha1-o7Bz+Uq0nrb6Vc1lInozQ4BjJJc= - dependencies: - babel-runtime "^6.26.0" - esutils "^2.0.2" - lodash "^4.17.4" - to-fast-properties "^1.0.3" - -babylon@^6.18.0: - version "6.18.0" - resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3" - integrity sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ== - bail@^1.0.0: version "1.0.4" resolved "https://registry.yarnpkg.com/bail/-/bail-1.0.4.tgz#7181b66d508aa3055d3f6c13f0a0c720641dde9b" @@ -2559,11 +2505,6 @@ core-js-pure@^3.0.0: resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.4.7.tgz#c998e1892da9949200c7452cbd33c0df95be9f54" integrity sha512-Am3uRS8WCdTFA3lP7LtKR0PxgqYzjAMGKXaZKSNSC/8sqU0Wfq8R/YzoRs2rqtOVEunfgH+0q3O0BKOg0AvjPw== -core-js@^2.4.0: - version "2.6.10" - resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.10.tgz#8a5b8391f8cc7013da703411ce5b585706300d7f" - integrity sha512-I39t74+4t+zau64EN1fE5v2W31Adtc/REhzWN+gWRRXg6WH5qAsZm62DHpQ1+Yhe4047T55jvzz7MUqF/dBBlA== - core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -3818,11 +3759,6 @@ globals@^12.1.0: dependencies: type-fest "^0.8.1" -globals@^9.18.0: - version "9.18.0" - resolved "https://registry.yarnpkg.com/globals/-/globals-9.18.0.tgz#aa3896b3e69b487f17e31ed2143d69a8e30c2d8a" - integrity sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ== - globalyzer@^0.1.0: version "0.1.4" resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.4.tgz#bc8e273afe1ac7c24eea8def5b802340c5cc534f" @@ -5056,11 +4992,6 @@ jpjs@^1.2.1: resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ== -js-tokens@^3.0.2: - version "3.0.2" - resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" - integrity sha1-mGbfOVECEw449/mWvOtlRDIJwls= - js-yaml@^3.13.1: version "3.13.1" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847" @@ -5402,7 +5333,7 @@ lodash.zip@^4.2.0: resolved "https://registry.yarnpkg.com/lodash.zip/-/lodash.zip-4.2.0.tgz#ec6662e4896408ed4ab6c542a3990b72cc080020" integrity sha1-7GZi5IlkCO1KtsVCo5kLcswIACA= -lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.4: +lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19: version "4.17.20" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA== @@ -6967,11 +6898,6 @@ regenerate@^1.4.0: resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11" integrity sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg== -regenerator-runtime@^0.11.0: - version "0.11.1" - resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9" - integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg== - regenerator-runtime@^0.13.2, regenerator-runtime@^0.13.4, regenerator-runtime@^0.13.7: version "0.13.7" resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz#cac2dacc8a1ea675feaabaeb8ae833898ae46f55" @@ -8033,11 +7959,6 @@ tmpl@1.0.x: resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1" integrity sha1-I2QN17QtAEM5ERQIIOXPRA5SHdE= -to-fast-properties@^1.0.3: - version "1.0.3" - resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47" - integrity sha1-uDVx+k2MJbguIxsG46MFXeTKGkc= - to-fast-properties@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e" From f3628f0edaedb21993bcd6293a933906ba51c777 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sun, 20 Sep 2020 23:36:36 -0400 Subject: [PATCH 4/4] deps: update extractErrors plugin's evalToString file - a line was added upstream in the last year, so add it here too to be up-to-date - now only transformErrorMessages is out-of-date ...but there's a little too many changes for me to really prioritize right now --- src/errors/evalToString.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/errors/evalToString.ts b/src/errors/evalToString.ts index c55b3b879..fb819ae26 100644 --- a/src/errors/evalToString.ts +++ b/src/errors/evalToString.ts @@ -9,6 +9,7 @@ export function evalToString(ast: any): string { switch (ast.type) { case 'StringLiteral': + case 'Literal': // ESLint return ast.value; case 'BinaryExpression': // `+` if (ast.operator !== '+') {