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

Forbid initializer of TSPropertySignature #14264

Merged
merged 1 commit into from
Jan 31, 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
8 changes: 8 additions & 0 deletions src/language-js/parse/postprocess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ function postprocess(ast, options) {
}
}
break;
case "TSPropertySignature":
if (node.initializer) {
throwSyntaxError(
node.initializer,
"An interface property cannot have an initializer."
);
}
break;

// For hack-style pipeline
case "TopicReference":
Expand Down
18 changes: 4 additions & 14 deletions src/language-js/print/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,12 @@ function printTypescript(path, options, print) {
case "TSArrayType":
return [print("elementType"), "[]"];
case "TSPropertySignature":
if (node.readonly) {
parts.push("readonly ");
}

parts.push(
return [
node.readonly ? "readonly " : "",
printPropertyKey(path, options, print),
printOptionalToken(path),
printTypeAnnotationProperty(path, print)
);

// This isn't valid semantically, but it's in the AST so we can print it.
if (node.initializer) {
parts.push(" = ", print("initializer"));
}

return parts;
printTypeAnnotationProperty(path, print),
];

case "TSParameterProperty":
if (node.accessibility) {
Expand Down
3 changes: 3 additions & 0 deletions src/language-js/traverse/visitor-keys.evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const excludeKeys = {
// TODO: Remove `types` when babel changes AST of `TupleTypeAnnotation`
// Flow parser changed `.types` to `.elementTypes` https://github.com/facebook/flow/commit/5b60e6a81dc277dfab2e88fa3737a4dc9aafdcab
// TupleTypeAnnotation: ["types"],

// TypeScript
TSPropertySignature: ["initializer"],
};

const visitorKeys = Object.fromEntries(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snippet: #0 [babel-ts] format 1`] = `
"Unexpected token, expected ";" (1:25)
> 1 | interface I { x: number = 1;}
| ^"
`;

exports[`snippet: #0 [typescript] format 1`] = `
"An interface property cannot have an initializer. (1:27)
> 1 | interface I { x: number = 1;}
| ^"
`;
10 changes: 10 additions & 0 deletions tests/format/misc/errors/typescript/interface/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
run_spec(
{
importMeta: import.meta,
snippets: [
// Invalid initializer
"interface I { x: number = 1;}",
],
},
["babel-ts", "typescript"]
);
27 changes: 0 additions & 27 deletions tests/format/typescript/compiler/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -441,33 +441,6 @@ const;
================================================================================
`;

exports[`errorOnInitializerInInterfaceProperty.ts [babel-ts] format 1`] = `
"Unexpected token, expected ";" (2:17)
1 | interface Foo {
> 2 | bar: number = 5;
| ^
3 | }
4 |"
`;

exports[`errorOnInitializerInInterfaceProperty.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
interface Foo {
bar: number = 5;
}

=====================================output=====================================
interface Foo {
bar: number = 5;
}

================================================================================
`;

exports[`es5ExportDefaultClassDeclaration4.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down

This file was deleted.

6 changes: 1 addition & 5 deletions tests/format/typescript/compiler/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
run_spec(import.meta, ["typescript"], {
errors: {
"babel-ts": [
"downlevelLetConst1.ts",
"errorOnInitializerInInterfaceProperty.ts",
"decrementAndIncrementOperators.ts",
],
"babel-ts": ["downlevelLetConst1.ts", "decrementAndIncrementOperators.ts"],
},
});
1 change: 0 additions & 1 deletion tests/unit/__snapshots__/visitor-keys.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,6 @@ exports[`visitor keys estree 1`] = `
"TSPropertySignature": [
"key",
"typeAnnotation",
"initializer",
],
"TSProtectedKeyword": [],
"TSPublicKeyword": [],
Expand Down