Skip to content

Commit cb5fd53

Browse files
author
Andy
authored
Handle shebang in import code fix (#20306)
1 parent bbb56fe commit cb5fd53

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/services/utilities.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,13 +1329,17 @@ namespace ts {
13291329
return getTokenAtPosition(sourceFile, declaration.members.pos - 1, /*includeJsDocComment*/ false);
13301330
}
13311331

1332-
export function getSourceFileImportLocation(node: SourceFile) {
1333-
// For a source file, it is possible there are detached comments we should not skip
1334-
const text = node.text;
1335-
const textLength = text.length;
1336-
let ranges = getLeadingCommentRanges(text, 0);
1337-
if (!ranges) return 0;
1332+
export function getSourceFileImportLocation({ text }: SourceFile) {
1333+
const shebang = getShebang(text);
13381334
let position = 0;
1335+
if (shebang !== undefined) {
1336+
position = shebang.length;
1337+
advancePastLineBreak();
1338+
}
1339+
1340+
// For a source file, it is possible there are detached comments we should not skip
1341+
let ranges = getLeadingCommentRanges(text, position);
1342+
if (!ranges) return position;
13391343
// However we should still skip a pinned comment at the top
13401344
if (ranges.length && ranges[0].kind === SyntaxKind.MultiLineCommentTrivia && isPinnedComment(text, ranges[0])) {
13411345
position = ranges[0].end;
@@ -1344,7 +1348,7 @@ namespace ts {
13441348
}
13451349
// As well as any triple slash references
13461350
for (const range of ranges) {
1347-
if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(node.text, range.pos, range.end)) {
1351+
if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(text, range.pos, range.end)) {
13481352
position = range.end;
13491353
advancePastLineBreak();
13501354
continue;
@@ -1354,12 +1358,12 @@ namespace ts {
13541358
return position;
13551359

13561360
function advancePastLineBreak() {
1357-
if (position < textLength) {
1361+
if (position < text.length) {
13581362
const charCode = text.charCodeAt(position);
13591363
if (isLineBreak(charCode)) {
13601364
position++;
13611365

1362-
if (position < textLength && charCode === CharacterCodes.carriageReturn && text.charCodeAt(position) === CharacterCodes.lineFeed) {
1366+
if (position < text.length && charCode === CharacterCodes.carriageReturn && text.charCodeAt(position) === CharacterCodes.lineFeed) {
13631367
position++;
13641368
}
13651369
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
////export const foo = 0;
5+
6+
// @Filename: /b.ts
7+
////[|#!/usr/bin/env node
8+
////foo/**/|]
9+
10+
goTo.file("/a.ts");
11+
goTo.file("/b.ts");
12+
13+
verify.importFixAtPosition([
14+
`#!/usr/bin/env node
15+
import { foo } from "./a";
16+
17+
foo`,
18+
]);

0 commit comments

Comments
 (0)