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

TypeScript Constant contexts #9534

Merged

Conversation

@tanhauhau
Copy link
Contributor

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
@tanhauhau tanhauhau changed the title WIP: Add as const fixtures WIP: TypeScript Constant contexts Feb 18, 2019
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Feb 19, 2019

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10453/

@danez

This comment has been minimized.

Copy link
Member

commented Feb 22, 2019

Looks very promising, let us know when you are done.

@tanhauhau tanhauhau changed the title WIP: TypeScript Constant contexts TypeScript Constant contexts Feb 22, 2019
@tanhauhau

This comment has been minimized.

Copy link
Contributor Author

commented Feb 22, 2019

@danez it's ready for code review 😄

case "UnaryExpression":
return this.tsCheckLiteralForConstantContext(node.argument);
default:
throw this.unexpected(node.start);

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Feb 22, 2019

Member

We can throw better error messages 😉

Suggested change
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":

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Feb 22, 2019

Member

For arrays and objects, don't we need to check their children?

e.g.

[ foo() ] as const

This comment has been minimized.

Copy link
@tanhauhau

tanhauhau Feb 22, 2019

Author Contributor

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();

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Feb 22, 2019

Member
Suggested change
node.typeAnnotation = _const ? _const : this.tsNextThenParseType();
node.typeAnnotation = _const || this.tsNextThenParseType();
@nicolo-ribaudo nicolo-ribaudo removed the PR: WIP label Feb 22, 2019
@tanhauhau tanhauhau force-pushed the tanhauhau:tanhauhau/parser-typescript-as-const branch from cc8eb28 to 76f1c61 Feb 25, 2019
@nicolo-ribaudo nicolo-ribaudo added this to the v7.5.0 milestone Mar 10, 2019
let o5 = { ...o4 } as const;
let o6 = { ...o5 };
let o7 = { ...d } as const;
let o8 = { ...o7 };

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Mar 10, 2019

Member

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.

This comment has been minimized.

Copy link
@tanhauhau

tanhauhau Mar 11, 2019

Author Contributor

Updated the test cases

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Mar 10, 2019

Some tests are failing on travis/circle because of regenerator, but they will work if you rebase this PR on master.

@tanhauhau tanhauhau force-pushed the tanhauhau:tanhauhau/parser-typescript-as-const branch from 76f1c61 to 735d539 Mar 11, 2019
@tanhauhau

This comment has been minimized.

Copy link
Contributor Author

commented Mar 11, 2019

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Mar 11, 2019

You also need to update the output 😛

@tanhauhau tanhauhau force-pushed the tanhauhau:tanhauhau/parser-typescript-as-const branch from 3acf446 to fcb060f Mar 11, 2019
@danez
danez approved these changes Mar 12, 2019
@nicolo-ribaudo nicolo-ribaudo merged commit 25a3825 into babel:master Mar 16, 2019
5 checks passed
5 checks passed
babel/repl REPL preview is available
Details
buildsize Significant change of babel.min.js up by 1.61 KB (0.02%)
Details
ci/circleci Your tests passed on CircleCI!
Details
codecov/project 87.08% (target 80%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@tanhauhau tanhauhau deleted the tanhauhau:tanhauhau/parser-typescript-as-const branch Apr 18, 2019
@lock lock bot added the outdated label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.