Skip to content

Commit ff027d4

Browse files
committed
Handle triple slash refs
1 parent 36eae04 commit ff027d4

File tree

9 files changed

+107
-81
lines changed

9 files changed

+107
-81
lines changed

src/compiler/factory.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,19 +2701,21 @@ namespace ts {
27012701
const node = <UnparsedSource>createNode(SyntaxKind.UnparsedSource);
27022702
if (!isString(textOrInputFiles)) {
27032703
Debug.assert(mapPathOrType === "js" || mapPathOrType === "dts");
2704-
node.fileName = mapPathOrType === "js" ? textOrInputFiles.javascriptPath : textOrInputFiles.declarationPath;
2704+
node.fileName = (mapPathOrType === "js" ? textOrInputFiles.javascriptPath : textOrInputFiles.declarationPath) || "";
27052705
node.sourceMapPath = mapPathOrType === "js" ? textOrInputFiles.javascriptMapPath : textOrInputFiles.declarationMapPath;
27062706
Object.defineProperties(node, {
27072707
text: { get() { return mapPathOrType === "js" ? textOrInputFiles.javascriptText : textOrInputFiles.declarationText; } },
27082708
sourceMapText: { get() { return mapPathOrType === "js" ? textOrInputFiles.javascriptMapText : textOrInputFiles.declarationMapText; } },
27092709
});
27102710
}
27112711
else {
2712+
node.fileName = "";
27122713
node.text = textOrInputFiles;
27132714
node.sourceMapPath = mapPathOrType;
27142715
node.sourceMapText = map;
27152716
}
27162717
const text = node.text;
2718+
node.languageVersion = ScriptTarget.Latest;
27172719

27182720
// Shebang
27192721
let pos = isShebangTrivia(text, 0) ? skipTrivia(text, 0, /*stopAfterLineBreak*/ true) : 0;
@@ -2742,8 +2744,12 @@ namespace ts {
27422744
(helpers || (helpers = [])).push(helperInfo.helper);
27432745
}
27442746

2747+
// triple slash directives
2748+
const newPos = processCommentPragmas(node as {} as PragmaContext, text);
2749+
processPragmasIntoFields(node as {} as PragmaContext, noop);
2750+
27452751
// Rest of the text
2746-
node.pos = pos;
2752+
node.pos = newPos > pos ? newPos : pos;
27472753
node.prologues = prologues || emptyArray;
27482754
node.helpers = helpers;
27492755
node.getLineAndCharacterOfPosition = pos => getLineAndCharacterOfPosition(node, pos);

src/compiler/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7721,7 +7721,7 @@ namespace ts {
77217721
}
77227722

77237723
/*@internal*/
7724-
export function processCommentPragmas(context: PragmaContext, sourceText: string): void {
7724+
export function processCommentPragmas(context: PragmaContext, sourceText: string) {
77257725
const triviaScanner = createScanner(context.languageVersion, /*skipTrivia*/ false, LanguageVariant.Standard, sourceText);
77267726
const pragmas: PragmaPseudoMapEntry[] = [];
77277727

@@ -7758,6 +7758,7 @@ namespace ts {
77587758
}
77597759
context.pragmas.set(pragma!.name, pragma!.args);
77607760
}
7761+
return triviaScanner.getStartPos();
77617762
}
77627763

77637764
type PragmaDiagnosticReporter = (pos: number, length: number, message: DiagnosticMessage) => void;

src/compiler/transformers/declarations.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ namespace ts {
207207
}
208208
), mapDefined(node.prepends, prepend => {
209209
if (prepend.kind === SyntaxKind.InputFiles) {
210-
return createUnparsedSourceFile(prepend, "dts");
210+
const sourceFile = createUnparsedSourceFile(prepend, "dts");
211+
hasNoDefaultLib = hasNoDefaultLib || !!sourceFile.hasNoDefaultLib;
212+
collectReferences(sourceFile, refs);
213+
collectLibs(sourceFile, libs);
214+
return sourceFile;
211215
}
212216
}));
213217
bundle.syntheticFileReferences = [];
@@ -311,8 +315,8 @@ namespace ts {
311315
}
312316
}
313317

314-
function collectReferences(sourceFile: SourceFile, ret: Map<SourceFile>) {
315-
if (noResolve || isSourceFileJS(sourceFile)) return ret;
318+
function collectReferences(sourceFile: SourceFile | UnparsedSource, ret: Map<SourceFile>) {
319+
if (noResolve || (!isUnparsedSource(sourceFile) && isSourceFileJS(sourceFile))) return ret;
316320
forEach(sourceFile.referencedFiles, f => {
317321
const elem = tryResolveScriptReference(host, sourceFile, f);
318322
if (elem) {
@@ -322,7 +326,7 @@ namespace ts {
322326
return ret;
323327
}
324328

325-
function collectLibs(sourceFile: SourceFile, ret: Map<boolean>) {
329+
function collectLibs(sourceFile: SourceFile | UnparsedSource, ret: Map<boolean>) {
326330
forEach(sourceFile.libReferenceDirectives, ref => {
327331
const lib = host.getLibFileFromReference(ref);
328332
if (lib) {

src/compiler/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2774,10 +2774,15 @@ namespace ts {
27742774

27752775
export interface UnparsedSource extends Node {
27762776
kind: SyntaxKind.UnparsedSource;
2777-
fileName?: string;
2777+
fileName: string;
27782778
text: string;
2779+
languageVersion: ScriptTarget;
27792780
prologues: ReadonlyArray<UnparsedPrologue>;
27802781
helpers: ReadonlyArray<UnscopedEmitHelpers> | undefined;
2782+
referencedFiles: FileReference[];
2783+
typeReferenceDirectives: FileReference[];
2784+
libReferenceDirectives: FileReference[];
2785+
hasNoDefaultLib?: boolean;
27812786
sourceMapPath?: string;
27822787
sourceMapText?: string;
27832788
/*@internal*/ parsedSourceMap?: RawSourceMap | false | undefined;

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,7 @@ namespace ts {
25572557
return undefined;
25582558
}
25592559

2560-
export function tryResolveScriptReference(host: ScriptReferenceHost, sourceFile: SourceFile, reference: FileReference) {
2560+
export function tryResolveScriptReference(host: ScriptReferenceHost, sourceFile: SourceFile | UnparsedSource, reference: FileReference) {
25612561
if (!host.getCompilerOptions().noResolve) {
25622562
const referenceFileName = isRootedDiskPath(reference.fileName) ? reference.fileName : combinePaths(getDirectoryPath(sourceFile.fileName), reference.fileName);
25632563
return host.getSourceFile(referenceFileName);

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,10 +1738,15 @@ declare namespace ts {
17381738
}
17391739
interface UnparsedSource extends Node {
17401740
kind: SyntaxKind.UnparsedSource;
1741-
fileName?: string;
1741+
fileName: string;
17421742
text: string;
1743+
languageVersion: ScriptTarget;
17431744
prologues: ReadonlyArray<UnparsedPrologue>;
17441745
helpers: ReadonlyArray<UnscopedEmitHelpers> | undefined;
1746+
referencedFiles: FileReference[];
1747+
typeReferenceDirectives: FileReference[];
1748+
libReferenceDirectives: FileReference[];
1749+
hasNoDefaultLib?: boolean;
17451750
sourceMapPath?: string;
17461751
sourceMapText?: string;
17471752
}

tests/baselines/reference/api/typescript.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,10 +1738,15 @@ declare namespace ts {
17381738
}
17391739
interface UnparsedSource extends Node {
17401740
kind: SyntaxKind.UnparsedSource;
1741-
fileName?: string;
1741+
fileName: string;
17421742
text: string;
1743+
languageVersion: ScriptTarget;
17431744
prologues: ReadonlyArray<UnparsedPrologue>;
17441745
helpers: ReadonlyArray<UnscopedEmitHelpers> | undefined;
1746+
referencedFiles: FileReference[];
1747+
typeReferenceDirectives: FileReference[];
1748+
libReferenceDirectives: FileReference[];
1749+
hasNoDefaultLib?: boolean;
17451750
sourceMapPath?: string;
17461751
sourceMapText?: string;
17471752
}

tests/baselines/reference/outFile-triple-slash-refs-in-all-projects.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ declare class secondsecond_part1 { }
824824

825825
//// [/src/third/thirdjs/output/third-output.d.ts]
826826
/// <reference path="../../tripleRef.d.ts" />
827-
/// <reference path="../tripleRef.d.ts" />
827+
/// <reference path="../../../first/tripleRef.d.ts" />
828+
/// <reference path="../../../second/tripleRef.d.ts" />
828829
interface TheFirst {
829830
none: any;
830831
}
@@ -835,7 +836,6 @@ interface NoJsForHereEither {
835836
declare const first_part2Const: firstfirst_part2;
836837
declare function f(): string;
837838
//# sourceMappingURL=first-output.d.ts.map
838-
/// <reference path="../second/tripleRef.d.ts" />
839839
declare const second_part1Const: secondsecond_part1;
840840
declare namespace N {
841841
}
@@ -850,7 +850,7 @@ declare var c: C;
850850
//# sourceMappingURL=third-output.d.ts.map
851851

852852
//// [/src/third/thirdjs/output/third-output.d.ts.map]
853-
{"version":3,"file":"third-output.d.ts","sourceRoot":"","sources":["../../third_part1.ts","../../../first/first_PART1.ts","../../../first/first_part2.ts","../../../first/first_part3.ts","../../../second/second_part1.ts","../../../second/second_part2.ts"],"names":[],"mappings":";;ACAA,UAAU,QAAQ;IACd,IAAI,EAAE,GAAG,CAAC;CACb;AAED,QAAA,MAAM,CAAC,iBAAiB,CAAC;AAEzB,UAAU,iBAAiB;IACvB,IAAI,EAAE,GAAG,CAAC;CACb;ACPD,QAAA,MAAM,gBAAgB,kBAAyB,CAAC;ACDhD,iBAAS,CAAC,WAET;;;ACDD,QAAA,MAAM,iBAAiB,oBAA2B,CAAC;AACnD,kBAAU,CAAC,CAAC;CAEX;AAED,kBAAU,CAAC,CAAC;CAMX;ACZD,cAAM,CAAC;IACH,WAAW;CAGd;;ALHD,QAAA,MAAM,gBAAgB,kBAAyB,CAAC;AAChD,QAAA,IAAI,CAAC,GAAU,CAAC"}
853+
{"version":3,"file":"third-output.d.ts","sourceRoot":"","sources":["../../third_part1.ts","../../../first/first_PART1.ts","../../../first/first_part2.ts","../../../first/first_part3.ts","../../../second/second_part1.ts","../../../second/second_part2.ts"],"names":[],"mappings":";;;ACAA,UAAU,QAAQ;IACd,IAAI,EAAE,GAAG,CAAC;CACb;AAED,QAAA,MAAM,CAAC,iBAAiB,CAAC;AAEzB,UAAU,iBAAiB;IACvB,IAAI,EAAE,GAAG,CAAC;CACb;ACPD,QAAA,MAAM,gBAAgB,kBAAyB,CAAC;ACDhD,iBAAS,CAAC,WAET;;ACDD,QAAA,MAAM,iBAAiB,oBAA2B,CAAC;AACnD,kBAAU,CAAC,CAAC;CAEX;AAED,kBAAU,CAAC,CAAC;CAMX;ACZD,cAAM,CAAC;IACH,WAAW;CAGd;;ALHD,QAAA,MAAM,gBAAgB,kBAAyB,CAAC;AAChD,QAAA,IAAI,CAAC,GAAU,CAAC"}
854854

855855
//// [/src/third/thirdjs/output/third-output.d.ts.map.baseline.txt]
856856
===================================================================
@@ -864,17 +864,18 @@ emittedFile:/src/third/thirdjs/output/third-output.d.ts
864864
sourceFile:../../../first/first_PART1.ts
865865
-------------------------------------------------------------------
866866
>>>/// <reference path="../../tripleRef.d.ts" />
867-
>>>/// <reference path="../tripleRef.d.ts" />
867+
>>>/// <reference path="../../../first/tripleRef.d.ts" />
868+
>>>/// <reference path="../../../second/tripleRef.d.ts" />
868869
>>>interface TheFirst {
869870
1 >
870871
2 >^^^^^^^^^^
871872
3 > ^^^^^^^^
872873
1 >
873874
2 >interface
874875
3 > TheFirst
875-
1 >Emitted(3, 1) Source(1, 1) + SourceIndex(1)
876-
2 >Emitted(3, 11) Source(1, 11) + SourceIndex(1)
877-
3 >Emitted(3, 19) Source(1, 19) + SourceIndex(1)
876+
1 >Emitted(4, 1) Source(1, 1) + SourceIndex(1)
877+
2 >Emitted(4, 11) Source(1, 11) + SourceIndex(1)
878+
3 >Emitted(4, 19) Source(1, 19) + SourceIndex(1)
878879
---
879880
>>> none: any;
880881
1 >^^^^
@@ -888,18 +889,18 @@ sourceFile:../../../first/first_PART1.ts
888889
3 > :
889890
4 > any
890891
5 > ;
891-
1 >Emitted(4, 5) Source(2, 5) + SourceIndex(1)
892-
2 >Emitted(4, 9) Source(2, 9) + SourceIndex(1)
893-
3 >Emitted(4, 11) Source(2, 11) + SourceIndex(1)
894-
4 >Emitted(4, 14) Source(2, 14) + SourceIndex(1)
895-
5 >Emitted(4, 15) Source(2, 15) + SourceIndex(1)
892+
1 >Emitted(5, 5) Source(2, 5) + SourceIndex(1)
893+
2 >Emitted(5, 9) Source(2, 9) + SourceIndex(1)
894+
3 >Emitted(5, 11) Source(2, 11) + SourceIndex(1)
895+
4 >Emitted(5, 14) Source(2, 14) + SourceIndex(1)
896+
5 >Emitted(5, 15) Source(2, 15) + SourceIndex(1)
896897
---
897898
>>>}
898899
1 >^
899900
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
900901
1 >
901902
>}
902-
1 >Emitted(5, 2) Source(3, 2) + SourceIndex(1)
903+
1 >Emitted(6, 2) Source(3, 2) + SourceIndex(1)
903904
---
904905
>>>declare const s = "Hello, world";
905906
1->
@@ -916,12 +917,12 @@ sourceFile:../../../first/first_PART1.ts
916917
4 > s
917918
5 > = "Hello, world"
918919
6 > ;
919-
1->Emitted(6, 1) Source(5, 1) + SourceIndex(1)
920-
2 >Emitted(6, 9) Source(5, 1) + SourceIndex(1)
921-
3 >Emitted(6, 15) Source(5, 7) + SourceIndex(1)
922-
4 >Emitted(6, 16) Source(5, 8) + SourceIndex(1)
923-
5 >Emitted(6, 33) Source(5, 25) + SourceIndex(1)
924-
6 >Emitted(6, 34) Source(5, 26) + SourceIndex(1)
920+
1->Emitted(7, 1) Source(5, 1) + SourceIndex(1)
921+
2 >Emitted(7, 9) Source(5, 1) + SourceIndex(1)
922+
3 >Emitted(7, 15) Source(5, 7) + SourceIndex(1)
923+
4 >Emitted(7, 16) Source(5, 8) + SourceIndex(1)
924+
5 >Emitted(7, 33) Source(5, 25) + SourceIndex(1)
925+
6 >Emitted(7, 34) Source(5, 26) + SourceIndex(1)
925926
---
926927
>>>interface NoJsForHereEither {
927928
1 >
@@ -932,9 +933,9 @@ sourceFile:../../../first/first_PART1.ts
932933
>
933934
2 >interface
934935
3 > NoJsForHereEither
935-
1 >Emitted(7, 1) Source(7, 1) + SourceIndex(1)
936-
2 >Emitted(7, 11) Source(7, 11) + SourceIndex(1)
937-
3 >Emitted(7, 28) Source(7, 28) + SourceIndex(1)
936+
1 >Emitted(8, 1) Source(7, 1) + SourceIndex(1)
937+
2 >Emitted(8, 11) Source(7, 11) + SourceIndex(1)
938+
3 >Emitted(8, 28) Source(7, 28) + SourceIndex(1)
938939
---
939940
>>> none: any;
940941
1 >^^^^
@@ -948,18 +949,18 @@ sourceFile:../../../first/first_PART1.ts
948949
3 > :
949950
4 > any
950951
5 > ;
951-
1 >Emitted(8, 5) Source(8, 5) + SourceIndex(1)
952-
2 >Emitted(8, 9) Source(8, 9) + SourceIndex(1)
953-
3 >Emitted(8, 11) Source(8, 11) + SourceIndex(1)
954-
4 >Emitted(8, 14) Source(8, 14) + SourceIndex(1)
955-
5 >Emitted(8, 15) Source(8, 15) + SourceIndex(1)
952+
1 >Emitted(9, 5) Source(8, 5) + SourceIndex(1)
953+
2 >Emitted(9, 9) Source(8, 9) + SourceIndex(1)
954+
3 >Emitted(9, 11) Source(8, 11) + SourceIndex(1)
955+
4 >Emitted(9, 14) Source(8, 14) + SourceIndex(1)
956+
5 >Emitted(9, 15) Source(8, 15) + SourceIndex(1)
956957
---
957958
>>>}
958959
1 >^
959960
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
960961
1 >
961962
>}
962-
1 >Emitted(9, 2) Source(9, 2) + SourceIndex(1)
963+
1 >Emitted(10, 2) Source(9, 2) + SourceIndex(1)
963964
---
964965
-------------------------------------------------------------------
965966
emittedFile:/src/third/thirdjs/output/third-output.d.ts
@@ -979,12 +980,12 @@ sourceFile:../../../first/first_part2.ts
979980
4 > first_part2Const
980981
5 > = new firstfirst_part2()
981982
6 > ;
982-
1->Emitted(10, 1) Source(2, 1) + SourceIndex(2)
983-
2 >Emitted(10, 9) Source(2, 1) + SourceIndex(2)
984-
3 >Emitted(10, 15) Source(2, 7) + SourceIndex(2)
985-
4 >Emitted(10, 31) Source(2, 23) + SourceIndex(2)
986-
5 >Emitted(10, 49) Source(2, 48) + SourceIndex(2)
987-
6 >Emitted(10, 50) Source(2, 49) + SourceIndex(2)
983+
1->Emitted(11, 1) Source(2, 1) + SourceIndex(2)
984+
2 >Emitted(11, 9) Source(2, 1) + SourceIndex(2)
985+
3 >Emitted(11, 15) Source(2, 7) + SourceIndex(2)
986+
4 >Emitted(11, 31) Source(2, 23) + SourceIndex(2)
987+
5 >Emitted(11, 49) Source(2, 48) + SourceIndex(2)
988+
6 >Emitted(11, 50) Source(2, 49) + SourceIndex(2)
988989
---
989990
-------------------------------------------------------------------
990991
emittedFile:/src/third/thirdjs/output/third-output.d.ts
@@ -1002,17 +1003,16 @@ sourceFile:../../../first/first_part3.ts
10021003
4 > () {
10031004
> return "JS does hoists";
10041005
> }
1005-
1 >Emitted(11, 1) Source(1, 1) + SourceIndex(3)
1006-
2 >Emitted(11, 18) Source(1, 10) + SourceIndex(3)
1007-
3 >Emitted(11, 19) Source(1, 11) + SourceIndex(3)
1008-
4 >Emitted(11, 30) Source(3, 2) + SourceIndex(3)
1006+
1 >Emitted(12, 1) Source(1, 1) + SourceIndex(3)
1007+
2 >Emitted(12, 18) Source(1, 10) + SourceIndex(3)
1008+
3 >Emitted(12, 19) Source(1, 11) + SourceIndex(3)
1009+
4 >Emitted(12, 30) Source(3, 2) + SourceIndex(3)
10091010
---
10101011
-------------------------------------------------------------------
10111012
emittedFile:/src/third/thirdjs/output/third-output.d.ts
10121013
sourceFile:../../../second/second_part1.ts
10131014
-------------------------------------------------------------------
10141015
>>>//# sourceMappingURL=first-output.d.ts.map
1015-
>>>/// <reference path="../second/tripleRef.d.ts" />
10161016
>>>declare const second_part1Const: secondsecond_part1;
10171017
1->
10181018
2 >^^^^^^^^

0 commit comments

Comments
 (0)