@@ -186,6 +186,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
186
186
/** Sourcemap data that will get encoded */
187
187
let sourceMapData : SourceMapData ;
188
188
189
+ /** If removeComments is true, no leading-comments needed to be emitted **/
190
+ let emitLeadingCommentsOfPosition = compilerOptions . removeComments ? function ( pos : number ) { } : emitLeadingCommentsOfPositionWorker ;
191
+
189
192
if ( compilerOptions . sourceMap || compilerOptions . inlineSourceMap ) {
190
193
initializeEmitterWithSourceMaps ( ) ;
191
194
}
@@ -3669,7 +3672,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
3669
3672
3670
3673
function emitFunctionDeclaration ( node : FunctionLikeDeclaration ) {
3671
3674
if ( nodeIsMissing ( node . body ) ) {
3672
- return emitOnlyPinnedOrTripleSlashComments ( node ) ;
3675
+ return emitCommentsOnNotEmittedNode ( node ) ;
3673
3676
}
3674
3677
3675
3678
// TODO (yuisu) : we should not have special cases to condition emitting comments
@@ -4146,7 +4149,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
4146
4149
}
4147
4150
else if ( member . kind === SyntaxKind . MethodDeclaration || node . kind === SyntaxKind . MethodSignature ) {
4148
4151
if ( ! ( < MethodDeclaration > member ) . body ) {
4149
- return emitOnlyPinnedOrTripleSlashComments ( member ) ;
4152
+ return emitCommentsOnNotEmittedNode ( member ) ;
4150
4153
}
4151
4154
4152
4155
writeLine ( ) ;
@@ -4213,7 +4216,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
4213
4216
function emitMemberFunctionsForES6AndHigher ( node : ClassLikeDeclaration ) {
4214
4217
for ( let member of node . members ) {
4215
4218
if ( ( member . kind === SyntaxKind . MethodDeclaration || node . kind === SyntaxKind . MethodSignature ) && ! ( < MethodDeclaration > member ) . body ) {
4216
- emitOnlyPinnedOrTripleSlashComments ( member ) ;
4219
+ emitCommentsOnNotEmittedNode ( member ) ;
4217
4220
}
4218
4221
else if ( member . kind === SyntaxKind . MethodDeclaration ||
4219
4222
member . kind === SyntaxKind . GetAccessor ||
@@ -4270,7 +4273,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
4270
4273
// Emit the constructor overload pinned comments
4271
4274
forEach ( node . members , member => {
4272
4275
if ( member . kind === SyntaxKind . Constructor && ! ( < ConstructorDeclaration > member ) . body ) {
4273
- emitOnlyPinnedOrTripleSlashComments ( member ) ;
4276
+ emitCommentsOnNotEmittedNode ( member ) ;
4274
4277
}
4275
4278
// Check if there is any non-static property assignment
4276
4279
if ( member . kind === SyntaxKind . PropertyDeclaration && ( < PropertyDeclaration > member ) . initializer && ( member . flags & NodeFlags . Static ) === 0 ) {
@@ -5169,7 +5172,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
5169
5172
}
5170
5173
5171
5174
function emitInterfaceDeclaration ( node : InterfaceDeclaration ) {
5172
- emitOnlyPinnedOrTripleSlashComments ( node ) ;
5175
+ emitCommentsOnNotEmittedNode ( node ) ;
5173
5176
}
5174
5177
5175
5178
function shouldEmitEnumDeclaration ( node : EnumDeclaration ) {
@@ -5291,7 +5294,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
5291
5294
let shouldEmit = shouldEmitModuleDeclaration ( node ) ;
5292
5295
5293
5296
if ( ! shouldEmit ) {
5294
- return emitOnlyPinnedOrTripleSlashComments ( node ) ;
5297
+ return emitCommentsOnNotEmittedNode ( node ) ;
5295
5298
}
5296
5299
let hoistedInDeclarationScope = shouldHoistDeclarationInSystemJsModule ( node ) ;
5297
5300
let emitVarForModule = ! hoistedInDeclarationScope && ! isModuleMergedWithES6Class ( node ) ;
@@ -6721,7 +6724,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
6721
6724
function emitNodeConsideringCommentsOption ( node : Node , emitNodeConsideringSourcemap : ( node : Node ) => void ) : void {
6722
6725
if ( node ) {
6723
6726
if ( node . flags & NodeFlags . Ambient ) {
6724
- return emitOnlyPinnedOrTripleSlashComments ( node ) ;
6727
+ return emitCommentsOnNotEmittedNode ( node ) ;
6725
6728
}
6726
6729
6727
6730
if ( isSpecializedCommentHandling ( node ) ) {
@@ -6988,22 +6991,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
6988
6991
return leadingComments ;
6989
6992
}
6990
6993
6994
+ function isPinnedComments ( comment : CommentRange ) {
6995
+ return currentSourceFile . text . charCodeAt ( comment . pos + 1 ) === CharacterCodes . asterisk &&
6996
+ currentSourceFile . text . charCodeAt ( comment . pos + 2 ) === CharacterCodes . exclamation ;
6997
+ }
6998
+
6991
6999
/**
6992
- * Removes all but the pinned or triple slash comments.
6993
- * @param ranges The array to be filtered
6994
- * @param onlyPinnedOrTripleSlashComments whether the filtering should be performed.
6995
- */
6996
- function filterComments ( ranges : CommentRange [ ] , onlyPinnedOrTripleSlashComments : boolean ) : CommentRange [ ] {
6997
- // If we're removing comments, then we want to strip out all but the pinned or
6998
- // triple slash comments.
6999
- if ( ranges && onlyPinnedOrTripleSlashComments ) {
7000
- ranges = filter ( ranges , isPinnedOrTripleSlashComment ) ;
7001
- if ( ranges . length === 0 ) {
7002
- return undefined ;
7003
- }
7000
+ * Determine if the given comment is a triple-slash
7001
+ *
7002
+ * @return true if the comment is a triple-slash comment else false
7003
+ **/
7004
+ function isTripleSlashComment ( comment : CommentRange ) {
7005
+ // Verify this is /// comment, but do the regexp match only when we first can find /// in the comment text
7006
+ // so that we don't end up computing comment string and doing match for all // comments
7007
+ if ( currentSourceFile . text . charCodeAt ( comment . pos + 1 ) === CharacterCodes . slash &&
7008
+ comment . pos + 2 < comment . end &&
7009
+ currentSourceFile . text . charCodeAt ( comment . pos + 2 ) === CharacterCodes . slash ) {
7010
+ let textSubStr = currentSourceFile . text . substring ( comment . pos , comment . end ) ;
7011
+ return textSubStr . match ( fullTripleSlashReferencePathRegEx ) ||
7012
+ textSubStr . match ( fullTripleSlashAMDReferencePathRegEx ) ?
7013
+ true : false ;
7004
7014
}
7005
-
7006
- return ranges ;
7015
+ return false ;
7007
7016
}
7008
7017
7009
7018
function getLeadingCommentsToEmit ( node : Node ) {
@@ -7031,28 +7040,53 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
7031
7040
}
7032
7041
}
7033
7042
7034
- function emitOnlyPinnedOrTripleSlashComments ( node : Node ) {
7035
- emitLeadingCommentsWorker ( node , /*onlyPinnedOrTripleSlashComments:*/ true ) ;
7043
+ /**
7044
+ * Emit comments associated with node that will not be emitted into JS file
7045
+ */
7046
+ function emitCommentsOnNotEmittedNode ( node : Node ) {
7047
+ emitLeadingCommentsWorker ( node , /*isEmittedNode:*/ false ) ;
7036
7048
}
7037
7049
7038
7050
function emitLeadingComments ( node : Node ) {
7039
- return emitLeadingCommentsWorker ( node , /*onlyPinnedOrTripleSlashComments :*/ compilerOptions . removeComments ) ;
7051
+ return emitLeadingCommentsWorker ( node , /*isEmittedNode :*/ true ) ;
7040
7052
}
7041
7053
7042
-
10000
function emitLeadingCommentsWorker ( node : Node , onlyPinnedOrTripleSlashComments : boolean ) {
7043
- // If the caller only wants pinned or triple slash comments, then always filter
7044
- // down to that set. Otherwise, filter based on the current compiler options.
7045
- let leadingComments = filterComments ( getLeadingCommentsToEmit ( node ) , onlyPinnedOrTripleSlashComments ) ;
7054
+ function emitLeadingCommentsWorker ( node : Node , isEmittedNode : boolean ) {
7055
+ if ( compilerOptions . removeComments ) {
7056
+ return ;
7057
+ }
7058
+
7059
+ let leadingComments : CommentRange [ ] ;
7060
+ if ( isEmittedNode ) {
7061
+ leadingComments = getLeadingCommentsToEmit ( node ) ;
7062
+ }
7063
+ else {
7064
+ // If the node will not be emitted in JS, remove all the comments(normal, pinned and ///) associated with the node,
7065
+ // unless it is a triple slash comment at the top of the file.
7066
+ // For Example:
7067
+ // /// <reference-path ...>
7068
+ // declare var x;
7069
+ // /// <reference-path ...>
7070
+ // interface F {}
7071
+ // The first /// will NOT be removed while the second one will be removed eventhough both node will not be emitted
7072
+ if ( node . pos === 0 ) {
7073
+ leadingComments = filter ( getLeadingCommentsToEmit ( node ) , isTripleSlashComment ) ;
7074
+ }
7075
+ }
7046
7076
7047
7077
emitNewLineBeforeLeadingComments ( currentSourceFile , writer , node , leadingComments ) ;
7048
7078
7049
7079
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
7050
- emitComments ( currentSourceFile , writer , leadingComments , /*trailingSeparator*/ true , newLine , writeComment ) ;
7080
+ emitComments ( currentSourceFile , writer , leadingComments , /*trailingSeparator: */ true , newLine , writeComment ) ;
7051
7081
}
7052
7082
7053
7083
function emitTrailingComments ( node : Node ) {
7084
+ if ( compilerOptions . removeComments ) {
7085
+ return ;
7086
+ }
7087
+
7054
7088
// Emit the trailing comments only if the parent's end doesn't match
7055
- let trailingComments = filterComments ( getTrailingCommentsToEmit ( node ) , /*onlyPinnedOrTripleSlashComments:*/ compilerOptions . removeComments ) ;
7089
+ let trailingComments = getTrailingCommentsToEmit ( node ) ;
7056
7090
7057
7091
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
7058
7092
emitComments ( currentSourceFile , writer , trailingComments , /*trailingSeparator*/ false , newLine , writeComment ) ;
@@ -7064,13 +7098,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
7064
7098
* ^ => pos; the function will emit "comment1" in the emitJS
7065
7099
*/
7066
7100
function emitTrailingCommentsOfPosition ( pos : number ) {
7067
- let trailingComments = filterComments ( getTrailingCommentRanges ( currentSourceFile . text , pos ) , /*onlyPinnedOrTripleSlashComments:*/ compilerOptions . removeComments ) ;
7101
+ if ( compilerOptions . removeComments ) {
7102
+ return ;
7103
+ }
7104
+
7105
+ let trailingComments = getTrailingCommentRanges ( currentSourceFile . text , pos ) ;
7068
7106
7069
7107
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
7070
7108
emitComments ( currentSourceFile , writer , trailingComments , /*trailingSeparator*/ true , newLine , writeComment ) ;
7071
7109
}
7072
7110
7073
- function emitLeadingCommentsOfPosition ( pos : number ) {
7111
+ function emitLeadingCommentsOfPositionWorker ( pos : number ) {
7112
+ if ( compilerOptions . removeComments ) {
7113
+ return ;
7114
+ }
7115
+
7074
7116
let leadingComments : CommentRange [ ] ;
7075
7117
if ( hasDetachedComments ( pos ) ) {
7076
7118
// get comments without detached comments
@@ -7081,15 +7123,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
7081
7123
leadingComments = getLeadingCommentRanges ( currentSourceFile . text , pos ) ;
7082
7124
}
7083
7125
7084
- leadingComments = filterComments ( leadingComments , compilerOptions . removeComments ) ;
7085
7126
emitNewLineBeforeLeadingComments ( currentSourceFile , writer , { pos : pos , end : pos } , leadingComments ) ;
7086
7127
7087
7128
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
7088
7129
emitComments ( currentSourceFile , writer , leadingComments , /*trailingSeparator*/ true , newLine , writeComment ) ;
7089
7130
}
7090
7131
7091
7132
function emitDetachedComments ( node : TextRange ) {
7092
- let leadingComments = getLeadingCommentRanges ( currentSourceFile . text , node . pos ) ;
7133
+ let leadingComments : CommentRange [ ] ;
7134
+ if ( compilerOptions . removeComments ) {
7135
+ // removeComments is true, only reserve pinned comment at the top of file
7136
+ // For example:
7137
+ // /*! Pinned Comment */
7138
+ //
7139
+ // var x = 10;
7140
+ if ( node . pos === 0 ) {
7141
+ leadingComments = filter ( getLeadingCommentRanges ( currentSourceFile . text , node . pos ) , isPinnedComments ) ;
7142
+ }
7143
+ }
7144
+ else {
7145
+ // removeComments is false, just get detached as normal and bypass the process to filter comment
7146
+ leadingComments = getLeadingCommentRanges ( currentSourceFile . text , node . pos ) ;
7147
+ }
7148
+
7093
7149
if ( leadingComments ) {
7094
7150
let detachedComments : CommentRange [ ] = [ ] ;
7095
7151
let lastComment : CommentRange ;
@@ -7139,20 +7195,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
7139
7195
write ( shebang ) ;
7140
7196
}
7141
7197
}
7142
-
7143
- function isPinnedOrTripleSlashComment ( comment : CommentRange ) {
7144
- if ( currentSourceFile . text . charCodeAt ( comment . pos + 1 ) === CharacterCodes . asterisk ) {
7145
- return currentSourceFile . text . charCodeAt ( comment . pos + 2 ) === CharacterCodes . exclamation ;
7146
- }
7147
- // Verify this is /// comment, but do the regexp match only when we first can find /// in the comment text
7148
- // so that we don't end up computing comment string and doing match for all // comments
7149
- else if ( currentSourceFile . text . charCodeAt ( comment . pos + 1 ) === CharacterCodes . slash &&
7150
- comment . pos + 2 < comment . end &&
7151
- currentSourceFile . text . charCodeAt ( comment . pos + 2 ) === CharacterCodes . slash &&
7152
- currentSourceFile . text . substring ( comment . pos , comment . end ) . match ( fullTripleSlashReferencePathRegEx ) ) {
7153
- return true ;
7154
- }
7155
- }
7156
7198
}
7157
7199
7158
7200
function emitFile ( jsFilePath : string , sourceFile ?: SourceFile ) {
0 commit comments