From dd0040251c41fd915c6c5ef10897bf54e677b50a Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Sun, 12 Apr 2020 17:54:16 -0700 Subject: [PATCH] Port babel-parser changes from 2019-12-22 to 2020-04-12 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a18166d2a refactor: remove inClassProperty parser state (#10906) 🚫 Already removed from parser state. 197a8da04 Remove unused parser methods (#10942) 🚫 Already removed. 86245a83a TSTypeCastExpression should not be inside call parameters (#10939) 🚫 Validation only. 30449fe05 Refactor parseSubscript (#10937) 🚫 Skipped the refactor since Sucrase's subscriptStartIndex seems to work nicer when the cases are handled independently. 2f3f77926 refactor: remove unused invalidTemplateEscapePosition tokenizer state (#10935) 🚫 Already removed. 023824426 refactor: remove unecessary checkYieldAwaitInDefaultParams (#10936) 🚫 Already removed. 26eb89187 fix: Class Field Initializer should not allow await expression as immediate child (#10946) 🚫 Scope code not relevant for Sucrase. 6ee8c97e6 Fix: TopLevelAwait should respect await identifiers defined in… (#10947) 🚫 Validation code not relevant to Sucrase. 467667af8 When reading a new string, U+2028/2029 should correctly set th… (#10944) 🚫 Line information not maintained by Sucrase parser. a28353703 fix: check await when parsing AsyncArrowBindingIdentifier (#10953) 🚫 Validation only. 455d782ef test: add invalid-lone-import test (#10950) 🚫 Test only. 8fd532db3 LiteralPropertyName should allow BigIntLiteral (#10955) βœ… Didn't fail tests, but I added the check and a test. 771c730fd [parser] Disallow duplicate and undeclared private names (#10456) 🚫 All related scopes and validation, neither of which we handle. 5b907e9bb [parser] enable dynamic import by default (#10843) 🚫 Already enabled by default in Sucrase. c3388ea42 Enable nullish coalescing by default in @babel/parser (#10819) 🚫 Already enabled by default in Sucrase. 5f807ae45 Enable optional chaining by default in @babel/parser (#10817) 🚫 Already enabled by default in Sucrase. e8dac621d chore: specify package type (#10849) 🚫 Might be good to do for Sucrase, but outside the scope of this change. 81c5f1f22 Disallow private name in object elements and TS type elements (#10980) 🚫 Validation only. 2486615a7 v7.8.0 🚫 Release only. 8fce431d8 Revert "chore: specify package type (#10849)" (#11003) 🚫 Nothing to revert. a7620bd26 v7.8.3 🚫 Release only. 9df70b450 Duplicate `__proto__` key should be allowed in object patterns (#10987) 🚫 Validation only. a1063d258 fix: triple __proto__ in object patterns should be allowed (#11009) 🚫 Validation only. 6648d62f4 chore: use @babel/eslint-config-internal (#11026) 🚫 Lint only. 45301c530 Update coalesce precedence (#11017) βœ… Switched precedence and made sure tests still pass. It's unclear what test could confirm this in terms of Sucrase behavior. 34a965328 add AST for the module attribute proposal (#11015) 🚫 Docs only. 341964bd4 fix: properly parse member expression after property initializ… (#11031) 🚫 Code was no longer in Sucrase. facfd4d8a Enable `no-process-exit` ESLint rule (#11025) 🚫 Lint only. 9bc04baeb fix(babel-parser): avoid state.clone() to clone the whole token store (#11029) 🚫 I already implemented a very similar optimization in a different way. 43b23e086 refactor: simplify toAssignable routine (#11032) 🚫 Sucrase already removed toAssignable. 5c2e6bc07 v7.8.4 🚫 Release only. 11d9826e7 Fix Async Generic After Await Parsing Error (#11092) 🚫 Code change not relevant for Sucrase. 0b3dea8f5 Turn on no-fallthrough rule (#11093) 🚫 Lint only. 8ab27c8ff Wrap type-only class fields in flow comments (#11096) 🚫 Internal-only change. 38529699d Refactor await/yield production parameter tracking (#10956) 🚫 Seems to be only related to scopes and validation. 223cee724 Parse declare modifier around accessibility modifiers (#11146) 🚫 Issue still tracked as #486 afb0f489d Enable more eslint recommended rules (#11168) 🚫 Lint only 750d3dde3 v7.8.6 🚫 Release only 2603c2e22 fix(babel-parser): chain off optionally chained keys named cla… (#11198) 🚫 Seems to be working in Sucrase, and changed code doesn't exist anymore. 21c914187 Refactor: add parser message template (#11192) 🚫 Validation-related change. I might eventually want something like this in Sucrase, but for now, Sucrase isn't focused on error messages. 595f65f33 v7.8.7 🚫 Release only. e297e406c refactor: remove redundant contextDescription empty check (#11219) 🚫 Validation only. 2057d2b15 fix: non-directive "use strict" should not enable parsing in strict mode (#11188) 🚫 Sucrase always parses in strict mode. c831a2450 v7.8.8 🚫 Release only. 84a9ea455 Allow await when it is not in AsyncArrowHead (#11148) 🚫 Validation only. 4a4845585 feat: align ID_Start/ID_Continue regex to Unicode 13.0.0 (#11246) 🚫 We don't validate unicode in identifiers. 20d9a1018 Add estree parsing support for `export * as A` (#11254) 🚫 estree not supported. 740260b23 Add `import type` and `export type` support to TypeScript (#11171) βœ… Ported both changes, though looks like export type doesn't end up working with the transform right now. 2bce1e5e2 Parse BigInts by default (#11117) 🚫 Already parsed by default. 4f394e30d Add support for flow's SymbolTypeAnnotation (#11077) 🚫 We don't parse flow built-in types like babel does. 5c1a8210d Implement support for `declare` on class fields with Flow (#11178) 🚫 Will be done along with #486. 3ce7c2e39 Added support for record and tuple syntax. (#10865) 🚫 I won't support this syntax for now since it's experimental. 841f4428e Rephrase parser error message (#11208) 🚫 Validation only. e39b50803 Add @babel/helper-validator-identifier (#11289) 🚫 I'll keep the identifier checking code inline for now. 8d5e422be v7.9.0 🚫 Release only. 7ca814489 fix: parse value imports named type as values (#11296) 🚫 Seems to work fine in Sucrase. 2399e0df2 v7.9.2 🚫 Release only. 0e5c1da65 fix: async arrow functions should not be allowed after binary… (#11284) 🚫 Validation only. 1ae85560a v7.9.3 🚫 Release only. dc7c5640e Compact parser fixture loc info (#11322) 🚫 Test data change only. 4e6c9c52e fix: token after strict mode block is evaluated in strict mode (#11186) 🚫 Sucrase always uses strict mode. d3cf5fb5f v7.9.4 🚫 Release only. 8b976b067 fix: do not push new token context when function is following dot/questionDot (#11388) 🚫 Sucrase doesn't use token contexts. --- generator/generateTokenTypes.ts | 36 +++++++++++++++--------------- src/parser/plugins/typescript.ts | 3 +++ src/parser/tokenizer/types.ts | 36 +++++++++++++++--------------- src/parser/traverser/expression.ts | 2 +- src/parser/traverser/statement.ts | 3 +++ test/sucrase-test.ts | 12 ++++++++++ test/typescript-test.ts | 13 +++++++++++ 7 files changed, 68 insertions(+), 37 deletions(-) diff --git a/generator/generateTokenTypes.ts b/generator/generateTokenTypes.ts index a506724f..bb8562f1 100644 --- a/generator/generateTokenTypes.ts +++ b/generator/generateTokenTypes.ts @@ -87,22 +87,22 @@ const types = { tilde: new TokenType("~", {prefix}), pipeline: new BinopTokenType("|>", 0), nullishCoalescing: new BinopTokenType("??", 1), - logicalOR: new BinopTokenType("||", 2), - logicalAND: new BinopTokenType("&&", 3), - bitwiseOR: new BinopTokenType("|", 4), - bitwiseXOR: new BinopTokenType("^", 5), - bitwiseAND: new BinopTokenType("&", 6), - equality: new BinopTokenType("==/!=", 7), - lessThan: new BinopTokenType("<", 8), - greaterThan: new BinopTokenType(">", 8), - relationalOrEqual: new BinopTokenType("<=/>=", 8), - bitShift: new BinopTokenType("<>", 9), - plus: new TokenType("+", {binop: 10, prefix}), - minus: new TokenType("-", {binop: 10, prefix}), - modulo: new BinopTokenType("%", 11), - star: new BinopTokenType("*", 11), - slash: new BinopTokenType("/", 11), - exponent: new TokenType("**", {binop: 12, rightAssociative: true}), + logicalOR: new BinopTokenType("||", 1), + logicalAND: new BinopTokenType("&&", 2), + bitwiseOR: new BinopTokenType("|", 3), + bitwiseXOR: new BinopTokenType("^", 4), + bitwiseAND: new BinopTokenType("&", 5), + equality: new BinopTokenType("==/!=", 6), + lessThan: new BinopTokenType("<", 7), + greaterThan: new BinopTokenType(">", 7), + relationalOrEqual: new BinopTokenType("<=/>=", 7), + bitShift: new BinopTokenType("<>", 8), + plus: new TokenType("+", {binop: 9, prefix}), + minus: new TokenType("-", {binop: 9, prefix}), + modulo: new BinopTokenType("%", 10), + star: new BinopTokenType("*", 10), + slash: new BinopTokenType("/", 10), + exponent: new TokenType("**", {binop: 11, rightAssociative: true}), jsxName: new TokenType("jsxName"), jsxText: new TokenType("jsxText"), @@ -144,8 +144,8 @@ const types = { _null: new KeywordTokenType("null"), _true: new KeywordTokenType("true"), _false: new KeywordTokenType("false"), - _in: new KeywordTokenType("in", {binop: 8}), - _instanceof: new KeywordTokenType("instanceof", {binop: 8}), + _in: new KeywordTokenType("in", {binop: 7}), + _instanceof: new KeywordTokenType("instanceof", {binop: 7}), _typeof: new KeywordTokenType("typeof", {prefix}), _void: new KeywordTokenType("void", {prefix}), _delete: new KeywordTokenType("delete", {prefix}), diff --git a/src/parser/plugins/typescript.ts b/src/parser/plugins/typescript.ts index 4f111a6c..f98e8b07 100644 --- a/src/parser/plugins/typescript.ts +++ b/src/parser/plugins/typescript.ts @@ -1160,6 +1160,9 @@ export function tsTryParseExport(): boolean { semicolon(); return true; } else { + if (isContextual(ContextualKeyword._type) && lookaheadType() === tt.braceL) { + next(); + } return false; } } diff --git a/src/parser/tokenizer/types.ts b/src/parser/tokenizer/types.ts index 134656b2..68e0c3b3 100644 --- a/src/parser/tokenizer/types.ts +++ b/src/parser/tokenizer/types.ts @@ -47,22 +47,22 @@ export enum TokenType { tilde = 17024, // ~ prefix pipeline = 17409, // |> prec:1 nullishCoalescing = 17922, // ?? prec:2 - logicalOR = 18435, // || prec:3 - logicalAND = 18948, // && prec:4 - bitwiseOR = 19461, // | prec:5 - bitwiseXOR = 19974, // ^ prec:6 - bitwiseAND = 20487, // & prec:7 - equality = 21000, // ==/!= prec:8 - lessThan = 21513, // < prec:9 - greaterThan = 22025, // > prec:9 - relationalOrEqual = 22537, // <=/>= prec:9 - bitShift = 23050, // <> prec:10 - plus = 23691, // + prec:11 prefix - minus = 24203, // - prec:11 prefix - modulo = 24588, // % prec:12 - star = 25100, // * prec:12 - slash = 25612, // / prec:12 - exponent = 26189, // ** prec:13 rightAssociative + logicalOR = 18434, // || prec:2 + logicalAND = 18947, // && prec:3 + bitwiseOR = 19460, // | prec:4 + bitwiseXOR = 19973, // ^ prec:5 + bitwiseAND = 20486, // & prec:6 + equality = 20999, // ==/!= prec:7 + lessThan = 21512, // < prec:8 + greaterThan = 22024, // > prec:8 + relationalOrEqual = 22536, // <=/>= prec:8 + bitShift = 23049, // <> prec:9 + plus = 23690, // + prec:10 prefix + minus = 24202, // - prec:10 prefix + modulo = 24587, // % prec:11 + star = 25099, // * prec:11 + slash = 25611, // / prec:11 + exponent = 26188, // ** prec:12 rightAssociative jsxName = 26624, // jsxName jsxText = 27136, // jsxText jsxTagStart = 27648, // jsxTagStart @@ -101,8 +101,8 @@ export enum TokenType { _null = 44560, // null keyword _true = 45072, // true keyword _false = 45584, // false keyword - _in = 46105, // in prec:9 keyword - _instanceof = 46617, // instanceof prec:9 keyword + _in = 46104, // in prec:8 keyword + _instanceof = 46616, // instanceof prec:8 keyword _typeof = 47248, // typeof keyword prefix _void = 47760, // void keyword prefix _delete = 48272, // delete keyword prefix diff --git a/src/parser/traverser/expression.ts b/src/parser/traverser/expression.ts index 81d44388..debb1e9c 100644 --- a/src/parser/traverser/expression.ts +++ b/src/parser/traverser/expression.ts @@ -867,7 +867,7 @@ export function parsePropertyName(objectContextId: number): void { expect(tt.bracketR); state.tokens[state.tokens.length - 1].contextId = objectContextId; } else { - if (match(tt.num) || match(tt.string)) { + if (match(tt.num) || match(tt.string) || match(tt.bigint)) { parseExprAtom(); } else { parseMaybePrivateName(); diff --git a/src/parser/traverser/statement.ts b/src/parser/traverser/statement.ts index e4204532..7496d8b5 100644 --- a/src/parser/traverser/statement.ts +++ b/src/parser/traverser/statement.ts @@ -1036,6 +1036,9 @@ export function parseImport(): void { tsParseImportEqualsDeclaration(); return; } + if (isTypeScriptEnabled) { + eatContextual(ContextualKeyword._type); + } // import '...' if (match(tt.string)) { diff --git a/test/sucrase-test.ts b/test/sucrase-test.ts index 5003c8fd..5863f264 100644 --- a/test/sucrase-test.ts +++ b/test/sucrase-test.ts @@ -1155,4 +1155,16 @@ describe("sucrase", () => { {transforms: []}, ); }); + + it("allows bigint literals as object keys", () => { + assertResult( + ` + const o = {0n: 0}; + `, + ` + const o = {0n: 0}; + `, + {transforms: []}, + ); + }); }); diff --git a/test/typescript-test.ts b/test/typescript-test.ts index c2c4cac0..f68befb7 100644 --- a/test/typescript-test.ts +++ b/test/typescript-test.ts @@ -1961,4 +1961,17 @@ describe("typescript transform", () => { `, ); }); + + it("parses and removes import type statements", () => { + assertTypeScriptResult( + ` + import type foo from 'foo'; + console.log(foo); + `, + `"use strict"; + + console.log(foo); + `, + ); + }); });