From 9f28ce98e028e2509bc9791981b9f130895628bb Mon Sep 17 00:00:00 2001 From: Zzzen Date: Thu, 28 Mar 2024 00:32:57 +0800 Subject: [PATCH 1/3] preserve declarations if @internal is mentioned in unrelated comment --- src/compiler/factory/nodeFactory.ts | 7 +++ src/compiler/factory/nodeTests.ts | 5 ++ src/compiler/parser.ts | 3 ++ src/compiler/types.ts | 8 ++++ src/compiler/utilitiesPublic.ts | 39 +++------------- tests/baselines/reference/api/typescript.d.ts | 23 +++++++--- .../declarationEmitWorkWithInlineComments.js | 6 ++- tests/baselines/reference/stripInternal1.js | 1 + tests/baselines/reference/stripInternal2.js | 46 +++++++++++++++++++ .../reference/stripInternal2.symbols | 20 ++++++++ .../baselines/reference/stripInternal2.types | 22 +++++++++ tests/cases/compiler/stripInternal2.ts | 16 +++++++ 12 files changed, 156 insertions(+), 40 deletions(-) create mode 100644 tests/baselines/reference/stripInternal2.js create mode 100644 tests/baselines/reference/stripInternal2.symbols create mode 100644 tests/baselines/reference/stripInternal2.types create mode 100644 tests/cases/compiler/stripInternal2.ts diff --git a/src/compiler/factory/nodeFactory.ts b/src/compiler/factory/nodeFactory.ts index 76e084c90d8f0..63cf849656810 100644 --- a/src/compiler/factory/nodeFactory.ts +++ b/src/compiler/factory/nodeFactory.ts @@ -227,6 +227,7 @@ import { JSDocFunctionType, JSDocImplementsTag, JSDocImportTag, + JSDocInternalTag, JSDocLink, JSDocLinkCode, JSDocLinkPlain, @@ -949,6 +950,12 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode get updateJSDocSatisfiesTag() { return getJSDocTypeLikeTagUpdateFunction(SyntaxKind.JSDocSatisfiesTag); }, + get createJSDocInternalTag() { + return getJSDocSimpleTagCreateFunction(SyntaxKind.JSDocInternalTag); + }, + get updateJSDocInternalTag() { + return getJSDocSimpleTagUpdateFunction(SyntaxKind.JSDocInternalTag); + }, createJSDocEnumTag, updateJSDocEnumTag, diff --git a/src/compiler/factory/nodeTests.ts b/src/compiler/factory/nodeTests.ts index 012616ac5dc70..edab7952d7a23 100644 --- a/src/compiler/factory/nodeTests.ts +++ b/src/compiler/factory/nodeTests.ts @@ -92,6 +92,7 @@ import { JSDocFunctionType, JSDocImplementsTag, JSDocImportTag, + JSDocInternalTag, JSDocLink, JSDocLinkCode, JSDocLinkPlain, @@ -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 */ diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index ca4bb9ed61275..f6124edaab58c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -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; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index fe828ad4a4529..174b0acc79923 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -440,6 +440,7 @@ export const enum SyntaxKind { JSDocThrowsTag, JSDocSatisfiesTag, JSDocImportTag, + JSDocInternalTag, // Synthesized list SyntaxList, @@ -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 { @@ -8782,6 +8788,8 @@ export interface NodeFactory { updateJSDocSatisfiesTag(node: JSDocSatisfiesTag, tagName: Identifier | undefined, typeExpression: JSDocTypeExpression, comment: string | NodeArray | undefined): JSDocSatisfiesTag; createJSDocImportTag(tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes?: ImportAttributes, comment?: string | NodeArray): JSDocImportTag; updateJSDocImportTag(node: JSDocImportTag, tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes: ImportAttributes | undefined, comment: string | NodeArray | undefined): JSDocImportTag; + createJSDocInternalTag(tagName: Identifier | undefined, comment?: string | NodeArray): JSDocInternalTag; + updateJSDocInternalTag(node: JSDocInternalTag, tagName: Identifier | undefined, comment: string | NodeArray | undefined): JSDocInternalTag; createJSDocText(text: string): JSDocText; updateJSDocText(node: JSDocText, text: string): JSDocText; createJSDocComment(comment?: string | NodeArray | undefined, tags?: readonly JSDocTag[] | undefined): JSDoc; diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index 328e1b2f0e55c..b41934d2fe8e6 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -32,10 +32,8 @@ import { ClassLikeDeclaration, ClassStaticBlockDeclaration, combinePaths, - CommentRange, compareDiagnostics, CompilerOptions, - concatenate, ConciseBody, ConstructorDeclaration, ConstructorTypeNode, @@ -64,7 +62,6 @@ import { filter, find, flatMap, - forEach, ForInitializer, ForInOrOfStatement, FunctionBody, @@ -84,10 +81,7 @@ import { getJSDocCommentsAndTags, getJSDocRoot, getJSDocTypeParameterDeclarations, - getLeadingCommentRanges, - getLeadingCommentRangesOfNode, getSourceFileOfNode, - getTrailingCommentRanges, hasAccessorModifier, HasDecorators, hasDecorators, @@ -140,6 +134,7 @@ import { isJSDocEnumTag, isJSDocFunctionType, isJSDocImplementsTag, + isJSDocInternalTag, isJSDocOverloadTag, isJSDocOverrideTag, isJSDocParameterTag, @@ -185,6 +180,7 @@ import { JSDocDeprecatedTag, JSDocEnumTag, JSDocImplementsTag, + JSDocInternalTag, JSDocLink, JSDocLinkCode, JSDocLinkPlain, @@ -211,7 +207,6 @@ import { JsxTagNameExpression, KeywordSyntaxKind, LabeledStatement, - last, lastOrUndefined, LeftHandSideExpression, length, @@ -266,7 +261,6 @@ import { setUILocale, SignatureDeclaration, skipOuterExpressions, - skipTrivia, some, sortAndDeduplicate, SortedReadonlyArray, @@ -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. @@ -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); } diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 3c7a59ea0da74..e1df89167daa3 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -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, @@ -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, @@ -7706,6 +7711,8 @@ declare namespace ts { updateJSDocSatisfiesTag(node: JSDocSatisfiesTag, tagName: Identifier | undefined, typeExpression: JSDocTypeExpression, comment: string | NodeArray | undefined): JSDocSatisfiesTag; createJSDocImportTag(tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes?: ImportAttributes, comment?: string | NodeArray): JSDocImportTag; updateJSDocImportTag(node: JSDocImportTag, tagName: Identifier | undefined, importClause: ImportClause | undefined, moduleSpecifier: Expression, attributes: ImportAttributes | undefined, comment: string | NodeArray | undefined): JSDocImportTag; + createJSDocInternalTag(tagName: Identifier | undefined, comment?: string | NodeArray): JSDocInternalTag; + updateJSDocInternalTag(node: JSDocInternalTag, tagName: Identifier | undefined, comment: string | NodeArray | undefined): JSDocInternalTag; createJSDocText(text: string): JSDocText; updateJSDocText(node: JSDocText, text: string): JSDocText; createJSDocComment(comment?: string | NodeArray | undefined, tags?: readonly JSDocTag[] | undefined): JSDoc; @@ -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; /** @@ -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; @@ -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; diff --git a/tests/baselines/reference/declarationEmitWorkWithInlineComments.js b/tests/baselines/reference/declarationEmitWorkWithInlineComments.js index 227121b0db4fb..a47e986d0a8d7 100644 --- a/tests/baselines/reference/declarationEmitWorkWithInlineComments.js +++ b/tests/baselines/reference/declarationEmitWorkWithInlineComments.js @@ -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( @@ -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); } diff --git a/tests/baselines/reference/stripInternal1.js b/tests/baselines/reference/stripInternal1.js index db4bb57fbefa4..b95645b932784 100644 --- a/tests/baselines/reference/stripInternal1.js +++ b/tests/baselines/reference/stripInternal1.js @@ -21,4 +21,5 @@ var C = /** @class */ (function () { //// [stripInternal1.d.ts] declare class C { foo(): void; + bar(): void; } diff --git a/tests/baselines/reference/stripInternal2.js b/tests/baselines/reference/stripInternal2.js new file mode 100644 index 0000000000000..641b5fc81347d --- /dev/null +++ b/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; +} diff --git a/tests/baselines/reference/stripInternal2.symbols b/tests/baselines/reference/stripInternal2.symbols new file mode 100644 index 0000000000000..bb7d996b9b294 --- /dev/null +++ b/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)) +} diff --git a/tests/baselines/reference/stripInternal2.types b/tests/baselines/reference/stripInternal2.types new file mode 100644 index 0000000000000..927a47c769532 --- /dev/null +++ b/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 +} diff --git a/tests/cases/compiler/stripInternal2.ts b/tests/cases/compiler/stripInternal2.ts new file mode 100644 index 0000000000000..9c589d0bedeef --- /dev/null +++ b/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; +} \ No newline at end of file From 943263e489207cef623be3a653141e168734600e Mon Sep 17 00:00:00 2001 From: Zzzen Date: Thu, 28 Mar 2024 01:00:40 +0800 Subject: [PATCH 2/3] Update utilitiesPublic.ts Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com> --- src/compiler/utilitiesPublic.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index b41934d2fe8e6..37b5dde8b44f6 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -2597,5 +2597,5 @@ export function isRestParameter(node: ParameterDeclaration | JSDocParameterTag): export function isInternalDeclaration(node: Node, sourceFile?: SourceFile) { sourceFile ??= getSourceFileOfNode(node); const parseTreeNode = getParseTreeNode(node); - return parseTreeNode && !!getJSDocInternalTag(parseTreeNode); + return !!parseTreeNode && !!getJSDocInternalTag(parseTreeNode); } From fec903e41f341f0910d8f0d1325ce5117cd0de44 Mon Sep 17 00:00:00 2001 From: Zzzen Date: Thu, 28 Mar 2024 01:32:53 +0800 Subject: [PATCH 3/3] update baseline --- tests/baselines/reference/api/typescript.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index e1df89167daa3..1332029fc24d5 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -8702,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 | undefined; + function isInternalDeclaration(node: Node, sourceFile?: SourceFile): boolean; const unchangedTextChangeRange: TextChangeRange; type ParameterPropertyDeclaration = ParameterDeclaration & { parent: ConstructorDeclaration;