mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-19 10:41:56 -05:00
Handle shebang when using prepend nodes
This commit is contained in:
@@ -1208,7 +1208,7 @@ namespace ts {
|
||||
|
||||
// SyntaxKind.UnparsedSource
|
||||
function emitUnparsedSource(unparsed: UnparsedSource) {
|
||||
writer.rawWrite(unparsed.text);
|
||||
writer.rawWrite(unparsed.text.substr(unparsed.pos));
|
||||
}
|
||||
|
||||
//
|
||||
@@ -3020,8 +3020,8 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function emitShebangIfNeeded(sourceFileOrBundle: Bundle | SourceFile) {
|
||||
if (isSourceFile(sourceFileOrBundle)) {
|
||||
function emitShebangIfNeeded(sourceFileOrBundle: Bundle | SourceFile | UnparsedSource) {
|
||||
if (isSourceFile(sourceFileOrBundle) || isUnparsedSource(sourceFileOrBundle)) {
|
||||
const shebang = getShebang(sourceFileOrBundle.text);
|
||||
if (shebang) {
|
||||
writeComment(shebang);
|
||||
@@ -3030,10 +3030,16 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (const prepend of sourceFileOrBundle.prepends) {
|
||||
Debug.assertNode(prepend, isUnparsedSource);
|
||||
if (emitShebangIfNeeded(prepend as UnparsedSource)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (const sourceFile of sourceFileOrBundle.sourceFiles) {
|
||||
// Emit only the first encountered shebang
|
||||
if (emitShebangIfNeeded(sourceFile)) {
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4346,7 +4352,8 @@ namespace ts {
|
||||
writer.getLine(),
|
||||
writer.getColumn(),
|
||||
parsed,
|
||||
node.sourceMapPath!);
|
||||
node.sourceMapPath!,
|
||||
node.pos && node.getLineAndCharacterOfPosition(node.pos).line);
|
||||
}
|
||||
pipelinePhase(hint, node);
|
||||
}
|
||||
|
||||
@@ -2648,6 +2648,9 @@ namespace ts {
|
||||
node.sourceMapPath = mapPathOrType;
|
||||
node.sourceMapText = map;
|
||||
}
|
||||
const text = node.text;
|
||||
node.pos = isShebangTrivia(text, 0) ? skipTrivia(text, 0, /*stopAfterLineBreak*/ true) : 0;
|
||||
node.getLineAndCharacterOfPosition = pos => getLineAndCharacterOfPosition(node, pos);
|
||||
return node;
|
||||
}
|
||||
export function createInputFiles(
|
||||
|
||||
@@ -623,13 +623,15 @@ namespace ts {
|
||||
|
||||
const shebangTriviaRegex = /^#!.*/;
|
||||
|
||||
function isShebangTrivia(text: string, pos: number) {
|
||||
/*@internal*/
|
||||
export function isShebangTrivia(text: string, pos: number) {
|
||||
// Shebangs check must only be done at the start of the file
|
||||
Debug.assert(pos === 0);
|
||||
return shebangTriviaRegex.test(text);
|
||||
}
|
||||
|
||||
function scanShebangTrivia(text: string, pos: number) {
|
||||
/*@internal*/
|
||||
export function scanShebangTrivia(text: string, pos: number) {
|
||||
const shebang = shebangTriviaRegex.exec(text)![0];
|
||||
pos = pos + shebang.length;
|
||||
return pos;
|
||||
|
||||
@@ -140,7 +140,7 @@ namespace ts {
|
||||
exit();
|
||||
}
|
||||
|
||||
function appendSourceMap(generatedLine: number, generatedCharacter: number, map: RawSourceMap, sourceMapPath: string) {
|
||||
function appendSourceMap(generatedLine: number, generatedCharacter: number, map: RawSourceMap, sourceMapPath: string, startLine: number) {
|
||||
Debug.assert(generatedLine >= pendingGeneratedLine, "generatedLine cannot backtrack");
|
||||
Debug.assert(generatedCharacter >= 0, "generatedCharacter cannot be negative");
|
||||
enter();
|
||||
@@ -149,6 +149,9 @@ namespace ts {
|
||||
let nameIndexToNewNameIndexMap: number[] | undefined;
|
||||
const mappingIterator = decodeMappings(map.mappings);
|
||||
for (let { value: raw, done } = mappingIterator.next(); !done; { value: raw, done } = mappingIterator.next()) {
|
||||
if (raw.generatedLine < startLine) {
|
||||
continue;
|
||||
}
|
||||
// Then reencode all the updated mappings into the overall map
|
||||
let newSourceIndex: number | undefined;
|
||||
let newSourceLine: number | undefined;
|
||||
@@ -178,8 +181,9 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
const newGeneratedLine = raw.generatedLine + generatedLine;
|
||||
const newGeneratedCharacter = raw.generatedLine === 0 ? raw.generatedCharacter + generatedCharacter : raw.generatedCharacter;
|
||||
const rawGeneratedLine = raw.generatedLine - startLine;
|
||||
const newGeneratedLine = rawGeneratedLine + generatedLine;
|
||||
const newGeneratedCharacter = rawGeneratedLine === 0 ? raw.generatedCharacter + generatedCharacter : raw.generatedCharacter;
|
||||
addMapping(newGeneratedLine, newGeneratedCharacter, newSourceIndex, newSourceLine, newSourceCharacter, newNameIndex);
|
||||
}
|
||||
exit();
|
||||
|
||||
@@ -2777,6 +2777,9 @@ namespace ts {
|
||||
text: string;
|
||||
sourceMapPath?: string;
|
||||
sourceMapText?: string;
|
||||
// Adding this to satisfy services, fix later
|
||||
/*@internal*/
|
||||
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
|
||||
}
|
||||
|
||||
export interface JsonSourceFile extends SourceFile {
|
||||
@@ -5528,7 +5531,7 @@ namespace ts {
|
||||
/**
|
||||
* Appends a source map.
|
||||
*/
|
||||
appendSourceMap(generatedLine: number, generatedCharacter: number, sourceMap: RawSourceMap, sourceMapPath: string): void;
|
||||
appendSourceMap(generatedLine: number, generatedCharacter: number, sourceMap: RawSourceMap, sourceMapPath: string, startLine: number): void;
|
||||
/**
|
||||
* Gets the source map as a `RawSourceMap` object.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user