Skip to content

Commit d876d05

Browse files
authored
Port --stripInternal (#2267)
1 parent 3fae7ba commit d876d05

File tree

6 files changed

+88
-47
lines changed

6 files changed

+88
-47
lines changed

internal/transformers/declarations/tracker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ type SymbolTrackerSharedState struct {
204204
getSymbolAccessibilityDiagnostic GetSymbolAccessibilityDiagnostic
205205
errorNameNode *ast.Node
206206
isolatedDeclarations bool
207+
stripInternal bool
207208
currentSourceFile *ast.SourceFile
208209
resolver printer.EmitResolver
209210
reportExpandoFunctionErrors func(node *ast.Node)

internal/transformers/declarations/transform.go

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package declarations
22

33
import (
44
"fmt"
5+
"iter"
6+
"slices"
7+
"strings"
58

69
"github.com/microsoft/typescript-go/internal/ast"
710
"github.com/microsoft/typescript-go/internal/collections"
@@ -66,7 +69,7 @@ type DeclarationTransformer struct {
6669
// TODO: Convert to transformers.TransformerFactory signature to allow more automatic composition with other transforms
6770
func NewDeclarationTransformer(host DeclarationEmitHost, context *printer.EmitContext, compilerOptions *core.CompilerOptions, declarationFilePath string, declarationMapPath string) *DeclarationTransformer {
6871
resolver := host.GetEmitResolver()
69-
state := &SymbolTrackerSharedState{isolatedDeclarations: compilerOptions.IsolatedDeclarations.IsTrue(), resolver: resolver}
72+
state := &SymbolTrackerSharedState{isolatedDeclarations: compilerOptions.IsolatedDeclarations.IsTrue(), stripInternal: compilerOptions.StripInternal.IsTrue(), resolver: resolver}
7073
tracker := NewSymbolTracker(host, resolver, state)
7174
// TODO: Use new host GetOutputPathsFor method instead of passing in entrypoint paths (which will also better support bundled emit)
7275
tx := &DeclarationTransformer{
@@ -86,6 +89,75 @@ func (tx *DeclarationTransformer) GetDiagnostics() []*ast.Diagnostic {
8689
return tx.state.diagnostics
8790
}
8891

92+
func (tx *DeclarationTransformer) shouldStripInternal(node *ast.Node) bool {
93+
return tx.state.stripInternal && node != nil && tx.isInternalDeclaration(node, tx.state.currentSourceFile)
94+
}
95+
96+
func (tx *DeclarationTransformer) isInternalDeclaration(node *ast.Node, sourceFile *ast.SourceFile) bool {
97+
if node == nil {
98+
return false
99+
}
100+
parseTreeNode := tx.EmitContext().MostOriginal(node)
101+
if !ast.IsParseTreeNode(parseTreeNode) {
102+
return false
103+
}
104+
if parseTreeNode.Kind == ast.KindParameter {
105+
params := parseTreeNode.Parent.Parameters()
106+
paramIdx := slices.IndexFunc(params, func(p *ast.ParameterDeclarationNode) bool {
107+
return p.AsNode() == parseTreeNode
108+
})
109+
var previousSibling *ast.Node
110+
if paramIdx > 0 {
111+
previousSibling = params[paramIdx-1].AsNode()
112+
}
113+
114+
text := sourceFile.Text()
115+
var commentRanges []ast.CommentRange
116+
117+
if previousSibling != nil {
118+
// to handle
119+
// ... parameters, /** @internal */
120+
// public param: string
121+
trailingPos := scanner.SkipTriviaEx(text, previousSibling.End()+1, &scanner.SkipTriviaOptions{StopAtComments: true})
122+
for comment := range scanner.GetTrailingCommentRanges(tx.Factory().AsNodeFactory(), text, trailingPos) {
123+
commentRanges = append(commentRanges, comment)
124+
}
125+
for comment := range scanner.GetLeadingCommentRanges(tx.Factory().AsNodeFactory(), text, node.Pos()) {
126+
commentRanges = append(commentRanges, comment)
127+
}
128+
} else {
129+
trailingPos := scanner.SkipTriviaEx(text, node.Pos(), &scanner.SkipTriviaOptions{StopAtComments: true})
130+
for comment := range scanner.GetTrailingCommentRanges(tx.Factory().AsNodeFactory(), text, trailingPos) {
131+
commentRanges = append(commentRanges, comment)
132+
}
133+
}
134+
135+
if len(commentRanges) > 0 {
136+
return hasInternalAnnotation(commentRanges[len(commentRanges)-1], sourceFile)
137+
}
138+
return false
139+
}
140+
141+
for commentRange := range tx.getLeadingCommentRangesOfNode(parseTreeNode, sourceFile) {
142+
if hasInternalAnnotation(commentRange, sourceFile) {
143+
return true
144+
}
145+
}
146+
return false
147+
}
148+
149+
func (tx *DeclarationTransformer) getLeadingCommentRangesOfNode(node *ast.Node, sourceFile *ast.SourceFile) iter.Seq[ast.CommentRange] {
150+
if node == nil || node.Kind == ast.KindJsxText {
151+
return nil
152+
}
153+
return scanner.GetLeadingCommentRanges(tx.Factory().AsNodeFactory(), sourceFile.Text(), node.Pos())
154+
}
155+
156+
func hasInternalAnnotation(commentRange ast.CommentRange, sourceFile *ast.SourceFile) bool {
157+
comment := sourceFile.Text()[commentRange.Pos():commentRange.End()]
158+
return strings.Contains(comment, "@internal")
159+
}
160+
89161
const declarationEmitNodeBuilderFlags = nodebuilder.FlagsMultilineObjectLiterals |
90162
nodebuilder.FlagsWriteClassExpressionAsTypeLiteral |
91163
nodebuilder.FlagsUseTypeOfFunction |
@@ -377,8 +449,9 @@ func (tx *DeclarationTransformer) getTypeReferences() (result []*ast.FileReferen
377449
}
378450

379451
func (tx *DeclarationTransformer) visitDeclarationSubtree(input *ast.Node) *ast.Node {
380-
// !!! TODO: stripInternal support?
381-
// if (shouldStripInternal(input)) return nil
452+
if tx.shouldStripInternal(input) {
453+
return nil
454+
}
382455
if ast.IsDeclaration(input) {
383456
if isDeclarationAndNotVisible(tx.EmitContext(), tx.resolver, input) {
384457
return nil
@@ -903,8 +976,9 @@ func (tx *DeclarationTransformer) transformMethodDeclaration(input *ast.MethodDe
903976
}
904977

905978
func (tx *DeclarationTransformer) visitDeclarationStatements(input *ast.Node) *ast.Node {
906-
// !!! TODO: stripInternal support?
907-
// if (shouldStripInternal(input)) return nil
979+
if tx.shouldStripInternal(input) {
980+
return nil
981+
}
908982
switch input.Kind {
909983
case ast.KindExportDeclaration:
910984
if ast.IsSourceFile(input.Parent) {
@@ -1132,8 +1206,9 @@ func (tx *DeclarationTransformer) transformTopLevelDeclaration(input *ast.Node)
11321206
// Remove duplicates of the current statement from the deferred work queue (this was done via orderedRemoveItem in strada - why? to ensure the same backing array? microop?)
11331207
tx.state.lateMarkedStatements = core.Filter(tx.state.lateMarkedStatements, func(node *ast.Node) bool { return node != input })
11341208
}
1135-
// !!! TODO: stripInternal support?
1136-
// if (shouldStripInternal(input)) return;
1209+
if tx.shouldStripInternal(input) {
1210+
return nil
1211+
}
11371212
if input.Kind == ast.KindImportEqualsDeclaration {
11381213
return tx.transformImportEqualsDeclaration(input.AsImportEqualsDeclaration())
11391214
}
@@ -1338,11 +1413,9 @@ func (tx *DeclarationTransformer) transformClassDeclaration(input *ast.ClassDecl
13381413
if ctor != nil {
13391414
oldDiag := tx.state.getSymbolAccessibilityDiagnostic
13401415
for _, param := range ctor.AsConstructorDeclaration().Parameters.Nodes {
1341-
if !ast.HasSyntacticModifier(param, ast.ModifierFlagsParameterPropertyModifier) {
1416+
if !ast.HasSyntacticModifier(param, ast.ModifierFlagsParameterPropertyModifier) || tx.shouldStripInternal(param) {
13421417
continue
13431418
}
1344-
// !!! TODO: stripInternal support?
1345-
// if (shouldStripInternal(param)) { continue }
13461419
tx.state.getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(param)
13471420
if param.Name().Kind == ast.KindIdentifier {
13481421
updated := tx.Factory().NewPropertyDeclaration(
@@ -1505,8 +1578,9 @@ func (tx *DeclarationTransformer) transformEnumDeclaration(input *ast.EnumDeclar
15051578
tx.ensureModifiers(input.AsNode()),
15061579
input.Name(),
15071580
tx.Factory().NewNodeList(core.MapNonNil(input.Members.Nodes, func(m *ast.Node) *ast.Node {
1508-
// !!! TODO: stripInternal support?
1509-
// if (shouldStripInternal(m)) return;
1581+
if tx.shouldStripInternal(m) {
1582+
return nil
1583+
}
15101584

15111585
// !!! TODO: isolatedDeclarations support
15121586
// if (

testdata/baselines/reference/submodule/compiler/stripInternal1.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ class C {
1818
//// [stripInternal1.d.ts]
1919
declare class C {
2020
foo(): void;
21-
bar(): void;
2221
}

testdata/baselines/reference/submodule/compiler/stripInternal1.js.diff

Lines changed: 0 additions & 8 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/declarationEmitWorkWithInlineComments.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ exports.Baz = Baz;
102102

103103
//// [declarationEmitWorkWithInlineComments.d.ts]
104104
export declare class Foo {
105-
isInternal1: string;
106-
isInternal2: string;
107-
isInternal3: string;
108-
isInternal4: string;
109-
isInternal5: string;
110-
isInternal6: string;
111-
isInternal7: string;
112105
notInternal1: string;
113106
notInternal2: string;
114107
notInternal3: string;
@@ -120,10 +113,8 @@ export declare class Foo {
120113
isInternal5: string, isInternal6: string, isInternal7: string, /** @internal */ notInternal1: string, notInternal2: string, notInternal3: string);
121114
}
122115
export declare class Bar {
123-
isInternal1: string;
124116
constructor(isInternal1: string);
125117
}
126118
export declare class Baz {
127-
isInternal: string;
128119
constructor(isInternal: string);
129120
}

testdata/baselines/reference/submodule/conformance/declarationEmitWorkWithInlineComments.js.diff

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,14 @@
3232
constructor(/* @internal */ isInternal) {
3333
this.isInternal = isInternal;
3434
}
35-
@@= skipped -15, +17 lines =@@
36-
37-
//// [declarationEmitWorkWithInlineComments.d.ts]
38-
export declare class Foo {
39-
+ isInternal1: string;
40-
+ isInternal2: string;
41-
+ isInternal3: string;
42-
+ isInternal4: string;
43-
+ isInternal5: string;
44-
+ isInternal6: string;
45-
+ isInternal7: string;
46-
notInternal1: string;
47-
notInternal2: string;
48-
notInternal3: string;
49-
@@= skipped -11, +18 lines =@@
35+
@@= skipped -26, +28 lines =@@
5036
isInternal5: string, isInternal6: string, isInternal7: string, /** @internal */ notInternal1: string, notInternal2: string, notInternal3: string);
5137
}
5238
export declare class Bar {
5339
- constructor(/* @internal */ isInternal1: string);
54-
+ isInternal1: string;
5540
+ constructor(isInternal1: string);
5641
}
5742
export declare class Baz {
5843
- constructor(/* @internal */ isInternal: string);
59-
+ isInternal: string;
6044
+ constructor(isInternal: string);
6145
}

0 commit comments

Comments
 (0)