Skip to content

Commit

Permalink
Add ParseExceptionResult marker to J.Unknown elements
Browse files Browse the repository at this point in the history
This way they will get reported by `FindParseFailures`.
  • Loading branch information
knutwannheden committed Sep 26, 2024
1 parent cc8c310 commit acc05ef
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
46 changes: 38 additions & 8 deletions openrewrite/src/core/markers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export class Markers {
this._markers = markers;
}

static build(markers: Marker[]) {
if (markers.length === 0) {
return Markers.EMPTY;
}
return new Markers(randomId(), markers);
}

public get id(): UUID {
return this._id;
}
Expand Down Expand Up @@ -107,19 +114,22 @@ export class ParseExceptionResult implements Marker {
private readonly _exceptionType: string;
private readonly _exceptionMessage: string;
private readonly _message: string | null;
private readonly _treeType: string | null;

constructor(
id: UUID,
parserType: string,
exceptionType: string,
exceptionMessage: string,
message: string | null
message: string | null,
treeType?: string | null
) {
this._id = id;
this._parserType = parserType;
this._exceptionType = exceptionType;
this._exceptionMessage = exceptionMessage;
this._message = message;
this._treeType = treeType;
}

static build(parser: Parser, exception: Error): ParseExceptionResult {
Expand All @@ -142,7 +152,8 @@ export class ParseExceptionResult implements Marker {
this._parserType,
this._exceptionType,
this._exceptionMessage,
this._message
this._message,
this._treeType
);
}

Expand All @@ -156,7 +167,8 @@ export class ParseExceptionResult implements Marker {
parserType,
this._exceptionType,
this._exceptionMessage,
this._message
this._message,
this._treeType
);
}

Expand All @@ -170,7 +182,8 @@ export class ParseExceptionResult implements Marker {
this._parserType,
exceptionType,
this._exceptionMessage,
this._message
this._message,
this._treeType
);
}

Expand All @@ -184,12 +197,13 @@ export class ParseExceptionResult implements Marker {
this._parserType,
this._exceptionType,
exceptionMessage,
this._message
this._message,
this._treeType
);
}

get message(): string {
return this._message!;
get message(): string | null {
return this._message;
}

withMessage(message: string): ParseExceptionResult {
Expand All @@ -198,7 +212,23 @@ export class ParseExceptionResult implements Marker {
this._parserType,
this._exceptionType,
this._exceptionMessage,
message
message,
this._treeType
);
}

get treeType(): string | null {
return this._treeType;
}

withTreeType(treeType: string) {
return treeType === this._treeType ? this : new ParseExceptionResult(
this._id,
this._parserType,
this._exceptionType,
this._exceptionMessage,
this._message,
this._treeType
);
}
}
29 changes: 23 additions & 6 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import * as ts from 'typescript';
import * as J from '../java/tree';
import {Comment, Expression, JavaType, JContainer, JRightPadded, Space, TextComment} from '../java/tree';
import * as JS from './tree';
import {ExecutionContext, Markers, ParseError, Parser, ParserInput, randomId, SourceFile} from "../core";
import {
ExecutionContext,
Markers,
ParseError,
ParseExceptionResult,
Parser,
ParserInput,
randomId,
SourceFile
} from "../core";
import {Semicolon, TrailingComma} from "../java";
import {getNextSibling} from "./parserUtils";

Expand Down Expand Up @@ -39,7 +48,7 @@ export class JavaScriptParser extends Parser {
const sourceFile = program.getSourceFile(input.path);
if (sourceFile) {
try {
result.push(new JavaScriptParserVisitor(sourceFile, typeChecker).visit(sourceFile) as SourceFile);
result.push(new JavaScriptParserVisitor(this, sourceFile, typeChecker).visit(sourceFile) as SourceFile);
} catch (error) {
console.error(error);
result.push(ParseError.build(
Expand Down Expand Up @@ -99,7 +108,10 @@ type TextSpan = [number, number];

// noinspection JSUnusedGlobalSymbols
export class JavaScriptParserVisitor {
constructor(private readonly sourceFile: ts.SourceFile, private readonly typeChecker: ts.TypeChecker) {
constructor(
private readonly parser: Parser,
private readonly sourceFile: ts.SourceFile,
private readonly typeChecker: ts.TypeChecker) {
}

visit(node: ts.Node): any {
Expand Down Expand Up @@ -134,7 +146,7 @@ export class JavaScriptParserVisitor {
private semicolonPaddedStatementList(node: ts.SourceFile) {
return this.rightPaddedList(node.statements, this.semicolonPrefix, n => {
const last = n.getLastToken();
return last?.kind == ts.SyntaxKind.SemicolonToken ? Markers.EMPTY.withMarkers([new Semicolon(randomId())]) : Markers.EMPTY;
return last?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY;
});
}

Expand All @@ -146,7 +158,12 @@ export class JavaScriptParserVisitor {
new J.Unknown.Source(
randomId(),
Space.EMPTY,
Markers.EMPTY,
Markers.build([
ParseExceptionResult.build(
this.parser,
new Error("Unsupported AST element: " + node)
).withTreeType(visitMethodMap.get(node.kind)!.substring(5))
]),
node.getFullText()
)
);
Expand Down Expand Up @@ -1144,7 +1161,7 @@ export class JavaScriptParserVisitor {
args.push(this.rightPadded(
this.convert(argList.getChildAt(i)),
this.prefix(argList.getChildAt(i + 1)),
last ? Markers.EMPTY.withMarkers([new TrailingComma(randomId(), this.prefix(nodes[2]))]) : Markers.EMPTY
last ? Markers.build([new TrailingComma(randomId(), this.prefix(nodes[2]))]) : Markers.EMPTY
));
}
if ((childCount & 1) === 1) {
Expand Down

0 comments on commit acc05ef

Please sign in to comment.