Skip to content

Commit

Permalink
Correct relative path of sources (#162)
Browse files Browse the repository at this point in the history
* Correct relative path of sources

* Add missing change
  • Loading branch information
knutwannheden authored Nov 28, 2024
1 parent dd4dea1 commit 86dc378
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions openrewrite/src/core/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export class ParseError implements SourceFile {
return new ParseError(
randomId(),
new Markers(randomId(), [ParseExceptionResult.build(parser, exception)]),
relativeTo ? path.resolve(input.path, relativeTo) : input.path,
relativeTo ? path.relative(relativeTo, input.path) : input.path,
input.fileAttributes,
parser.getCharset(ctx),
false,
Expand Down Expand Up @@ -761,4 +761,4 @@ class ParseErrorPrinter<P> extends ParseErrorVisitor<PrintOutputCapture<P>> {
}
return e;
}
}
}
5 changes: 3 additions & 2 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "../core";
import {binarySearch, compareTextSpans, getNextSibling, getPreviousSibling, TextSpan} from "./parserUtils";
import {JavaScriptTypeMapping} from "./typeMapping";
import path from "node:path";

export class JavaScriptParser extends Parser {

Expand Down Expand Up @@ -56,7 +57,7 @@ export class JavaScriptParser extends Parser {
const input = new ParserInput(filePath, null, false, () => Buffer.from(ts.sys.readFile(filePath)!));
try {
const parsed = new JavaScriptParserVisitor(this, sourceFile, typeChecker).visit(sourceFile) as SourceFile;
result.push(parsed);
result.push(parsed.withSourcePath(relativeTo != null ? path.relative(relativeTo, input.path) : input.path));
} catch (error) {
result.push(ParseError.build(this, input, relativeTo, ctx, error instanceof Error ? error : new Error('Parser threw unknown error: ' + error), null));
}
Expand Down Expand Up @@ -142,7 +143,7 @@ export class JavaScriptParser extends Parser {
if (sourceFile) {
try {
const parsed = new JavaScriptParserVisitor(this, sourceFile, typeChecker).visit(sourceFile) as SourceFile;
result.push(parsed);
result.push(parsed.withSourcePath(relativeTo != null ? path.relative(relativeTo, input.path) : input.path));
} catch (error) {
result.push(ParseError.build(this, input, relativeTo, ctx, error instanceof Error ? error : new Error('Parser threw unknown error: ' + error), null));
}
Expand Down

0 comments on commit 86dc378

Please sign in to comment.