Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port babel-parser changes from 2020-04-12 to 2020-07-22 #556

Merged
merged 8 commits into from Oct 13, 2020

Conversation

Rugvip
Copy link
Contributor

@Rugvip Rugvip commented Sep 23, 2020

a466f 5 months ago fix: report missing plugins on type exports (#11417)
🚫 Looks like this is already covered by the existing isTS and isFlow cases

fba64 5 months ago fix: disallow expression after binding identifier of (#11355)
🚫 Sucrase doesn't use token contexts.

40c51 5 months ago Set exprAllowed to false for star token (#11449)
🚫 Sucrase doesn't use token contexts.

2e4f18 5 months ago Add some parser missing plugins errors (#11478)
🚫 Validation related to babel plugins only.

fa98a 5 months ago docs: update AST spec (#11492)
🚫 Docs update

9c284 5 months ago (tag: v7.9.6) v7.9.6
🚫 Release only

90a91 5 months ago Update Flow to 0.123.0 (#11500)
🚫 Flow version bump with one change to the tokenizer that isn't present in sucrase

31b36 5 months ago Use ?. where it represents the intended semantics (#11512)
🚫 Refactor to make use of ?. in the all of babel, separate effort in sucrase if needed

2f31e 4 months ago fix: allow bigInt in method name and TSLiteralType (#11547)
βœ… Skipped refactor of isLiteralPropertyName in utils, but ported bigint type literal support + added test

62e68 4 months ago Fix comments for smartPipeline topic-forbidding contexts (#11597)
🚫 Comment update related to contexts

5dd7f 4 months ago Enable import.meta by default in @babel/parser (#11406)
🚫 Import meta is already always parsed

66b86 4 months ago added basic support for module attributes and tests updated (#10962)
🚫 Skipped as this is the implementation of a stage 1 proposal - the proposal is now stage 3 though, but is perhaps a separate effort? https://github.com/tc39/proposal-import-assertions

74590 4 months ago Add private-property-in-object support (#11372)
βœ… Ported

bda75 4 months ago Handle private access chained on an optional chain (#11248)
βœ… Ported

5da24 4 months ago (tag: v7.10.0) v7.10.0
🚫 Release only

242d9 4 months ago Use repository.directory field in package.json files (#11625)
🚫 Just babel package.json stuff

88f57 4 months ago (tag: v7.10.1) v7.10.1
🚫 Release only

b5c4a 4 months ago refactor: split locationParser into ParserErrors and error message (#11653)
🚫 Skipped, error message related refactoring

b0350 4 months ago (tag: v7.10.2) v7.10.2
🚫 Release only

71d352 4 months ago Properly parse export default from when exportDefaultFrom is not enabled (#11676)
βœ… Ported the change, but was likely working already.

41085 4 months ago Update prettier to v2 (#11579)
🚫 Skipped, formatting change

b27ab 3 months ago fix: add optional: false to MemberExpression (#11709)
🚫 estree not supported.

e15a5 3 months ago Fix innercomments (#11697)
🚫 Sucrase skips comments

eea15 3 months ago Migrate from "master" branch to "main" (#11715)
🚫 Branch rename

2787e 3 months ago (tag: v7.10.3) v7.10.3
🚫 Release only

30835 3 months ago fix: implement early errors for record and tuple (#11652)
🚫 Error handling only

beca7 3 months ago Add better parser error when using jsx (#11722)
🚫 Error handling only

75c23 3 months ago Add @babel/eslint-plugin-development-internal (#11376)
🚫 Linter changes only

7fd40d 3 months ago (tag: v7.10.4) v7.10.4
🚫 Release only

b1b21 3 months ago docs: add AST spec on optional chain [skip ci] (#11729)
🚫 Documentation change only

d6762 3 months ago fix: throw expect jsx plugin error when an idStart or > is seen (#11774)
🚫 Error handling only

02c8f 3 months ago fix: add optional: false to chained optional call expression (#11814)
❓ Sets node.optional, assuming this can be skipped since optional chaining is handled differently in Sucrase?

f7964 2 months ago (tag: v7.10.5) v7.10.5
🚫 Release only

8f191 10 weeks ago chore: fix typo in codebase (#11846)
🚫 These typos aren't present in Sucrase

f4eeff 10 weeks ago fix: correctly set innerEndPos in CoverParenthesizedExpressionAndArrowParameterList (#11847)
🚫 Fix for cone that's not present in Sucrase

3680f 9 weeks ago fix: allow 09.1_1 and 09e1_1 in sloppy mode (#11854)
🚫 Sucrase doesn't have separate handling of octals so this was not needed

2bf38 9 weeks ago fix: disallow \8, \9 in strict mode string (#11852)
🚫 Error handling only

059e91 8 weeks ago Add decimal parsing support (#11640)
βœ… Ported

d7347f 8 weeks ago eslint-parser: ES2020 features (#11815)
🚫 estree not supported.

5b4b3 3 months ago TypeScript 4.0: Allow spread in the middle of tuples (#11753)
βœ… Added tests, but the change did not need to be ported

8a1d7e4 3 months ago Allow unknown/any in TS catch clause param (#11755)
βœ… Ported + added TypeScript tests, only change needed to support catch clause params

9e666 3 months ago Follow-up on initial TS4 catch param support (#11767)
🚫 Skipped, this detail is not included in the TypeScript parser in sucrase

eba4c 2 months ago TypeScript 4.0: Support labeled tuple elements (#11754)
βœ… Ported, excluding validation logic

0e985 9 weeks ago feat: enable numericSeparator parsing support (#11863)
🚫 Skipped, already supported

b651a 9 weeks ago Enable logical assignment by default in @babel/parser (#11860) (#11869)
🚫 Skipped, already supported

Copy link
Owner

@alangpierce alangpierce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks! The one that you ported looks good. For the three where you had questions, I think the private fields ones should be ported as well so that the parser stays up-to-date with future JS.

66b86 4 months ago added basic support for module attributes and tests updated (#10962)
❓ Skipped as this is the implementation of a stage 1 proposal - the proposal is now stage 3 though, but is perhaps a separate effort? https://github.com/tc39/proposal-import-assertions

Makes sense to skip given that the syntax has changed since that implementation. Whenever babel-parser implements the new assert syntax, the port of that commit would be a good opportunity to implement the syntax in Sucrase.

74590 4 months ago Add private-property-in-object support (#11372)
❓ Skipped this one as I didn't quite understand the aim of the existing private fields support - they seem to be removed completely?

The current state of private class fields isn't great right now. Theoretically, they're "passed through" so that they're supported as long as the runtime supports them, but the class fields transform removes them, which basically causes any uses to break. I think ideally this issue would be either fixed or documented.

Longer-term (or maybe actually pretty soon), I think Sucrase should have a semver-major release to drop the class fields transform since it's fairly well-supported by node and browsers at this point. At that point, supporting private class fields is just a matter of passing it through to the compiled JS.

So I think we should actually port this change, with the goal of just passing through the tokens without crashing. From the parser perspective, it should properly emit the token without breaking the parser state.

bda75 4 months ago Handle private access chained on an optional chain (#11248)
❓ Skipped for the same reason as 74590 above

Like above, I think we should port this change so that it works in the parser, but not worry about the runtime implementation. In the future, when Sucrase removes the class fields and optional chaining transforms, it should be able to parse and pass through the syntax so that the underlying JS runtime can evaluate it.

@Rugvip
Copy link
Contributor Author

Rugvip commented Sep 28, 2020

@alangpierce Thanks! Makes sense to forward the private field tokens, will have a look. Would that just be a tt.hash followed by an identifier? I ran into some trouble with # myVar being treated the same as #myVar, but is it actually in the scope of sucrase to validate that?

@alangpierce
Copy link
Owner

Would that just be a tt.hash followed by an identifier?

Yep! A reasonable alternative is a separate privateName token type, but when in doubt, it's best to stick with the tokens that Babel uses.

I ran into some trouble with # myVar being treated the same as #myVar, but is it actually in the scope of sucrase to validate that?

I'd call it out of scope, yes. I looked at the Babel source code to confirm what it does, and it parses a # token followed by a name token potentially with a space in between, and it then checks if there's a space and throws an error if so. Generally if Babel has specific error-checking code like that, Sucrase leaves it out, which means that sucrase will emit the space and the next downstream tool (which may be webpack or may be the JS engine itself) will give a hopefully-helpful error about the space. A lot of complexity in the Babel parser is these validation checks that would have been caught downstream anyway, so I decided that Sucrase should skip all checks like that, since Sucrase's parser tries to be as simple and fast as possible. It would be harder if the space was valid syntax and parsed to something different; in that case Sucrase would need to implement it.

@Rugvip
Copy link
Contributor Author

Rugvip commented Oct 4, 2020

@alangpierce Updated to include support for #x in obj and this?.#x as discussed

@@ -218,6 +218,12 @@ export function lookaheadTypeAndKeyword(): TypeAndKeyword {
return new TypeAndKeyword(type, contextualKeyword);
}

// Returns true if the next character is a valid start for an identifier
export function nextIsIdentifier(): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted for this instead of accessing input in the expression traverser, which is essentially what babel does. Should be simple to switch around if you prefer something different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, ended up syncing more from babel-parser, which moves over to accessing input in the traverser

@codecov-commenter
Copy link

codecov-commenter commented Oct 4, 2020

Codecov Report

Merging #556 into master will decrease coverage by 0.03%.
The diff coverage is 81.48%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #556      +/-   ##
==========================================
- Coverage   81.64%   81.60%   -0.04%     
==========================================
  Files          55       55              
  Lines        5503     5758     +255     
  Branches     1299     1303       +4     
==========================================
+ Hits         4493     4699     +206     
- Misses        721      768      +47     
- Partials      289      291       +2     
Impacted Files Coverage Ξ”
src/parser/plugins/typescript.ts 80.87% <ΓΈ> (ΓΈ)
src/parser/traverser/statement.ts 82.46% <50.00%> (-1.58%) ⬇️
src/parser/tokenizer/index.ts 79.18% <100.00%> (+0.38%) ⬆️
src/parser/traverser/expression.ts 88.00% <100.00%> (+0.21%) ⬆️
src/parser/util/whitespace.ts 100.00% <100.00%> (ΓΈ)
src/util/formatTokens.ts 70.58% <0.00%> (-10.06%) ⬇️
src/parser/util/identifier.ts 90.90% <0.00%> (-4.10%) ⬇️
src/util/shouldElideDefaultExport.ts 77.77% <0.00%> (-3.48%) ⬇️
src/util/getClassInfo.ts 85.24% <0.00%> (-2.05%) ⬇️
src/parser/plugins/jsx/index.ts 91.33% <0.00%> (-1.48%) ⬇️
... and 24 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Ξ” = absolute <relative> (impact), ΓΈ = not affected, ? = missing data
Powered by Codecov. Last update b3ce7fd...95c2c41. Read the comment docs.

@Rugvip Rugvip changed the title Port babel-parser changes from 2020-04-12 to 2020-05-30 Port babel-parser changes from 2020-04-12 to 2020-07-16 Oct 4, 2020
@Rugvip Rugvip changed the title Port babel-parser changes from 2020-04-12 to 2020-07-16 Port babel-parser changes from 2020-04-12 to 2020-07-22 Oct 5, 2020
@Rugvip
Copy link
Contributor Author

Rugvip commented Oct 5, 2020

@alangpierce Added a couple more commits, should hopefully now have all that we need in the parser to support TS4.

Copy link
Owner

@alangpierce alangpierce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘ really great work, thank you! I have a few nitpicks, but none particularly important, so I'll merge as-is, then do a release.

02c8f 3 months ago fix: add optional: false to chained optional call expression (#11814)
❓ Sets node.optional, assuming this can be skipped since optional chaining is handled differently in Sucrase?

Yep, that sounds right to me! It was just an issue of AST format (optional missing vs explicitly false).

@@ -408,6 +408,7 @@ export function parseExprAtom(): boolean {
case tt.regexp:
case tt.num:
case tt.bigint:
case tt.decimal:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this doesn't need to be kept in sync since it's just a benchmark. But either way is fine.

++state.pos;
isBigInt = true;
} else if (nextChar === charCodes.lowercaseM) {
unexpected("Invalid decimal", start);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer to leave this validation off for the sake of simplicity.

return pos + skip![0].length;
}

export function lookaheadCharCode(): number {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this makes sense to me! Both usages do feel a little ugly, but I guess that's to be expected for some parser cases.

One thought: it would be nice to avoid the skipWhiteSpace regex, at least in the long run. A while back, I did a lot of work to make Sucrase more compatible with AssemblyScript, and it didn't quite get there, but generally high-powered JS features like regexes aren't going to work nicely in WebAssembly. Rather than a regex, I'd prefer just a plain function that walks the string indices until it finds non-whitespace. I think that would execute faster as well, though I'm not 100% sure. (Not that this is a common code path anyway.) I'll merge this as-is, since it's not really a big deal, but might be nice to rework it a little in the future.

@@ -367,6 +367,7 @@ function tsParseMappedType(): void {
function tsParseTupleType(): void {
expect(tt.bracketL);
while (!eat(tt.bracketR) && !state.error) {
// Do not validate presence of either none or only labeled elements
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't seem super-necessary; across the parser code, all validation is skipped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put it here because it replaces a large chunk of code in babel, but agree that it's pretty useless outside the context of porting babel changes

@alangpierce alangpierce merged commit c076f15 into alangpierce:master Oct 13, 2020
@Rugvip Rugvip deleted the rugvip/ts4 branch October 13, 2020 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants