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

Support TS type argument syntax in optional chaining #496

Merged
merged 1 commit into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,8 @@ export function tsParseSubscript(
}
tsParseTypeArguments();
if (!noCalls && eat(tt.parenL)) {
// With f<T>(), the subscriptStartIndex marker is on the ( token.
state.tokens[state.tokens.length - 1].subscriptStartIndex = startTokenIndex;
parseCallExpressionArguments();
} else if (match(tt.backQuote)) {
// Tagged template with a type argument.
Expand All @@ -1092,6 +1094,16 @@ export function tsParseSubscript(
} else {
return;
}
} else if (!noCalls && match(tt.questionDot) && lookaheadType() === tt.lessThan) {
// If we see f?.<, then this must be an optional call with a type argument.
next();
state.tokens[startTokenIndex].isOptionalChainStart = true;
// With f?.<T>(), the subscriptStartIndex marker is on the ?. token.
state.tokens[state.tokens.length - 1].subscriptStartIndex = startTokenIndex;

tsParseTypeArguments();
expect(tt.parenL);
parseCallExpressionArguments();
}
baseParseSubscript(startTokenIndex, noCalls, stopState);
}
Expand Down
5 changes: 4 additions & 1 deletion src/transformers/OptionalChainingNullishTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export default class OptionalChainingNullishTransformer extends Transformer {
} else {
arrowStartSnippet = `${param} => ${param}`;
}
if (this.tokens.matches2(tt.questionDot, tt.parenL)) {
if (
this.tokens.matches2(tt.questionDot, tt.parenL) ||
this.tokens.matches2(tt.questionDot, tt.lessThan)
) {
this.tokens.replaceTokenTrimmingLeftWhitespace(`, 'optionalCall', ${arrowStartSnippet}`);
} else if (this.tokens.matches2(tt.questionDot, tt.bracketL)) {
this.tokens.replaceTokenTrimmingLeftWhitespace(`, 'optionalAccess', ${arrowStartSnippet}`);
Expand Down
13 changes: 12 additions & 1 deletion test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ describe("typescript transform", () => {
example.inner?.greet<string>()
`,
`"use strict";${OPTIONAL_CHAIN_PREFIX}
_optionalChain([example, 'access', _ => _.inner, 'optionalAccess', _2 => _2.greet()])
_optionalChain([example, 'access', _ => _.inner, 'optionalAccess', _2 => _2.greet, 'call', _3 => _3()])
`,
);
});
Expand Down Expand Up @@ -1931,4 +1931,15 @@ describe("typescript transform", () => {
`,
);
});

it("supports type arguments with optional chaining", () => {
assertTypeScriptResult(
`
const x = a.b?.<number>();
`,
`"use strict";${OPTIONAL_CHAIN_PREFIX}
const x = _optionalChain([a, 'access', _ => _.b, 'optionalCall', _2 => _2()]);
`,
);
});
});