Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 86dc378

Browse files
Correct relative path of sources (#162)
* Correct relative path of sources * Add missing change
1 parent dd4dea1 commit 86dc378

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

openrewrite/src/core/tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ export class ParseError implements SourceFile {
530530
return new ParseError(
531531
randomId(),
532532
new Markers(randomId(), [ParseExceptionResult.build(parser, exception)]),
533-
relativeTo ? path.resolve(input.path, relativeTo) : input.path,
533+
relativeTo ? path.relative(relativeTo, input.path) : input.path,
534534
input.fileAttributes,
535535
parser.getCharset(ctx),
536536
false,
@@ -761,4 +761,4 @@ class ParseErrorPrinter<P> extends ParseErrorVisitor<PrintOutputCapture<P>> {
761761
}
762762
return e;
763763
}
764-
}
764+
}

openrewrite/src/javascript/parser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "../core";
2626
import {binarySearch, compareTextSpans, getNextSibling, getPreviousSibling, TextSpan} from "./parserUtils";
2727
import {JavaScriptTypeMapping} from "./typeMapping";
28+
import path from "node:path";
2829

2930
export class JavaScriptParser extends Parser {
3031

@@ -56,7 +57,7 @@ export class JavaScriptParser extends Parser {
5657
const input = new ParserInput(filePath, null, false, () => Buffer.from(ts.sys.readFile(filePath)!));
5758
try {
5859
const parsed = new JavaScriptParserVisitor(this, sourceFile, typeChecker).visit(sourceFile) as SourceFile;
59-
result.push(parsed);
60+
result.push(parsed.withSourcePath(relativeTo != null ? path.relative(relativeTo, input.path) : input.path));
6061
} catch (error) {
6162
result.push(ParseError.build(this, input, relativeTo, ctx, error instanceof Error ? error : new Error('Parser threw unknown error: ' + error), null));
6263
}
@@ -142,7 +143,7 @@ export class JavaScriptParser extends Parser {
142143
if (sourceFile) {
143144
try {
144145
const parsed = new JavaScriptParserVisitor(this, sourceFile, typeChecker).visit(sourceFile) as SourceFile;
145-
result.push(parsed);
146+
result.push(parsed.withSourcePath(relativeTo != null ? path.relative(relativeTo, input.path) : input.path));
146147
} catch (error) {
147148
result.push(ParseError.build(this, input, relativeTo, ctx, error instanceof Error ? error : new Error('Parser threw unknown error: ' + error), null));
148149
}

0 commit comments

Comments
 (0)