Skip to content

Commit 8369d41

Browse files
fix ordering of code fix import with triple-slash directives (#52484)
1 parent 0f724c0 commit 8369d41

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,9 @@ export function isRecognizedTripleSlashComment(text: string, commentPos: number,
10721072
const textSubStr = text.substring(commentPos, commentEnd);
10731073
return fullTripleSlashReferencePathRegEx.test(textSubStr) ||
10741074
fullTripleSlashAMDReferencePathRegEx.test(textSubStr) ||
1075+
fullTripleSlashAMDModuleRegEx.test(textSubStr) ||
10751076
fullTripleSlashReferenceTypeReferenceDirectiveRegEx.test(textSubStr) ||
1077+
fullTripleSlashLibReferenceRegEx.test(textSubStr) ||
10761078
defaultLibReferenceRegEx.test(textSubStr) ?
10771079
true : false;
10781080
}
@@ -2365,8 +2367,10 @@ export function getJSDocCommentRanges(node: Node, text: string) {
23652367
/** @internal */
23662368
export const fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
23672369
const fullTripleSlashReferenceTypeReferenceDirectiveRegEx = /^(\/\/\/\s*<reference\s+types\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
2370+
const fullTripleSlashLibReferenceRegEx = /^(\/\/\/\s*<reference\s+lib\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
23682371
/** @internal */
23692372
export const fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*<amd-dependency\s+path\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
2373+
const fullTripleSlashAMDModuleRegEx = /^\/\/\/\s*<amd-module\s+.*?\/>/;
23702374
const defaultLibReferenceRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)(('[^']*')|("[^"]*"))\s*\/>/;
23712375

23722376
/** @internal */

tests/baselines/reference/libReferenceDeclarationEmit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare const elem: HTMLElement;
1212
//// [file1.js]
1313
"use strict";
1414
Object.defineProperty(exports, "__esModule", { value: true });
15+
/// <reference lib="dom" />
1516
//// [file2.js]
1617
"use strict";
1718
Object.defineProperty(exports, "__esModule", { value: true });

tests/baselines/reference/libReferenceDeclarationEmitBundle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare const elem: HTMLElement;
1313
define("file1", ["require", "exports"], function (require, exports) {
1414
"use strict";
1515
Object.defineProperty(exports, "__esModule", { value: true });
16+
/// <reference lib="dom" />
1617
});
1718
define("file2", ["require", "exports"], function (require, exports) {
1819
"use strict";

tests/baselines/reference/libReferenceNoLib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const elem: HTMLElement = { field: 'a' };
2525
"use strict";
2626
Object.defineProperty(exports, "__esModule", { value: true });
2727
exports.elem = void 0;
28+
/// <reference lib="dom" />
2829
exports.elem = { field: 'a' };
2930

3031

tests/baselines/reference/libReferenceNoLibBundle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ define("file1", ["require", "exports"], function (require, exports) {
2525
"use strict";
2626
Object.defineProperty(exports, "__esModule", { value: true });
2727
exports.elem = void 0;
28+
/// <reference lib="dom" />
2829
exports.elem = { field: 'a' };
2930
});
3031

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// repro from #52263
4+
5+
// @Filename: /tsconfig.json
6+
////{
7+
//// "compilerOptions": {
8+
//// "skipDefaultLibCheck": false
9+
//// }
10+
////}
11+
12+
// @Filename: /a.ts
13+
////export const x = 0;
14+
15+
// @Filename: /b.ts
16+
////// some comment
17+
////
18+
/////// <reference lib="es2017.string" />
19+
////
20+
////const y = x + 1;
21+
22+
// @Filename: /c.ts
23+
////// some comment
24+
////
25+
/////// <reference path="jquery-1.8.3.js" />
26+
////
27+
////const y = x + 1;
28+
29+
// @Filename: /d.ts
30+
////// some comment
31+
////
32+
/////// <reference types="node" />
33+
////
34+
////const y = x + 1;
35+
36+
// @Filename: /e.ts
37+
////// some comment
38+
////
39+
/////// <reference no-default-lib="true" />
40+
////
41+
////const y = x + 1;
42+
43+
// @Filename: /f.ts
44+
////// some comment
45+
////
46+
/////// <amd-module name="NamedModule" />
47+
////
48+
////const y = x + 1;
49+
50+
// @Filename: /g.ts
51+
////// some comment
52+
////
53+
/////// <amd-dependency path="legacy/moduleA" name="moduleA" />
54+
////
55+
////const y = x + 1;
56+
57+
goTo.file("/b.ts");
58+
verify.importFixAtPosition([
59+
`// some comment
60+
61+
/// <reference lib="es2017.string" />
62+
63+
import { x } from "./a";
64+
65+
const y = x + 1;`,
66+
]);
67+
68+
goTo.file("/c.ts");
69+
verify.importFixAtPosition([
70+
`// some comment
71+
72+
/// <reference path="jquery-1.8.3.js" />
73+
74+
import { x } from "./a";
75+
76+
const y = x + 1;`,
77+
]);
78+
79+
goTo.file("/d.ts");
80+
verify.importFixAtPosition([
81+
`// some comment
82+
83+
/// <reference types="node" />
84+
85+
import { x } from "./a";
86+
87+
const y = x + 1;`,
88+
]);
89+
90+
goTo.file("/e.ts");
91+
92+
verify.importFixAtPosition([
93+
`// some comment
94+
95+
/// <reference no-default-lib="true" />
96+
97+
import { x } from "./a";
98+
99+
const y = x + 1;`,
100+
]);
101+
102+
goTo.file("/f.ts");
103+
verify.importFixAtPosition([
104+
`// some comment
105+
106+
/// <amd-module name="NamedModule" />
107+
108+
import { x } from "./a";
109+
110+
const y = x + 1;`,
111+
]);
112+
113+
goTo.file("/g.ts");
114+
verify.importFixAtPosition([
115+
`// some comment
116+
117+
/// <amd-dependency path="legacy/moduleA" name="moduleA" />
118+
119+
import { x } from "./a";
120+
121+
const y = x + 1;`,
122+
]);

0 commit comments

Comments
 (0)