Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions scripts/eslint/rules/jsdoc-format.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = createRule({
internalCommentNotLastError: `@internal should only appear in final JSDoc comment for declaration.`,
multipleJSDocError: `Declaration has multiple JSDoc comments.`,
internalCommentOnParameterProperty: `@internal cannot appear on a JSDoc comment; use a declared property and an assignment in the constructor instead.`,
internalCommentOnUnexported: `@internal should not appear on an unexported declaration.`,
},
schema: [],
type: "problem",
Expand All @@ -23,6 +24,31 @@ module.exports = createRule({
const atInternal = "@internal";
const jsdocStart = "/**";

/** @type {Map<import("@typescript-eslint/utils").TSESTree.Node, boolean>} */
const isExportedCache = new Map();
Copy link
Member Author

Choose a reason for hiding this comment

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

This closure is created per-file so there's no leaking happening here via the Map.


/** @type {(node: import("@typescript-eslint/utils").TSESTree.Node) => boolean} */
function isExported(node) {
const exported = isExportedCache.get(node);
if (exported !== undefined) {
return exported;
}

/** @type {import("@typescript-eslint/utils").TSESTree.Node | undefined} */
let current = node;
while (current) {
// https://github.com/typescript-eslint/typescript-eslint/blob/e44a1a280f08f9fd0d29f74e5c3e73b7b64a9606/packages/eslint-plugin/src/util/collectUnusedVariables.ts#L440
if (current.type.startsWith("Export")) {
isExportedCache.set(node, true);
return true;
}
isExportedCache.set(current, false);
current = current.parent;
}

return false;
}

/** @type {(text: string) => boolean} */
function isJSDocText(text) {
return text.startsWith(jsdocStart);
Expand Down Expand Up @@ -81,12 +107,15 @@ module.exports = createRule({
if (!isJSDoc) {
context.report({ messageId: "internalCommentInNonJSDocError", node: c, loc: getAtInternalLoc(c, indexInComment) });
}
else if (i !== last) {
context.report({ messageId: "internalCommentNotLastError", node: c, loc: getAtInternalLoc(c, indexInComment) });
}
else if (node.type === "TSParameterProperty") {
context.report({ messageId: "internalCommentOnParameterProperty", node: c, loc: getAtInternalLoc(c, indexInComment) });
}
else if (!isExported(node)) {
context.report({ messageId: "internalCommentOnUnexported", node: c, loc: getAtInternalLoc(c, indexInComment) });
}
else if (i !== last) {
context.report({ messageId: "internalCommentNotLastError", node: c, loc: getAtInternalLoc(c, indexInComment) });
}
}
};

Expand Down
2 changes: 0 additions & 2 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2805,13 +2805,11 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}
}

/** @internal */
function codePointAt(s: string, i: number): number {
// TODO(jakebailey): this is wrong and should have ?? 0; but all users are okay with it
return s.codePointAt(i)!;
}

/** @internal */
function charSize(ch: number) {
if (ch >= 0x10000) {
return 2;
Expand Down
2 changes: 1 addition & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ class IdentifierObject extends TokenOrIdentifierObject<SyntaxKind.Identifier> im
declare _declarationBrand: any;
declare _jsdocContainerBrand: any;
declare _flowContainerBrand: any;
/** @internal */ typeArguments!: NodeArray<TypeNode>;
typeArguments!: NodeArray<TypeNode>;
constructor(kind: SyntaxKind.Identifier, pos: number, end: number) {
super(kind, pos, end);
}
Expand Down