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 - Tuple elements can be optional #8720

Merged
merged 7 commits into from Oct 2, 2018

Conversation

@Retsam
Copy link
Contributor

Retsam commented Sep 17, 2018

Q                       A
Fixed Issues? #8623 (partial)
Patch: Bug Fix?
Major: Breaking Change? Maybe.
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This is part of #8623:

Tuples elements are allowed to be optional (so each element in a tuple can have a trailing ?).

To implement this, I introduced a new layer in the AST: previously TSTupleType had an array of TSType nodes; now it has an array of TSTupleElementType nodes, which are a wrapper around a TSType, with information about whether it's optional and (eventually) whether it's a rest element. So [string, number?] is parsed as:

{
    type: "TSTupleType",
    elementTypes: [{
        type: "TSTupleElementType",
        isOptional: false,
        elementType: {
            type: "TSStringKeyword"
        }
    }, {
        type: "TSTupleElementType",
        isOptional: true,
        elementType: {
            type: "TSNumberKeyword"
        }
    }]
}

I'm not sure if this change to the AST would be considered a breaking change. Alternatively, I think that information could be added onto all TSType nodes directly, but it seemed a bit ugly to put that sort of context-specific information onto arbitrary type nodes.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

babel-bot commented Sep 17, 2018

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

@Retsam Retsam force-pushed the Retsam:ts3-tuples branch from e6d19e8 to 902f277 Sep 17, 2018
@g-plane

This comment has been minimized.

Copy link
Contributor

g-plane commented Sep 24, 2018

Why add a new type TSTupleElementType?

And, compared with the AST of TypeScript and typescript-eslint-parser, I think we should add a new type called TSOptionalType, instead of adding property called isOptional.

Uses a TsOptionalType instead of introducing a new TsTupleElementType
@Retsam

This comment has been minimized.

Copy link
Contributor Author

Retsam commented Sep 24, 2018

I've reworked this to be more inline with what @g-plane suggested. I went down the route of TSTupleElementType because this syntax is a special case for tuples, but I suppose it's perhaps best if the AST doesn't reflect that.

So the parsing logic is now a special case when parsing a tuple, but the AST is just TSOptionalType as suggested.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

nicolo-ribaudo commented Sep 25, 2018

Since we already use typeAnnotation for flow, I guess that it would be better to use it also for typesctipt?

defineType("NullableTypeAnnotation", {
visitor: ["typeAnnotation"],
aliases: ["Flow", "FlowType"],
fields: {
typeAnnotation: validateType("FlowType"),
},
});

This renames the main member of the TSOptionalType node
@Retsam Retsam force-pushed the Retsam:ts3-tuples branch from 58f09a9 to 743501c Sep 26, 2018
@Retsam

This comment has been minimized.

Copy link
Contributor Author

Retsam commented Sep 26, 2018

Okay; still seems like an odd convention to me, but consistency is good. I switched it to typeAnnotation.

@existentialism

This comment has been minimized.

Copy link
Member

existentialism commented Sep 27, 2018

@andy-ms
andy-ms approved these changes Oct 1, 2018
Copy link
Member

existentialism left a comment

I know it's fairly safe, but might want to add the same two test cases to transform-typescript to prevent regressions?

Otherwise, LGTM. Nice work @Retsam!

@Retsam

This comment has been minimized.

Copy link
Contributor Author

Retsam commented Oct 2, 2018

@existentialism What tests are you looking for, specifically? I've already added a test with optional tuple parameters to transform-typescript. Does this cover it, or should I add another sort of test?

@existentialism

This comment has been minimized.

Copy link
Member

existentialism commented Oct 2, 2018

@Retsam don't mind me, somehow missed that, lol.

@existentialism existentialism merged commit a5b5ed9 into babel:master Oct 2, 2018
4 checks passed
4 checks passed
babel/repl REPL preview is available
Details
ci/circleci Your tests passed on CircleCI!
Details
codecov/project 80.57% (target 80%)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@Retsam Retsam deleted the Retsam:ts3-tuples branch Oct 2, 2018
@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.
Projects
None yet
6 participants
You can’t perform that action at this time.