Skip to content

Commit 79dfc47

Browse files
committed
WIP
1 parent 16f69da commit 79dfc47

File tree

3 files changed

+80
-69
lines changed

3 files changed

+80
-69
lines changed

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ namespace ts {
468468
function emitSourceFile(node: SourceFile) {
469469
currentSourceFile = node;
470470
enclosingDeclaration = node;
471+
emitDetachedComments(currentSourceFile, writer, writeCommentRange, node, newLine, true, []);
471472
emitLines(node.statements);
472473
}
473474

src/compiler/emitter.ts

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4907,7 +4907,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
49074907

49084908
increaseIndent();
49094909
let outPos = writer.getTextPos();
4910-
emitDetachedComments(node.body);
4910+
detachedCommentsInfo = emitDetachedComments(currentSourceFile, writer, writeComment, node.body, newLine, compilerOptions.removeComments, detachedCommentsInfo);
49114911
emitFunctionBodyPreamble(node);
49124912
let preambleEmitted = writer.getTextPos() !== outPos;
49134913
decreaseIndent();
@@ -4952,7 +4952,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
49524952
let initialTextPos = writer.getTextPos();
49534953

49544954
increaseIndent();
4955-
emitDetachedComments(body.statements);
4955+
detachedCommentsInfo = emitDetachedComments(currentSourceFile, writer, writeComment, body.statements, newLine, compilerOptions.removeComments, detachedCommentsInfo);
4956+
49564957

49574958
// Emit all the directive prologues (like "use strict"). These have to come before
49584959
// any other preamble code we write (like parameter initializers).
@@ -5274,7 +5275,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
52745275
// Emit all the directive prologues (like "use strict"). These have to come before
52755276
// any other preamble code we write (like parameter initializers).
52765277
startIndex = emitDirectivePrologues(ctor.body.statements, /*startWithNewLine*/ true);
5277-
emitDetachedComments(ctor.body.statements);
5278+
detachedCommentsInfo = emitDetachedComments(currentSourceFile, writer, writeComment, ctor.body.statements, newLine, compilerOptions.removeComments, detachedCommentsInfo);
52785279
}
52795280
emitCaptureThisForNodeIfNecessary(node);
52805281
let superCall: ExpressionStatement;
@@ -7652,7 +7653,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
76527653
// Start new file on new line
76537654
writeLine();
76547655
emitShebang();
7655-
emitDetachedComments(node);
7656+
detachedCommentsInfo = emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, compilerOptions.removeComments, detachedCommentsInfo);
76567657

76577658
if (isExternalModule(node) || compilerOptions.isolatedModules) {
76587659
let emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[ModuleKind.CommonJS];
@@ -7948,11 +7949,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
79487949
return leadingComments;
79497950
}
79507951

7951-
function isPinnedComments(comment: CommentRange) {
7952-
return currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
7953-
currentSourceFile.text.charCodeAt(comment.pos + 2) === CharacterCodes.exclamation;
7954-
}
7955-
79567952
/**
79577953
* Determine if the given comment is a triple-slash
79587954
*
@@ -8086,66 +8082,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
80868082
emitComments(currentSourceFile, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment);
80878083
}
80888084

8089-
function emitDetachedComments(node: TextRange) {
8090-
let leadingComments: CommentRange[];
8091-
if (compilerOptions.removeComments) {
8092-
// removeComments is true, only reserve pinned comment at the top of file
8093-
// For example:
8094-
// /*! Pinned Comment */
8095-
//
8096-
// var x = 10;
8097-
if (node.pos === 0) {
8098-
leadingComments = filter(getLeadingCommentRanges(currentSourceFile.text, node.pos), isPinnedComments);
8099-
}
8100-
}
8101-
else {
8102-
// removeComments is false, just get detached as normal and bypass the process to filter comment
8103-
leadingComments = getLeadingCommentRanges(currentSourceFile.text, node.pos);
8104-
}
8105-
8106-
if (leadingComments) {
8107-
let detachedComments: CommentRange[] = [];
8108-
let lastComment: CommentRange;
8109-
8110-
forEach(leadingComments, comment => {
8111-
if (lastComment) {
8112-
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastComment.end);
8113-
let commentLine = getLineOfLocalPosition(currentSourceFile, comment.pos);
8114-
8115-
if (commentLine >= lastCommentLine + 2) {
8116-
// There was a blank line between the last comment and this comment. This
8117-
// comment is not part of the copyright comments. Return what we have so
8118-
// far.
8119-
return detachedComments;
8120-
}
8121-
}
8122-
8123-
detachedComments.push(comment);
8124-
lastComment = comment;
8125-
});
8126-
8127-
if (detachedComments.length) {
8128-
// All comments look like they could have been part of the copyright header. Make
8129-
// sure there is at least one blank line between it and the node. If not, it's not
8130-
// a copyright header.
8131-
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastOrUndefined(detachedComments).end);
8132-
let nodeLine = getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node.pos));
8133-
if (nodeLine >= lastCommentLine + 2) {
8134-
// Valid detachedComments
8135-
emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments);
8136-
emitComments(currentSourceFile, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment);
8137-
let currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: lastOrUndefined(detachedComments).end };
8138-
if (detachedCommentsInfo) {
8139-
detachedCommentsInfo.push(currentDetachedCommentInfo);
8140-
}
8141-
else {
8142-
detachedCommentsInfo = [currentDetachedCommentInfo];
8143-
}
8144-
}
8145-
}
8146-
}
8147-
}
8148-
81498085
function emitShebang() {
81508086
let shebang = getShebang(currentSourceFile.text);
81518087
if (shebang) {

src/compiler/utilities.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,80 @@ namespace ts {
19041904
});
19051905
}
19061906

1907+
/**
1908+
* Detached comment is a comment at the top of file or function body that is separated from
1909+
* the next statement by space.
1910+
*/
1911+
export function emitDetachedComments(currentSourceFile: SourceFile, writer: EmitTextWriter,
1912+
writeComment: (currentSourceFile: SourceFile, writer: EmitTextWriter, comment: CommentRange, newLine: string) => void,
1913+
node: TextRange, newLine: string, removeComments: boolean,
1914+
detachedCommentsInfo: { nodePos: number; detachedCommentEndPos: number }[]) {
1915+
let leadingComments: CommentRange[];
1916+
if (removeComments) {
1917+
// removeComments is true, only reserve pinned comment at the top of file
1918+
// For example:
1919+
// /*! Pinned Comment */
1920+
//
1921+
// var x = 10;
1922+
if (node.pos === 0) {
1923+
leadingComments = filter(getLeadingCommentRanges(currentSourceFile.text, node.pos), isPinnedComment);
1924+
}
1925+
}
1926+
else {
1927+
// removeComments is false, just get detached as normal and bypass the process to filter comment
1928+
leadingComments = getLeadingCommentRanges(currentSourceFile.text, node.pos);
1929+
}
1930+
1931+
if (leadingComments) {
1932+
let detachedComments: CommentRange[] = [];
1933+
let lastComment: CommentRange;
1934+
1935+
for (let comment of leadingComments) {
1936+
if (lastComment) {
1937+
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastComment.end);
1938+
let commentLine = getLineOfLocalPosition(currentSourceFile, comment.pos);
1939+
1940+
if (commentLine >= lastCommentLine + 2) {
1941+
// There was a blank line between the last comment and this comment. This
1942+
// comment is not part of the copyright comments. Return what we have so
1943+
// far.
1944+
break;
1945+
}
1946+
}
1947+
1948+
detachedComments.push(comment);
1949+
lastComment = comment;
1950+
}
1951+
1952+
if (detachedComments.length) {
1953+
// All comments look like they could have been part of the copyright header. Make
1954+
// sure there is at least one blank line between it and the node. If not, it's not
1955+
// a copyright header.
1956+
let lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastOrUndefined(detachedComments).end);
1957+
let nodeLine = getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node.pos));
1958+
if (nodeLine >= lastCommentLine + 2) {
1959+
// Valid detachedComments
1960+
emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments);
1961+
emitComments(currentSourceFile, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment);
1962+
let currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: lastOrUndefined(detachedComments).end };
1963+
if (detachedCommentsInfo) {
1964+
detachedCommentsInfo.push(currentDetachedCommentInfo);
1965+
}
1966+
else {
1967+
detachedCommentsInfo = [currentDetachedCommentInfo];
1968+
}
1969+
}
1970+
}
1971+
1972+
return detachedCommentsInfo;
1973+
}
1974+
1975+
function isPinnedComment(comment: CommentRange) {
1976+
return currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
1977+
currentSourceFile.text.charCodeAt(comment.pos + 2) === CharacterCodes.exclamation;
1978+
}
1979+
}
1980+
19071981
export function writeCommentRange(currentSourceFile: SourceFile, writer: EmitTextWriter, comment: CommentRange, newLine: string) {
19081982
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
19091983
let firstCommentLineAndCharacter = getLineAndCharacterOfPosition(currentSourceFile, comment.pos);

0 commit comments

Comments
 (0)