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
TypeScript Constant contexts #9534
TypeScript Constant contexts #9534
Conversation
tanhauhau
commented
Feb 18, 2019
Q | A |
---|---|
Fixed Issues? | Fixes #9502 |
Patch: Bug Fix? | |
Major: Breaking Change? | |
Minor: New Feature? | |
Tests Added + Pass? | Yes |
Documentation PR Link | |
Any Dependency Changes? | |
License | MIT |
packages/babel-parser/test/fixtures/typescript/cast/as-const-2/input.js
Outdated
Show resolved
Hide resolved
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10453/ |
Looks very promising, let us know when you are done. |
@danez it's ready for code review 😄 |
case "UnaryExpression": | ||
return this.tsCheckLiteralForConstantContext(node.argument); | ||
default: | ||
throw this.unexpected(node.start); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can throw better error messages 😉
throw this.unexpected(node.start); | |
this.raise(node.start, "Only literal values are allowed in constant contexts"); |
case "NumericLiteral": | ||
case "BooleanLiteral": | ||
case "ObjectExpression": | ||
case "ArrayExpression": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For arrays and objects, don't we need to check their children?
e.g.
[ foo() ] as const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated and added new test case
// but need `tsInType` to satisfy the assertion in `tsParseType`. | ||
node.typeAnnotation = this.tsInType(() => this.tsParseType()); | ||
const _const = this.tsTryNextParseConstantContext(); | ||
node.typeAnnotation = _const ? _const : this.tsNextThenParseType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node.typeAnnotation = _const ? _const : this.tsNextThenParseType(); | |
node.typeAnnotation = _const || this.tsNextThenParseType(); |
cc8eb28
to
76f1c61
Compare
let o5 = { ...o4 } as const; | ||
let o6 = { ...o5 }; | ||
let o7 = { ...d } as const; | ||
let o8 = { ...o7 }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are these tests without as const
needed for? I think that you copied them from https://github.com/Microsoft/TypeScript/blob/08fe06f5275bae91a2b2d7eb6387218b95ce3566/tests/baselines/reference/constAssertions.js, but since Babel doesn't type-check they aren't needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the test cases
Some tests are failing on travis/circle because of regenerator, but they will work if you rebase this PR on master. |
76f1c61
to
735d539
Compare
@nicolo-ribaudo cool. |
You also need to update the output 😛 |
3acf446
to
fcb060f
Compare