Skip to content

Commit 61b95fe

Browse files
authored
Merge pull request #29555 from petebacondarwin/external-skip-trivia-fix
Use the correct source when skipping trivia
2 parents 71d70ef + 331b9bc commit 61b95fe

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,10 +4372,10 @@ namespace ts {
43724372
}
43734373

43744374
/**
4375-
* Skips trivia such as comments and white-space that can optionally overriden by the source map source
4375+
* Skips trivia such as comments and white-space that can be optionally overridden by the source-map source
43764376
*/
43774377
function skipSourceTrivia(source: SourceMapSource, pos: number): number {
4378-
return source.skipTrivia ? source.skipTrivia(pos) : skipTrivia(sourceMapSource.text, pos);
4378+
return source.skipTrivia ? source.skipTrivia(pos) : skipTrivia(source.text, pos);
43794379
}
43804380

43814381
/**

src/testRunner/unittests/customTransforms.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,34 @@ namespace ts {
129129
},
130130
{ sourceMap: true }
131131
);
132+
133+
emitsCorrectly("skipTriviaExternalSourceFiles",
134+
[
135+
{
136+
file: "source.ts",
137+
// The source file contains preceding trivia (e.g. whitespace) to try to confuse the `skipSourceTrivia` function.
138+
text: " original;"
139+
},
140+
],
141+
{
142+
before: [
143+
context => node => visitNode(node, function visitor(node: Node): Node {
144+
if (isIdentifier(node) && node.text === "original") {
145+
const newNode = createIdentifier("changed");
146+
setSourceMapRange(newNode, {
147+
pos: 0,
148+
end: 7,
149+
// Do not provide a custom skipTrivia function for `source`.
150+
source: createSourceMapSource("another.html", "changed;")
151+
});
152+
return newNode;
153+
}
154+
return visitEachChild(node, visitor, context);
155+
})
156+
]
157+
},
158+
{ sourceMap: true }
159+
);
160+
132161
});
133162
}

tests/baselines/reference/customTransforms/skipTriviaExternalSourceFiles.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)