Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,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;
}
Expand Down Expand Up @@ -2021,8 +2023,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 */
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/libReferenceDeclarationEmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/libReferenceNoLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' };


Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/libReferenceNoLibBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
});

Expand Down
122 changes: 122 additions & 0 deletions tests/cases/fourslash/importNameCodeFix_tripleSlashOrdering.ts
Original file line number Diff line number Diff line change
@@ -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;`,
]);