@@ -10,7 +10,7 @@ import * as ts from 'typescript';
10
10
11
11
import * as jsdoc from './jsdoc' ;
12
12
import * as path from './path' ;
13
- import { createNotEmittedStatement , reportDiagnostic , synthesizeCommentRanges , updateSourceFileNode } from './transformer_util' ;
13
+ import { reportDiagnostic , synthesizeCommentRanges , updateSourceFileNode } from './transformer_util' ;
14
14
15
15
/**
16
16
* A set of JSDoc tags that mark a comment as a fileoverview comment. These are
@@ -152,35 +152,34 @@ export function transformFileoverviewCommentFactory(
152
152
// they do not get lost later on.
153
153
const synthesizedComments =
154
154
jsdoc . synthesizeLeadingComments ( firstStatement ) ;
155
- const notEmitted = ts . factory . createNotEmittedStatement ( sourceFile ) ;
156
155
// Modify the comments on the firstStatement in place by removing the
157
156
// file-level comments.
158
157
fileComments = synthesizedComments . splice ( 0 , i + 1 ) ;
159
- // Move the fileComments onto notEmitted.
160
- ts . setSyntheticLeadingComments ( notEmitted , fileComments ) ;
161
- sourceFile =
162
- updateSourceFileNode ( sourceFile , ts . factory . createNodeArray ( [
163
- notEmitted , firstStatement , ...sourceFile . statements . slice ( 1 )
164
- ] ) ) ;
165
158
break ;
166
159
}
160
+ }
167
161
168
-
169
- // Now walk every top level statement and escape/drop any @fileoverview
170
- // comments found. Closure ignores all @fileoverview comments but the
171
- // last, so tsickle must make sure not to emit duplicated ones.
172
- for ( let i = 0 ; i < sourceFile . statements . length ; i ++ ) {
173
- const stmt = sourceFile . statements [ i ] ;
174
- // Accept the NotEmittedStatement inserted above.
175
- if ( i === 0 && stmt . kind === ts . SyntaxKind . NotEmittedStatement ) {
176
- continue ;
177
- }
178
- const comments = jsdoc . synthesizeLeadingComments ( stmt ) ;
179
- checkNoFileoverviewComments (
180
- stmt , comments ,
181
- `file comments must be at the top of the file, ` +
182
- `separated from the file body by an empty line.` ) ;
162
+ // Move the fileComments onto notEmitted.
163
+ const notEmitted = ts . factory . createNotEmittedStatement ( sourceFile ) ;
164
+ ts . setSyntheticLeadingComments ( notEmitted , fileComments ) ;
165
+ sourceFile = updateSourceFileNode (
166
+ sourceFile ,
167
+ ts . factory . createNodeArray ( [ notEmitted , ... sourceFile . statements ] ) ) ;
168
+
169
+ // Now walk every top level statement and escape/drop any @fileoverview
170
+ // comments found. Closure ignores all @fileoverview comments but the
171
+ // last, so tsickle must make sure not to emit duplicated ones.
172
+ for ( let i = 0 ; i < sourceFile . statements . length ; i ++ ) {
173
+ const stmt = sourceFile . statements [ i ] ;
174
+ // Accept the NotEmittedStatement inserted above.
175
+ if ( i === 0 && stmt . kind === ts . SyntaxKind . NotEmittedStatement ) {
176
+ continue ;
183
177
}
178
+ const comments = jsdoc . synthesizeLeadingComments ( stmt ) ;
179
+ checkNoFileoverviewComments (
180
+ stmt , comments ,
181
+ `file comments must be at the top of the file, ` +
182
+ `separated from the file body by an empty line.` ) ;
184
183
}
185
184
186
185
// Closure Compiler considers the *last* comment with @fileoverview (or
@@ -192,14 +191,17 @@ export function transformFileoverviewCommentFactory(
192
191
let fileoverviewIdx = - 1 ;
193
192
let tags : jsdoc . Tag [ ] = [ ] ;
194
193
for ( let i = fileComments . length - 1 ; i >= 0 ; i -- ) {
195
- const parse = jsdoc . parseContents ( fileComments [ i ] . text ) ;
196
- if ( parse !== null &&
197
- parse . tags . some ( t => FILEOVERVIEW_COMMENT_MARKERS . has ( t . tagName ) ) ) {
194
+ const parsed = jsdoc . parse ( fileComments [ i ] ) ;
195
+ if ( parsed !== null &&
196
+ parsed . tags . some (
197
+ t => FILEOVERVIEW_COMMENT_MARKERS . has ( t . tagName ) ) ) {
198
198
fileoverviewIdx = i ;
199
- tags = parse . tags ;
199
+ tags = parsed . tags ;
200
200
break ;
201
201
}
202
202
}
203
+ const mutableJsDoc = new jsdoc . MutableJSDoc (
204
+ notEmitted , fileComments , fileoverviewIdx , tags ) ;
203
205
204
206
if ( fileoverviewIdx !== - 1 ) {
205
207
checkNoFileoverviewComments (
@@ -208,28 +210,11 @@ export function transformFileoverviewCommentFactory(
208
210
`duplicate file level comment` ) ;
209
211
}
210
212
211
- augmentFileoverviewComments ( options , sourceFile , tags , generateExtraSuppressions ) ;
212
- const commentText = jsdoc . toStringWithoutStartEnd ( tags ) ;
213
-
214
- if ( fileoverviewIdx < 0 ) {
215
- // No existing comment to merge with, just emit a new one.
216
- return addNewFileoverviewComment ( sourceFile , commentText ) ;
217
- }
213
+ augmentFileoverviewComments (
214
+ options , sourceFile , mutableJsDoc . tags , generateExtraSuppressions ) ;
215
+ mutableJsDoc . updateComment ( ) ;
218
216
219
- fileComments [ fileoverviewIdx ] . text = commentText ;
220
- // sf does not need to be updated, synthesized comments are mutable.
221
217
return sourceFile ;
222
218
} ;
223
219
} ;
224
220
}
225
-
226
- function addNewFileoverviewComment (
227
- sf : ts . SourceFile , commentText : string ) : ts . SourceFile {
228
- let syntheticFirstStatement = createNotEmittedStatement ( sf ) ;
229
- syntheticFirstStatement = ts . addSyntheticTrailingComment (
230
- syntheticFirstStatement , ts . SyntaxKind . MultiLineCommentTrivia ,
231
- commentText , true ) ;
232
- return updateSourceFileNode (
233
- sf ,
234
- ts . factory . createNodeArray ( [ syntheticFirstStatement , ...sf . statements ] ) ) ;
235
- }
0 commit comments