@@ -2,6 +2,9 @@ package declarations
22
33import (
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
6770func 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+
89161const declarationEmitNodeBuilderFlags = nodebuilder .FlagsMultilineObjectLiterals |
90162 nodebuilder .FlagsWriteClassExpressionAsTypeLiteral |
91163 nodebuilder .FlagsUseTypeOfFunction |
@@ -377,8 +449,9 @@ func (tx *DeclarationTransformer) getTypeReferences() (result []*ast.FileReferen
377449}
378450
379451func (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
905978func (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 (
0 commit comments