Skip to content
New issue

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

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

Already on GitHub? # to your account

Support the override keyword from TypeScript 4.3. #630

Closed
wants to merge 1 commit into from

Conversation

lgarron
Copy link

@lgarron lgarron commented Jun 27, 2021

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-3.html#override-and-the---noimplicitoverride-flag

This implementation is modeled on the protected keyword, which is similarly stripped from TypeScript function declarations.

Fixes #629 and addresses preactjs/wmr#714

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-3.html#override-and-the---noimplicitoverride-flag

This implementation is modeled on the `protected` keyword, which is similarly stripped from TypeScript function declarations.

Fixes #629 and addresses preactjs/wmr#714
@codecov
Copy link

codecov bot commented Jun 28, 2021

Codecov Report

Merging #630 (02f8531) into main (291ccc5) will decrease coverage by 0.10%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #630      +/-   ##
==========================================
- Coverage   82.23%   82.12%   -0.11%     
==========================================
  Files          56       56              
  Lines        5883     5651     -232     
  Branches     1330     1330              
==========================================
- Hits         4838     4641     -197     
+ Misses        762      727      -35     
  Partials      283      283              
Impacted Files Coverage Δ
src/parser/plugins/typescript.ts 82.32% <0.00%> (-0.23%) ⬇️
src/parser/tokenizer/readWordTree.ts 100.00% <ø> (ø)
src/transformers/TypeScriptTransformer.ts 95.55% <ø> (ø)
src/util/getClassInfo.ts 89.70% <ø> (+1.84%) ⬆️
src/util/isIdentifier.ts 100.00% <ø> (ø)
src/parser/tokenizer/types.ts 57.87% <83.33%> (-0.07%) ⬇️
src/parser/tokenizer/keywords.ts 100.00% <100.00%> (ø)
src/Options-gen-types.ts 75.00% <0.00%> (-25.00%) ⬇️
src/parser/index.ts 77.77% <0.00%> (-7.94%) ⬇️
src/computeSourceMap.ts 83.33% <0.00%> (-5.56%) ⬇️
... and 26 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 291ccc5...02f8531. Read the comment docs.

@github-actions
Copy link

Benchmark results

Before this PR: 247.3 thousand lines per second
After this PR: 246.6 thousand lines per second

Measured change: 0.3% slower (2.49% slower to 3.07% faster)
Summary: Likely no significant difference

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.

Hi @lgarron , thanks for the contribution! Unfortunately, I tested it out and am getting a parse error when I try it even on basic cases, so looks like it's not quite working end-to-end.

Usually the way that new syntax gets added to Sucrase is by porting babel-parser changes like in this PR: #567 . Sucrase's parser was originally forked from Babel's parser, so usually the Babel implementation is a good code sketch of how to fix it in Sucrase, though sometimes the code has diverged enough that it's easier to just write the change from scratch. I try to comb through all changes and port relevant ones from time to time, though I'm certainly also open to individual high-priority changes like the override keyword here. I looked through the Babel changelog and looks like the override implementation is here: babel/babel#13097 . I think the typescript/index.js changes are the most useful there, particularly updating calls to tsParseModifiers. Note that any error checking (e.g. this.raise) is out of scope since Sucrase doesn't try to be an error checker.

It would also be good to see some tests for this PR, which can go in test/typescript-test.ts. Feel free to use the Babel tests for inspiration, or write your own code snippets. Just testing some basic cases should be fine.

@@ -46,6 +46,7 @@ const RESERVED_WORDS = new Set([
"implements",
"interface",
"let",
"override",
Copy link
Owner

Choose a reason for hiding this comment

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

override is actually not a reserved word in JS or TS, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords . It's a "contextual keyword", so it can be used as a variable name, but also has meaning in certain contexts. So it actually shouldn't be added to this file.

@@ -164,6 +164,7 @@ const types = {
_public: new KeywordTokenType("public"),
_private: new KeywordTokenType("private"),
_protected: new KeywordTokenType("protected"),
_override: new KeywordTokenType("override"),
Copy link
Owner

Choose a reason for hiding this comment

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

I was going to say that it's weird there to be both a regular keyword and a contextual keyword for override, and ideally it would be only a contextual keyword because it's not a true language keyword. But I guess other modifiers use the same strategy (including non-keywords like readonly), so seems very reasonable to be consistent with that. FWIW, it feels fragile because the transformer won't necessarily know when to use this.tokens.matches1(tt._override) and when to use this.tokens.matchesContextual(ContextualKeyword._override), so maybe it's something that could be cleaned up in the future.

@alangpierce
Copy link
Owner

I just put up #636, which will cover this case in addition to the other TS 4.3 syntax. I'll close this PR and the corresponding task when that one is merged and released.

@alangpierce
Copy link
Owner

I just released Sucrase 3.20.0 that has support for override via #636, which is partly based on this change. Thanks!

@alangpierce alangpierce closed this Jul 7, 2021
@lgarron lgarron deleted the override branch July 7, 2021 08:03
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for override token from TS 4.3
2 participants