fix ordering of code fix import with triple-slash directives (#52484)

This commit is contained in:
Tobias S 2023-04-10 19:45:57 +02:00 committed by GitHub
parent 0f724c0430
commit 8369d41efe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 130 additions and 0 deletions

View File

@ -1072,7 +1072,9 @@ export function isRecognizedTripleSlashComment(text: string, commentPos: number,
const textSubStr = text.substring(commentPos, commentEnd);
return fullTripleSlashReferencePathRegEx.test(textSubStr) ||
fullTripleSlashAMDReferencePathRegEx.test(textSubStr) ||
fullTripleSlashAMDModuleRegEx.test(textSubStr) ||
fullTripleSlashReferenceTypeReferenceDirectiveRegEx.test(textSubStr) ||
fullTripleSlashLibReferenceRegEx.test(textSubStr) ||
defaultLibReferenceRegEx.test(textSubStr) ?
true : false;
}
@ -2365,8 +2367,10 @@ export function getJSDocCommentRanges(node: Node, text: string) {
/** @internal */
export const fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
const fullTripleSlashReferenceTypeReferenceDirectiveRegEx = /^(\/\/\/\s*<reference\s+types\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
const fullTripleSlashLibReferenceRegEx = /^(\/\/\/\s*<reference\s+lib\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
/** @internal */
export const fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*<amd-dependency\s+path\s*=\s*)(('[^']*')|("[^"]*")).*?\/>/;
const fullTripleSlashAMDModuleRegEx = /^\/\/\/\s*<amd-module\s+.*?\/>/;
const defaultLibReferenceRegEx = /^(\/\/\/\s*<reference\s+no-default-lib\s*=\s*)(('[^']*')|("[^"]*"))\s*\/>/;
/** @internal */

View File

@ -12,6 +12,7 @@ declare const elem: HTMLElement;
//// [file1.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/// <reference lib="dom" />
//// [file2.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@ -13,6 +13,7 @@ declare const elem: HTMLElement;
define("file1", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/// <reference lib="dom" />
});
define("file2", ["require", "exports"], function (require, exports) {
"use strict";

View File

@ -25,6 +25,7 @@ export const elem: HTMLElement = { field: 'a' };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.elem = void 0;
/// <reference lib="dom" />
exports.elem = { field: 'a' };

View File

@ -25,6 +25,7 @@ define("file1", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.elem = void 0;
/// <reference lib="dom" />
exports.elem = { field: 'a' };
});

View File

@ -0,0 +1,122 @@
/// <reference path="fourslash.ts" />
// repro from #52263
// @Filename: /tsconfig.json
////{
//// "compilerOptions": {
//// "skipDefaultLibCheck": false
//// }
////}
// @Filename: /a.ts
////export const x = 0;
// @Filename: /b.ts
////// some comment
////
/////// <reference lib="es2017.string" />
////
////const y = x + 1;
// @Filename: /c.ts
////// some comment
////
/////// <reference path="jquery-1.8.3.js" />
////
////const y = x + 1;
// @Filename: /d.ts
////// some comment
////
/////// <reference types="node" />
////
////const y = x + 1;
// @Filename: /e.ts
////// some comment
////
/////// <reference no-default-lib="true" />
////
////const y = x + 1;
// @Filename: /f.ts
////// some comment
////
/////// <amd-module name="NamedModule" />
////
////const y = x + 1;
// @Filename: /g.ts
////// some comment
////
/////// <amd-dependency path="legacy/moduleA" name="moduleA" />
////
////const y = x + 1;
goTo.file("/b.ts");
verify.importFixAtPosition([
`// some comment
/// <reference lib="es2017.string" />
import { x } from "./a";
const y = x + 1;`,
]);
goTo.file("/c.ts");
verify.importFixAtPosition([
`// some comment
/// <reference path="jquery-1.8.3.js" />
import { x } from "./a";
const y = x + 1;`,
]);
goTo.file("/d.ts");
verify.importFixAtPosition([
`// some comment
/// <reference types="node" />
import { x } from "./a";
const y = x + 1;`,
]);
goTo.file("/e.ts");
verify.importFixAtPosition([
`// some comment
/// <reference no-default-lib="true" />
import { x } from "./a";
const y = x + 1;`,
]);
goTo.file("/f.ts");
verify.importFixAtPosition([
`// some comment
/// <amd-module name="NamedModule" />
import { x } from "./a";
const y = x + 1;`,
]);
goTo.file("/g.ts");
verify.importFixAtPosition([
`// some comment
/// <amd-dependency path="legacy/moduleA" name="moduleA" />
import { x } from "./a";
const y = x + 1;`,
]);