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

preserve declarations if @internal is mentioned in unrelated comment #57960

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Expand Up @@ -227,6 +227,7 @@ import {
JSDocFunctionType,
JSDocImplementsTag,
JSDocImportTag,
JSDocInternalTag,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
Expand Down Expand Up @@ -949,6 +950,12 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
get updateJSDocSatisfiesTag() {
return getJSDocTypeLikeTagUpdateFunction<JSDocSatisfiesTag>(SyntaxKind.JSDocSatisfiesTag);
},
get createJSDocInternalTag() {
return getJSDocSimpleTagCreateFunction<JSDocInternalTag>(SyntaxKind.JSDocInternalTag);
},
get updateJSDocInternalTag() {
return getJSDocSimpleTagUpdateFunction<JSDocInternalTag>(SyntaxKind.JSDocInternalTag);
},

createJSDocEnumTag,
updateJSDocEnumTag,
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/factory/nodeTests.ts
Expand Up @@ -92,6 +92,7 @@ import {
JSDocFunctionType,
JSDocImplementsTag,
JSDocImportTag,
JSDocInternalTag,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
Expand Down Expand Up @@ -1188,6 +1189,10 @@ export function isJSDocImportTag(node: Node): node is JSDocImportTag {
return node.kind === SyntaxKind.JSDocImportTag;
}

export function isJSDocInternalTag(node: Node): node is JSDocInternalTag {
return node.kind === SyntaxKind.JSDocInternalTag;
}

// Synthesized list

/** @internal */
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/parser.ts
Expand Up @@ -9087,6 +9087,9 @@ namespace Parser {
case "satisfies":
tag = parseSatisfiesTag(start, tagName, margin, indentText);
break;
case "internal":
tag = parseSimpleTag(start, factory.createJSDocInternalTag, tagName, margin, indentText);
break;
case "see":
tag = parseSeeTag(start, tagName, margin, indentText);
break;
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/types.ts
Expand Up @@ -440,6 +440,7 @@ export const enum SyntaxKind {
JSDocThrowsTag,
JSDocSatisfiesTag,
JSDocImportTag,
JSDocInternalTag,

// Synthesized list
SyntaxList,
Expand Down Expand Up @@ -4068,6 +4069,11 @@ export interface JSDocImportTag extends JSDocTag {
readonly attributes?: ImportAttributes;
}

export interface JSDocInternalTag extends JSDocTag {
readonly kind: SyntaxKind.JSDocInternalTag;
readonly typeExpression: JSDocTypeExpression;
}

// NOTE: Ensure this is up-to-date with src/debug/debug.ts
// dprint-ignore
export const enum FlowFlags {
Expand Down Expand Up @@ -8782,6 +8788,8 @@ export interface NodeFactory {
updateJSDocSatisfiesTag(node: JSDocSatisfiesTag, tagName: Identifier | undefined, typeExpression: JSDocTypeExpression, comment: string | NodeArray<JSDocComment> | undefined): JSDocSatisfiesTag;
createJSDocImportTag(tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes?: ImportAttributes, comment?: string | NodeArray<JSDocComment>): JSDocImportTag;
updateJSDocImportTag(node: JSDocImportTag, tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes: ImportAttributes | undefined, comment: string | NodeArray<JSDocComment> | undefined): JSDocImportTag;
createJSDocInternalTag(tagName: Identifier | undefined, comment?: string | NodeArray<JSDocComment>): JSDocInternalTag;
updateJSDocInternalTag(node: JSDocInternalTag, tagName: Identifier | undefined, comment: string | NodeArray<JSDocComment> | undefined): JSDocInternalTag;
createJSDocText(text: string): JSDocText;
updateJSDocText(node: JSDocText, text: string): JSDocText;
createJSDocComment(comment?: string | NodeArray<JSDocComment> | undefined, tags?: readonly JSDocTag[] | undefined): JSDoc;
Expand Down
39 changes: 7 additions & 32 deletions src/compiler/utilitiesPublic.ts
Expand Up @@ -32,10 +32,8 @@ import {
ClassLikeDeclaration,
ClassStaticBlockDeclaration,
combinePaths,
CommentRange,
compareDiagnostics,
CompilerOptions,
concatenate,
ConciseBody,
ConstructorDeclaration,
ConstructorTypeNode,
Expand Down Expand Up @@ -64,7 +62,6 @@ import {
filter,
find,
flatMap,
forEach,
ForInitializer,
ForInOrOfStatement,
FunctionBody,
Expand All @@ -84,10 +81,7 @@ import {
getJSDocCommentsAndTags,
getJSDocRoot,
getJSDocTypeParameterDeclarations,
getLeadingCommentRanges,
getLeadingCommentRangesOfNode,
getSourceFileOfNode,
getTrailingCommentRanges,
hasAccessorModifier,
HasDecorators,
hasDecorators,
Expand Down Expand Up @@ -140,6 +134,7 @@ import {
isJSDocEnumTag,
isJSDocFunctionType,
isJSDocImplementsTag,
isJSDocInternalTag,
isJSDocOverloadTag,
isJSDocOverrideTag,
isJSDocParameterTag,
Expand Down Expand Up @@ -185,6 +180,7 @@ import {
JSDocDeprecatedTag,
JSDocEnumTag,
JSDocImplementsTag,
JSDocInternalTag,
JSDocLink,
JSDocLinkCode,
JSDocLinkPlain,
Expand All @@ -211,7 +207,6 @@ import {
JsxTagNameExpression,
KeywordSyntaxKind,
LabeledStatement,
last,
lastOrUndefined,
LeftHandSideExpression,
length,
Expand Down Expand Up @@ -266,7 +261,6 @@ import {
setUILocale,
SignatureDeclaration,
skipOuterExpressions,
skipTrivia,
some,
sortAndDeduplicate,
SortedReadonlyArray,
Expand Down Expand Up @@ -1144,6 +1138,10 @@ export function getJSDocSatisfiesTag(node: Node): JSDocSatisfiesTag | undefined
return getFirstJSDocTag(node, isJSDocSatisfiesTag);
}

export function getJSDocInternalTag(node: Node): JSDocInternalTag | undefined {
return getFirstJSDocTag(node, isJSDocInternalTag);
}

/** Gets the JSDoc type tag for the node if present and valid */
export function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined {
// We should have already issued an error if there were multiple type jsdocs, so just use the first one.
Expand Down Expand Up @@ -2596,31 +2594,8 @@ export function isRestParameter(node: ParameterDeclaration | JSDocParameterTag):
return (node as ParameterDeclaration).dotDotDotToken !== undefined || !!type && type.kind === SyntaxKind.JSDocVariadicType;
}

function hasInternalAnnotation(range: CommentRange, sourceFile: SourceFile) {
const comment = sourceFile.text.substring(range.pos, range.end);
return comment.includes("@internal");
}

export function isInternalDeclaration(node: Node, sourceFile?: SourceFile) {
sourceFile ??= getSourceFileOfNode(node);
const parseTreeNode = getParseTreeNode(node);
if (parseTreeNode && parseTreeNode.kind === SyntaxKind.Parameter) {
const paramIdx = (parseTreeNode.parent as SignatureDeclaration).parameters.indexOf(parseTreeNode as ParameterDeclaration);
const previousSibling = paramIdx > 0 ? (parseTreeNode.parent as SignatureDeclaration).parameters[paramIdx - 1] : undefined;
const text = sourceFile.text;
const commentRanges = previousSibling
? concatenate(
// to handle
// ... parameters, /** @internal */
// public param: string
getTrailingCommentRanges(text, skipTrivia(text, previousSibling.end + 1, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true)),
getLeadingCommentRanges(text, node.pos),
)
: getTrailingCommentRanges(text, skipTrivia(text, node.pos, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true));
return some(commentRanges) && hasInternalAnnotation(last(commentRanges), sourceFile);
}
const leadingCommentRanges = parseTreeNode && getLeadingCommentRangesOfNode(parseTreeNode, sourceFile);
return !!forEach(leadingCommentRanges, range => {
return hasInternalAnnotation(range, sourceFile);
});
return parseTreeNode && !!getJSDocInternalTag(parseTreeNode);
Zzzen marked this conversation as resolved.
Show resolved Hide resolved
}
23 changes: 16 additions & 7 deletions tests/baselines/reference/api/typescript.d.ts
Expand Up @@ -3941,12 +3941,13 @@ declare namespace ts {
JSDocThrowsTag = 349,
JSDocSatisfiesTag = 350,
JSDocImportTag = 351,
SyntaxList = 352,
NotEmittedStatement = 353,
PartiallyEmittedExpression = 354,
CommaListExpression = 355,
SyntheticReferenceExpression = 356,
Count = 357,
JSDocInternalTag = 352,
SyntaxList = 353,
NotEmittedStatement = 354,
PartiallyEmittedExpression = 355,
CommaListExpression = 356,
SyntheticReferenceExpression = 357,
Count = 358,
FirstAssignment = 64,
LastAssignment = 79,
FirstCompoundAssignment = 65,
Expand Down Expand Up @@ -5791,6 +5792,10 @@ declare namespace ts {
readonly moduleSpecifier: Expression;
readonly attributes?: ImportAttributes;
}
interface JSDocInternalTag extends JSDocTag {
readonly kind: SyntaxKind.JSDocInternalTag;
readonly typeExpression: JSDocTypeExpression;
}
enum FlowFlags {
Unreachable = 1,
Start = 2,
Expand Down Expand Up @@ -7706,6 +7711,8 @@ declare namespace ts {
updateJSDocSatisfiesTag(node: JSDocSatisfiesTag, tagName: Identifier | undefined, typeExpression: JSDocTypeExpression, comment: string | NodeArray<JSDocComment> | undefined): JSDocSatisfiesTag;
createJSDocImportTag(tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes?: ImportAttributes, comment?: string | NodeArray<JSDocComment>): JSDocImportTag;
updateJSDocImportTag(node: JSDocImportTag, tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes: ImportAttributes | undefined, comment: string | NodeArray<JSDocComment> | undefined): JSDocImportTag;
createJSDocInternalTag(tagName: Identifier | undefined, comment?: string | NodeArray<JSDocComment>): JSDocInternalTag;
updateJSDocInternalTag(node: JSDocInternalTag, tagName: Identifier | undefined, comment: string | NodeArray<JSDocComment> | undefined): JSDocInternalTag;
createJSDocText(text: string): JSDocText;
updateJSDocText(node: JSDocText, text: string): JSDocText;
createJSDocComment(comment?: string | NodeArray<JSDocComment> | undefined, tags?: readonly JSDocTag[] | undefined): JSDoc;
Expand Down Expand Up @@ -8561,6 +8568,7 @@ declare namespace ts {
/** Gets the JSDoc template tag for the node if present */
function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined;
function getJSDocSatisfiesTag(node: Node): JSDocSatisfiesTag | undefined;
function getJSDocInternalTag(node: Node): JSDocInternalTag | undefined;
/** Gets the JSDoc type tag for the node if present and valid */
function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined;
/**
Expand Down Expand Up @@ -8694,7 +8702,7 @@ declare namespace ts {
function isJSDocLinkLike(node: Node): node is JSDocLink | JSDocLinkCode | JSDocLinkPlain;
function hasRestParameter(s: SignatureDeclaration | JSDocSignature): boolean;
function isRestParameter(node: ParameterDeclaration | JSDocParameterTag): boolean;
function isInternalDeclaration(node: Node, sourceFile?: SourceFile): boolean;
function isInternalDeclaration(node: Node, sourceFile?: SourceFile): boolean | undefined;
const unchangedTextChangeRange: TextChangeRange;
type ParameterPropertyDeclaration = ParameterDeclaration & {
parent: ConstructorDeclaration;
Expand Down Expand Up @@ -9009,6 +9017,7 @@ declare namespace ts {
function isJSDocSatisfiesTag(node: Node): node is JSDocSatisfiesTag;
function isJSDocThrowsTag(node: Node): node is JSDocThrowsTag;
function isJSDocImportTag(node: Node): node is JSDocImportTag;
function isJSDocInternalTag(node: Node): node is JSDocInternalTag;
function isQuestionOrExclamationToken(node: Node): node is QuestionToken | ExclamationToken;
function isIdentifierOrThisTypeNode(node: Node): node is Identifier | ThisTypeNode;
function isReadonlyKeywordOrPlusOrMinusToken(node: Node): node is ReadonlyKeyword | PlusToken | MinusToken;
Expand Down
Expand Up @@ -93,7 +93,9 @@ exports.Baz = Baz;

//// [declarationEmitWorkWithInlineComments.d.ts]
export declare class Foo {
notInternal1: string;
isInternal4: string;
isInternal6: string;
isInternal7: string;
notInternal2: string;
notInternal3: string;
constructor(
Expand All @@ -104,8 +106,10 @@ export declare class Foo {
isInternal5: string, isInternal6: string, isInternal7: string, /** @internal */ notInternal1: string, notInternal2: string, notInternal3: string);
}
export declare class Bar {
isInternal1: string;
constructor(/* @internal */ isInternal1: string);
}
export declare class Baz {
isInternal: string;
constructor(/* @internal */ isInternal: string);
}
1 change: 1 addition & 0 deletions tests/baselines/reference/stripInternal1.js
Expand Up @@ -21,4 +21,5 @@ var C = /** @class */ (function () {
//// [stripInternal1.d.ts]
declare class C {
foo(): void;
bar(): void;
}
46 changes: 46 additions & 0 deletions tests/baselines/reference/stripInternal2.js
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/stripInternal2.ts] ////

//// [stripInternal2.ts]
export class Foo {
/**
* Should be stripped
* @internal
*/
shouldBeStripped = 1;

// TODO: maybe make this @internal?
/**
* Should *not* be stripped
*/
shouldNotBeStripped = 2;
}

//// [stripInternal2.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Foo = void 0;
var Foo = /** @class */ (function () {
function Foo() {
/**
* Should be stripped
* @internal
*/
this.shouldBeStripped = 1;
// TODO: maybe make this @internal?
/**
* Should *not* be stripped
*/
this.shouldNotBeStripped = 2;
}
return Foo;
}());
exports.Foo = Foo;


//// [stripInternal2.d.ts]
export declare class Foo {
/**
* Should *not* be stripped
*/
shouldNotBeStripped: number;
}
20 changes: 20 additions & 0 deletions tests/baselines/reference/stripInternal2.symbols
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/stripInternal2.ts] ////

=== stripInternal2.ts ===
export class Foo {
>Foo : Symbol(Foo, Decl(stripInternal2.ts, 0, 0))

/**
* Should be stripped
* @internal
*/
shouldBeStripped = 1;
>shouldBeStripped : Symbol(Foo.shouldBeStripped, Decl(stripInternal2.ts, 0, 18))

// TODO: maybe make this @internal?
/**
* Should *not* be stripped
*/
shouldNotBeStripped = 2;
>shouldNotBeStripped : Symbol(Foo.shouldNotBeStripped, Decl(stripInternal2.ts, 5, 23))
}
22 changes: 22 additions & 0 deletions tests/baselines/reference/stripInternal2.types
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/stripInternal2.ts] ////

=== stripInternal2.ts ===
export class Foo {
>Foo : Foo

/**
* Should be stripped
* @internal
*/
shouldBeStripped = 1;
>shouldBeStripped : number
>1 : 1

// TODO: maybe make this @internal?
/**
* Should *not* be stripped
*/
shouldNotBeStripped = 2;
>shouldNotBeStripped : number
>2 : 2
}
16 changes: 16 additions & 0 deletions tests/cases/compiler/stripInternal2.ts
@@ -0,0 +1,16 @@
// @declaration:true
// @stripInternal:true

export class Foo {
/**
* Should be stripped
* @internal
*/
shouldBeStripped = 1;

// TODO: maybe make this @internal?
/**
* Should *not* be stripped
*/
shouldNotBeStripped = 2;
}