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

[ts] Support const modifier in type parameters #15384

Merged
merged 3 commits into from Feb 18, 2023
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
202 changes: 115 additions & 87 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -53,6 +53,7 @@ type TsModifier =
| "declare"
| "static"
| "override"
| "const"
| N.Accessibility
| N.VarianceAnnotations;

Expand Down Expand Up @@ -325,7 +326,11 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
allowedModifiers: T[],
stopOnStartOfClassStaticBlock?: boolean,
): T | undefined | null {
if (!tokenIsIdentifier(this.state.type) && this.state.type !== tt._in) {
if (
!tokenIsIdentifier(this.state.type) &&
this.state.type !== tt._in &&
this.state.type !== tt._const
) {
return undefined;
}

Expand All @@ -346,20 +351,20 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
* this.tsParseModifiers({ modified: node, allowedModifiers: ["public"] });
* this.tsParseModifiers({ modified: node, allowedModifiers: ["abstract", "readonly"] });
*/
tsParseModifiers({
modified,
allowedModifiers,
disallowedModifiers,
stopOnStartOfClassStaticBlock,
errorTemplate = TSErrors.InvalidModifierOnTypeMember,
}: {
modified: ModifierBase;
allowedModifiers: readonly TsModifier[];
disallowedModifiers?: TsModifier[];
stopOnStartOfClassStaticBlock?: boolean;
// FIXME: make sure errorTemplate can receive `modifier`
errorTemplate?: any;
}): void {
tsParseModifiers<N extends ModifierBase>(
{
allowedModifiers,
disallowedModifiers,
stopOnStartOfClassStaticBlock,
errorTemplate = TSErrors.InvalidModifierOnTypeMember,
}: {
allowedModifiers: readonly TsModifier[];
disallowedModifiers?: TsModifier[];
stopOnStartOfClassStaticBlock?: boolean;
errorTemplate?: typeof TSErrors.InvalidModifierOnTypeMember;
},
modified: N,
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this param to the end to more easily use .bind.

): void {
const enforceOrder = (
loc: Position,
modifier: TsModifier,
Expand Down Expand Up @@ -645,37 +650,44 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
return this.finishNode(node, "TSTypeQuery");
}

tsParseInOutModifiers(node: N.TsTypeParameter) {
this.tsParseModifiers({
modified: node,
allowedModifiers: ["in", "out"],
disallowedModifiers: [
"public",
"private",
"protected",
"readonly",
"declare",
"abstract",
"override",
],
errorTemplate: TSErrors.InvalidModifierOnTypeParameter,
});
}

// for better error recover
tsParseNoneModifiers(node: N.TsTypeParameter) {
this.tsParseModifiers({
modified: node,
allowedModifiers: [],
disallowedModifiers: ["in", "out"],
errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions,
});
}
tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
allowedModifiers: ["in", "out"],
disallowedModifiers: [
"const",
"public",
"private",
"protected",
"readonly",
"declare",
"abstract",
"override",
],
errorTemplate: TSErrors.InvalidModifierOnTypeParameter,
});

tsParseConstModifier = this.tsParseModifiers.bind(this, {
allowedModifiers: ["const"],
// for better error recovery
disallowedModifiers: ["in", "out"],
errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions,
});

tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
allowedModifiers: ["in", "out", "const"],
disallowedModifiers: [
"public",
"private",
"protected",
"readonly",
"declare",
"abstract",
"override",
],
errorTemplate: TSErrors.InvalidModifierOnTypeParameter,
});

tsParseTypeParameter(
parseModifiers: (
node: Undone<N.TsTypeParameter>,
) => void = this.tsParseNoneModifiers.bind(this),
parseModifiers: (node: Undone<N.TsTypeParameter>) => void,
): N.TsTypeParameter {
const node = this.startNode<N.TsTypeParameter>();

Expand All @@ -688,16 +700,14 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
}

tsTryParseTypeParameters(
parseModifiers?: ((node: N.TsTypeParameter) => void) | null,
parseModifiers: (node: N.TsTypeParameter) => void,
): N.TsTypeParameterDeclaration | undefined | null {
if (this.match(tt.lt)) {
return this.tsParseTypeParameters(parseModifiers);
}
}

tsParseTypeParameters(
parseModifiers?: ((node: N.TsTypeParameter) => void) | null,
) {
tsParseTypeParameters(parseModifiers: (node: N.TsTypeParameter) => void) {
const node = this.startNode<N.TsTypeParameterDeclaration>();

if (this.match(tt.lt) || this.match(tt.jsxTagStart)) {
Expand Down Expand Up @@ -740,7 +750,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
? "returnType"
: "typeAnnotation";

signature.typeParameters = this.tsTryParseTypeParameters();
signature.typeParameters = this.tsTryParseTypeParameters(
this.tsParseConstModifier,
);
this.expect(tt.parenL);
signature[paramsKey] = this.tsParseBindingListForSignature();
if (returnTokenRequired) {
Expand Down Expand Up @@ -927,19 +939,21 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
}
}

this.tsParseModifiers({
modified: node,
allowedModifiers: ["readonly"],
disallowedModifiers: [
"declare",
"abstract",
"private",
"protected",
"public",
"static",
"override",
],
});
this.tsParseModifiers(
{
allowedModifiers: ["readonly"],
disallowedModifiers: [
"declare",
"abstract",
"private",
"protected",
"public",
"static",
"override",
],
},
node,
);

const idx = this.tsTryParseIndexSignature(node);
if (idx) {
Expand Down Expand Up @@ -1704,7 +1718,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
}

node.typeParameters = this.tsTryParseTypeParameters(
this.tsParseInOutModifiers.bind(this),
this.tsParseInOutConstModifiers,
);
if (this.eat(tt._extends)) {
node.extends = this.tsParseHeritageClause("extends");
Expand All @@ -1723,7 +1737,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>

node.typeAnnotation = this.tsInType(() => {
node.typeParameters = this.tsTryParseTypeParameters(
this.tsParseInOutModifiers.bind(this),
this.tsParseInOutModifiers,
);

this.expect(tt.eq);
Expand Down Expand Up @@ -2191,7 +2205,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
const res: Undone<N.ArrowFunctionExpression> | undefined | null =
this.tsTryParseAndCatch(() => {
const node = this.startNodeAt<N.ArrowFunctionExpression>(startLoc);
node.typeParameters = this.tsParseTypeParameters();
node.typeParameters = this.tsParseTypeParameters(
this.tsParseConstModifier,
);
// Don't use overloaded parseFunctionParams which would look for "<" again.
super.parseFunctionParams(node);
node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
Expand Down Expand Up @@ -2261,16 +2277,18 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
const startLoc = this.state.startLoc;

const modified: ModifierBase = {};
this.tsParseModifiers({
this.tsParseModifiers(
{
allowedModifiers: [
"public",
"private",
"protected",
"override",
"readonly",
],
},
modified,
allowedModifiers: [
"public",
"private",
"protected",
"override",
"readonly",
],
});
);
const accessibility = modified.accessibility;
const override = modified.override;
const readonly = modified.readonly;
Expand Down Expand Up @@ -2874,13 +2892,15 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
"readonly",
"static",
] as const;
this.tsParseModifiers({
modified: member,
allowedModifiers: modifiers,
disallowedModifiers: ["in", "out"],
stopOnStartOfClassStaticBlock: true,
errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions,
});
this.tsParseModifiers(
{
allowedModifiers: modifiers,
disallowedModifiers: ["in", "out"],
stopOnStartOfClassStaticBlock: true,
errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions,
},
member,
);

const callParseClassMemberWithIsStatic = () => {
if (this.tsIsStartOfStaticBlocks()) {
Expand Down Expand Up @@ -3126,7 +3146,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
(node as any).declare ? BIND_TS_AMBIENT : BIND_CLASS,
);
const typeParameters = this.tsTryParseTypeParameters(
this.tsParseInOutModifiers.bind(this),
this.tsParseInOutConstModifiers,
);
if (typeParameters) node.typeParameters = typeParameters;
}
Expand Down Expand Up @@ -3211,7 +3231,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
isConstructor: boolean,
allowsDirectSuper: boolean,
): void {
const typeParameters = this.tsTryParseTypeParameters();
const typeParameters = this.tsTryParseTypeParameters(
this.tsParseConstModifier,
);
if (typeParameters && isConstructor) {
this.raise(TSErrors.ConstructorHasTypeParameters, {
at: typeParameters,
Expand Down Expand Up @@ -3241,7 +3263,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
isGenerator: boolean,
isAsync: boolean,
): void {
const typeParameters = this.tsTryParseTypeParameters();
const typeParameters = this.tsTryParseTypeParameters(
this.tsParseConstModifier,
);
if (typeParameters) method.typeParameters = typeParameters;
super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
}
Expand Down Expand Up @@ -3278,7 +3302,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
isAccessor: boolean,
refExpressionErrors?: ExpressionErrors | null,
) {
const typeParameters = this.tsTryParseTypeParameters();
const typeParameters = this.tsTryParseTypeParameters(
this.tsParseConstModifier,
);
if (typeParameters) prop.typeParameters = typeParameters;

return super.parseObjPropValue(
Expand All @@ -3294,7 +3320,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
}

parseFunctionParams(node: N.Function, isConstructor: boolean): void {
const typeParameters = this.tsTryParseTypeParameters();
const typeParameters = this.tsTryParseTypeParameters(
this.tsParseConstModifier,
);
if (typeParameters) node.typeParameters = typeParameters;
super.parseFunctionParams(node, isConstructor);
}
Expand Down Expand Up @@ -3381,7 +3409,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
let typeParameters: N.TsTypeParameterDeclaration | undefined | null;
const arrow = this.tryParse(abort => {
// This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`.
typeParameters = this.tsParseTypeParameters();
typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
const expr = super.parseMaybeAssign(
refExpressionErrors,
afterLeftParse,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/types.d.ts
Expand Up @@ -1053,6 +1053,7 @@ export interface TsTypeParameter extends NodeBase {
name: string | Identifier;
in?: boolean;
out?: boolean;
const?: boolean;
constraint?: TsType;
default?: TsType;
}
Expand Down
@@ -0,0 +1,32 @@
function a<const T>() {}
function b<const T extends U>() {}
function c<T, const U>() {}
declare function d<const T>();
<const T>() => {};
<const T extends U>() => {};
(function <const T>() {});
(function <const T extends U>() {});
(function <T, const U>() {});

class A<const T> {}
class B<const T extends U> {}
class C<T, const U> {}
class D<in const T> {}
class E<const in T> {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some test cases for anonymous function/class expression? They should already work.

(class <const T> {});
(class <const T extends U> {});
(class <T, const U> {});
(class <in const T> {});
(class <const in T> {});

interface I<const T> {}
interface J<const T extends U> {}
interface K<T, const U> {}
interface L<in const T> {}
interface M<const in T> {}

class _ {
method<const T>() {}
method<const T extends U>() {}
method<T, const U>() {}
}
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}