Skip to content

Commit a11ad53

Browse files
committed
Emit copyright headers before anything else in the sourceFile
1 parent ce89da2 commit a11ad53

File tree

185 files changed

+768
-550
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+768
-550
lines changed

src/compiler/emitter.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,15 @@ module ts {
319319
/** write emitted output to disk*/
320320
var writeEmittedFiles = writeJavaScriptFile;
321321

322-
/** Emit leading comments of the declaration */
323-
var emitLeadingComments = compilerOptions.removeComments ? (d: Node) => { } : emitLeadingDeclarationComments;
322+
/** Emit leading comments of the node */
323+
var emitLeadingComments = compilerOptions.removeComments ? (node: Node) => { } : emitLeadingDeclarationComments;
324324

325-
/** Emit Trailing comments of the declaration */
326-
var emitTrailingComments = compilerOptions.removeComments ? (d: Node) => { } : emitTrailingDeclarationComments;
325+
/** Emit Trailing comments of the node */
326+
var emitTrailingComments = compilerOptions.removeComments ? (node: Node) => { } : emitTrailingDeclarationComments;
327+
328+
var detachedCommentsEndPos: number;
329+
/** Emit detached comments of the node */
330+
var emitDetachedComments = compilerOptions.removeComments ? (node: Node) => { } : emitDetachedCommentsAtPosition;
327331

328332
var writeComment = writeCommentRange;
329333

@@ -1912,6 +1916,7 @@ module ts {
19121916

19131917
function emitSourceFile(node: SourceFile) {
19141918
currentSourceFile = node;
1919+
emitDetachedComments(node);
19151920
// emit prologue directives prior to __extends
19161921
var startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
19171922
if (!extendsEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitExtends) {
@@ -2064,7 +2069,16 @@ module ts {
20642069
function emitLeadingDeclarationComments(node: Node) {
20652070
// Emit the leading comments only if the parent's pos doesnt match because parent should take care of emitting these comments
20662071
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
2067-
var leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
2072+
var leadingComments: Comment[];
2073+
if (detachedCommentsEndPos === undefined) {
2074+
// get the leading comments from the node
2075+
leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
2076+
}
2077+
else {
2078+
// get the leading comments from detachedPos
2079+
leadingComments = getLeadingComments(currentSourceFile.text, detachedCommentsEndPos);
2080+
detachedCommentsEndPos = undefined;
2081+
}
20682082
emitNewLineBeforeLeadingComments(node, leadingComments, writer);
20692083
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
20702084
emitComments(leadingComments, /*trailingSeparator*/ true, writer, writeComment);
@@ -2080,6 +2094,44 @@ module ts {
20802094
}
20812095
}
20822096

2097+
function emitDetachedCommentsAtPosition(node: Node) {
2098+
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
2099+
if (leadingComments) {
2100+
var detachedComments: Comment[] = [];
2101+
var lastComment: Comment;
2102+
2103+
forEach(leadingComments, comment => {
2104+
if (lastComment) {
2105+
var lastCommentLine = currentSourceFile.getLineAndCharacterFromPosition(lastComment.end).line;
2106+
var commentLine = currentSourceFile.getLineAndCharacterFromPosition(comment.pos).line;
2107+
2108+
if (commentLine >= lastCommentLine + 2) {
2109+
// There was a blank line between the last comment and this comment. This
2110+
// comment is not part of the copyright comments. Return what we have so
2111+
// far.
2112+
return detachedComments;
2113+
}
2114+
}
2115+
2116+
detachedComments.push(comment);
2117+
lastComment = comment;
2118+
});
2119+
2120+
if (detachedComments && detachedComments.length) {
2121+
// All comments look like they could have been part of the copyright header. Make
2122+
// sure there is at least one blank line between it and the node. If not, it's not
2123+
// a copyright header.
2124+
var lastCommentLine = currentSourceFile.getLineAndCharacterFromPosition(detachedComments[detachedComments.length - 1].end).line;
2125+
var astLine = currentSourceFile.getLineAndCharacterFromPosition(skipTrivia(currentSourceFile.text, node.pos)).line;
2126+
if (astLine >= lastCommentLine + 2) {
2127+
// Valid detachedComments
2128+
emitComments(detachedComments, /*trailingSeparator*/ true, writer, writeComment);
2129+
detachedCommentsEndPos = detachedComments[detachedComments.length - 1].end;
2130+
}
2131+
}
2132+
}
2133+
}
2134+
20832135
if (compilerOptions.sourceMap) {
20842136
initializeEmitterWithSourceMaps();
20852137
}

tests/baselines/reference/amdDependencyComment2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import m1 = require("m2")
55
m1.f();
66

77
//// [amdDependencyComment2.js]
8+
///<amd-dependency path='bar'/>
89
define(["require", "exports", "m2", "bar"], function (require, exports, m1) {
910
m1.f();
1011
});

tests/baselines/reference/anyAssignabilityInInheritance.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ declare function foo18(x: any): any;
8989
var r3 = foo3(a); // any
9090

9191
//// [anyAssignabilityInInheritance.js]
92+
// any is not a subtype of any other types, errors expected on all the below derived classes unless otherwise noted
9293
var a;
9394
var r3 = foo2(a); // any, not a subtype of number so it skips that overload, is a subtype of itself so it picks second (if truly ambiguous it would pick first overload)
9495
var r3 = foo3(a); // any

tests/baselines/reference/anyAssignableToEveryType2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ interface I20 {
131131

132132

133133
//// [anyAssignableToEveryType2.js]
134+
// any is not a subtype of any other types, but is assignable, all the below should work
134135
var A = (function () {
135136
function A() {
136137
}

tests/baselines/reference/apparentTypeSubtyping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class Derived2<U extends String> extends Base2 { // error because of the prototy
2222
}
2323

2424
//// [apparentTypeSubtyping.js]
25+
// subtype checks use the apparent type of the target type
26+
// S is a subtype of a type T, and T is a supertype of S, if one of the following is true, where S' denotes the apparent type (section 3.8.1) of S:
2527
var __extends = this.__extends || function (d, b) {
2628
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
2729
function __() { this.constructor = d; }
2830
__.prototype = b.prototype;
2931
d.prototype = new __();
3032
};
31-
// subtype checks use the apparent type of the target type
32-
// S is a subtype of a type T, and T is a supertype of S, if one of the following is true, where S' denotes the apparent type (section 3.8.1) of S:
3333
var Base = (function () {
3434
function Base() {
3535
}

tests/baselines/reference/apparentTypeSupertype.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class Derived<U extends String> extends Base { // error
1212
}
1313

1414
//// [apparentTypeSupertype.js]
15+
// subtype checks use the apparent type of the target type
16+
// S is a subtype of a type T, and T is a supertype of S, if one of the following is true, where S' denotes the apparent type (section 3.8.1) of S:
1517
var __extends = this.__extends || function (d, b) {
1618
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
1719
function __() { this.constructor = d; }
1820
__.prototype = b.prototype;
1921
d.prototype = new __();
2022
};
21-
// subtype checks use the apparent type of the target type
22-
// S is a subtype of a type T, and T is a supertype of S, if one of the following is true, where S' denotes the apparent type (section 3.8.1) of S:
2323
var Base = (function () {
2424
function Base() {
2525
}

tests/baselines/reference/arrayLiterals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ var context4: Base[] = [new Derived1(), new Derived1()];
4545

4646

4747
//// [arrayLiterals.js]
48+
// Empty array literal with no contextual type has type Undefined[]
4849
var __extends = this.__extends || function (d, b) {
4950
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
5051
function __() { this.constructor = d; }
5152
__.prototype = b.prototype;
5253
d.prototype = new __();
5354
};
54-
// Empty array literal with no contextual type has type Undefined[]
5555
var arr1 = [[], [1], ['']];
5656
var arr1; // Bug 825172: Error ({}[] does not match {}[]), but should be OK
5757
var arr2 = [[null], [1], ['']];

tests/baselines/reference/assignmentCompatWithCallSignatures.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ a = function (x: string) { return ''; }
4444

4545

4646
//// [assignmentCompatWithCallSignatures.js]
47+
// void returning call signatures can be assigned a non-void returning call signature that otherwise matches
4748
var t;
4849
var a;
4950
t = a;

tests/baselines/reference/assignmentCompatWithCallSignatures2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ a = function (x: string) { return ''; }
5151

5252

5353
//// [assignmentCompatWithCallSignatures2.js]
54+
// void returning call signatures can be assigned a non-void returning call signature that otherwise matches
5455
var t;
5556
var a;
5657
t = a;

tests/baselines/reference/assignmentCompatWithCallSignatures3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ b18 = a18; // ok
100100

101101

102102
//// [assignmentCompatWithCallSignatures3.js]
103+
// these are all permitted with the current rules, since we do not do contextual signature instantiation
103104
var __extends = this.__extends || function (d, b) {
104105
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
105106
function __() { this.constructor = d; }
106107
__.prototype = b.prototype;
107108
d.prototype = new __();
108109
};
109-
// these are all permitted with the current rules, since we do not do contextual signature instantiation
110110
var Base = (function () {
111111
function Base() {
112112
}

0 commit comments

Comments
 (0)