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

Stop checking invalid properties #14274

Merged
merged 14 commits into from
Feb 3, 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
1 change: 0 additions & 1 deletion src/language-js/parse/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ const allowedMessageCodes = new Set([
"ConstructorHasTypeParameters",

"UnsupportedParameterPropertyKind",
"UnexpectedParameterModifier",

"MixedLabeledAndUnlabeledElements",
"InvalidTupleMemberLabel",
Expand Down
66 changes: 64 additions & 2 deletions src/language-js/parse/postprocess/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import isNonEmptyArray from "../../../utils/is-non-empty-array.js";
import visitNode from "./visit-node.js";
import throwTsSyntaxError from "./throw-ts-syntax-error.js";

/** @type {import("typescript")} */
let ts;

function getTsNodeLocation(nodeOrToken) {
const sourceFile = ts.getSourceFileOfNode(nodeOrToken);
const position = ts.rangeOfNode(nodeOrToken);
const sourceFile =
// @ts-expect-error -- internal?
ts.getSourceFileOfNode(nodeOrToken);
const position =
// @ts-expect-error -- internal?
ts.rangeOfNode(nodeOrToken);
const [start, end] = [position.pos, position.end].map((position) => {
const { line, character: column } =
sourceFile.getLineAndCharacterOfPosition(position);
Expand Down Expand Up @@ -110,6 +115,24 @@ function throwErrorForInvalidModifier(node) {
);
}

if (
(modifier.kind === SyntaxKind.InKeyword ||
modifier.kind === SyntaxKind.OutKeyword) &&
(node.kind !== SyntaxKind.TypeParameter ||
!(
ts.isInterfaceDeclaration(node.parent) ||
ts.isClassLike(node.parent) ||
ts.isTypeAliasDeclaration(node.parent)
))
) {
throwErrorOnTsNode(
modifier,
`'${ts.tokenToString(
modifier.kind
)}' modifier can only appear on a type parameter of a class, interface or type alias`
);
}

if (
modifier.kind === SyntaxKind.ReadonlyKeyword &&
node.kind !== SyntaxKind.PropertyDeclaration &&
Expand Down Expand Up @@ -189,6 +212,45 @@ function throwErrorForInvalidModifier(node) {
) {
throwErrorOnTsNode(modifier, "'async' modifier cannot be used here.");
}

// `checkGrammarModifiers` function in `typescript`
if (
node.kind === SyntaxKind.Parameter &&
(modifier.kind === SyntaxKind.StaticKeyword ||
modifier.kind === SyntaxKind.ExportKeyword ||
modifier.kind === SyntaxKind.DeclareKeyword ||
modifier.kind === SyntaxKind.AsyncKeyword)
) {
throwErrorOnTsNode(
modifier,
`'${ts.tokenToString(
modifier.kind
)}' modifier cannot appear on a parameter.`
);
}

// `checkParameter` function in `typescript`
if (
node.kind === SyntaxKind.Parameter &&
// @ts-expect-error -- internal?
ts.hasSyntacticModifier(node, ts.ModifierFlags.ParameterPropertyModifier)
) {
const func =
// @ts-expect-error -- internal?
ts.getContainingFunction(node);
if (
!(
func.kind === SyntaxKind.Constructor &&
// @ts-expect-error -- internal?
ts.nodeIsPresent(func.body)
)
) {
throwErrorOnTsNode(
modifier,
"A parameter property is only allowed in a constructor implementation."
);
}
}
}
}

Expand Down
24 changes: 7 additions & 17 deletions src/language-js/print/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,14 @@ function printTypescript(path, options, print) {
];

case "TSParameterProperty":
parts.push(printTypeScriptAccessibilityToken(node));
if (node.export) {
parts.push("export ");
}
if (node.static) {
parts.push("static ");
}
if (node.override) {
parts.push("override ");
}
if (node.readonly) {
parts.push("readonly ");
}

parts.push(print("parameter"));
return [
printTypeScriptAccessibilityToken(node),
node.static ? "static " : "",
node.override ? "override " : "",
node.readonly ? "readonly " : "",
print("parameter"),
];

return parts;
case "TSTypeQuery":
return ["typeof ", print("exprName"), print("typeParameters")];
case "TSIndexSignature": {
Expand All @@ -212,7 +203,6 @@ function printTypescript(path, options, print) {
]);

return [
node.export ? "export " : "",
printTypeScriptAccessibilityToken(node),
node.static ? "static " : "",
node.readonly ? "readonly " : "",
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 @@ -50,6 +50,9 @@ const additionalVisitorKeys = {
};

const excludeKeys = {
// From `tsVisitorKeys`
MethodDefinition: ["typeParameters"],

// From `flowVisitorKeys`
ArrowFunctionExpression: ["id"],
DeclareOpaqueType: ["impltype"],
Expand Down