mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-03-15 14:05:47 -05:00
Add lint error for declarations marked internal, but unexported (#58229)
This commit is contained in:
@@ -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",
|
||||
@@ -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();
|
||||
|
||||
/** @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);
|
||||
@@ -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) });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user