Skip to content

#48650 - Allow arbitrary TS syntax in comments in JS files. #58601

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6885,6 +6885,10 @@
"category": "Error",
"code": 8039
},
"JavaScript syntax can not be used inside of a TS comment": {
"category": "Error",
"code": 8040
},

"Declaration emit for this file requires using private name '{0}'. An explicit type annotation may unblock declaration emit.": {
"category": "Error",
Expand Down
17 changes: 15 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,18 @@ export function forEachChild<T>(node: Node, cbNode: (node: Node) => T | undefine
return fn === undefined ? undefined : fn(node, cbNode, cbNodes);
}

/**
* The definition of a leaf node isn't super precise here.
* For example, an empty object could be thought of as a leaf node since it doesn't have any children,
* but this function would claim that it isn't a leaf node.
* For the purposes of how this function gets used,
* what's important is that it won't ever claim that a node that has children is a leaf node.
* Other types of minor errors are fine.
*/
export function isLeafNode(node: Node) {
return (forEachChildTable as Record<SyntaxKind, ForEachChildFunction<any>>)[node.kind] === undefined;
}

/**
* Invokes a callback for each child of the given node. The 'cbNode' callback is invoked for all child nodes
* stored in properties. If a 'cbNodes' callback is specified, it is invoked for embedded arrays; additionally,
Expand Down Expand Up @@ -5241,6 +5253,7 @@ namespace Parser {
}
}

const firstTokenPrecededByOpeningTSComment = (scanner.getTokenFlags() & TokenFlags.enteringTSComment) !== 0;
const first = token();
const second = nextToken();

Expand Down Expand Up @@ -5327,7 +5340,7 @@ namespace Parser {
}

// JSX overrides
if (languageVariant === LanguageVariant.JSX) {
if (languageVariant === LanguageVariant.JSX && !firstTokenPrecededByOpeningTSComment) {
const isArrowFunctionInJsx = lookAhead(() => {
parseOptional(SyntaxKind.ConstKeyword);
const third = nextToken();
Expand Down Expand Up @@ -6531,7 +6544,7 @@ namespace Parser {
}

function parseTypeArgumentsInExpression() {
if ((contextFlags & NodeFlags.JavaScriptFile) !== 0) {
if ((contextFlags & NodeFlags.JavaScriptFile) !== 0 && (scanner.getTokenFlags() & TokenFlags.enteringTSComment) === 0) {
// TypeArguments must not be parsed in JavaScript files to avoid ambiguity with binary operators.
return undefined;
}
Expand Down
Loading
Loading