Skip to content

Commit

Permalink
fix: improve parse error locations and aggregate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Dec 30, 2024
1 parent c9473f7 commit 6cf4978
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .changeset/strong-steaks-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"marko": patch
"@marko/compiler": patch
"@marko/runtime-tags": patch
"@marko/translator-interop-class-tags": patch
---

Support aggregate errors when final error is a HTMLJS parser error.
5 changes: 5 additions & 0 deletions .changeset/tall-stingrays-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"marko": patch
---

Fix invalid sourcemapping of parse errors in a class block.
30 changes: 29 additions & 1 deletion packages/compiler/src/babel-plugin/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
import { createParser, TagType } from "htmljs-parser";

import * as t from "../babel-types";
import { buildCodeFrameError } from "../util/build-code-frame";
import throwAggregateError from "../util/merge-errors";

const noop = () => {};
const emptyRange = (part) => part.start === part.end;
Expand Down Expand Up @@ -124,7 +126,33 @@ export function parseMarko(file) {

const parser = createParser({
onError(part) {
throw file.buildCodeFrameError({ loc: locationAt(part) }, part.message);
const err = buildCodeFrameError(
file.opts.filename,
file.code,
locationAt(part),
part.message,
);

if (!file.___hasParseErrors) {
throw err;
}

const errors = [];
t.traverseFast(file.path.node, (node) => {
if (node.type === "MarkoParseError") {
errors.push(
buildCodeFrameError(
file.opts.filename,
file.code,
node.errorLoc || node.loc,
node.label,
),
);
}
});

errors.push(err);
throwAggregateError(errors);
},
onText(part) {
const rawValue = parser.read(part);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default function (path) {
} = path;
const {
rawValue: code,
name: { start, end },
name: { start },
end,
} = node;
const meta = file.metadata.marko;

Expand All @@ -40,7 +41,9 @@ export default function (path) {

const parsed = parseExpression(file, code.replace(/;\s*$/, ""), start, end);
if (parsed.type === "MarkoParseError") {
path.replaceWith(t.markoClass([t.expressionStatement(parsed)]));
const replacement = t.markoClass(t.classBody([]));
replacement.body.body.push(parsed);
path.replaceWith(replacement);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CompileErrors:
at __tests__/template.marko:7:9
5 | }};
6 |
> 7 | this.hello = {
| ^ Unexpected token
8 | world: true
9 | };
10 | }

at __tests__/template.marko:11:3
9 | };
10 | }
> 11 | onFoo() {
| ^ Line has extra indentation at the beginning
12 | console.log("bar")
13 | }
14 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CompileErrors:
at __tests__/template.marko:7:9
5 | }};
6 |
> 7 | this.hello = {
| ^ Unexpected token
8 | world: true
9 | };
10 | }

at __tests__/template.marko:11:3
9 | };
10 | }
> 11 | onFoo() {
| ^ Line has extra indentation at the beginning
12 | console.log("bar")
13 | }
14 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CompileErrors:
at __tests__/template.marko:7:9
5 | }};
6 |
> 7 | this.hello = {
| ^ Unexpected token
8 | world: true
9 | };
10 | }

at __tests__/template.marko:11:3
9 | };
10 | }
> 11 | onFoo() {
| ^ Line has extra indentation at the beginning
12 | console.log("bar")
13 | }
14 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CompileErrors:
at __tests__/template.marko:7:9
5 | }};
6 |
> 7 | this.hello = {
| ^ Unexpected token
8 | world: true
9 | };
10 | }

at __tests__/template.marko:11:3
9 | };
10 | }
> 11 | onFoo() {
| ^ Line has extra indentation at the beginning
12 | console.log("bar")
13 | }
14 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CompileErrors:
at __tests__/template.marko:7:9
5 | }};
6 |
> 7 | this.hello = {
| ^ Unexpected token
8 | world: true
9 | };
10 | }

at __tests__/template.marko:11:3
9 | };
10 | }
> 11 | onFoo() {
| ^ Line has extra indentation at the beginning
12 | console.log("bar")
13 | }
14 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CompileErrors:
at __tests__/template.marko:7:9
5 | }};
6 |
> 7 | this.hello = {
| ^ Unexpected token
8 | world: true
9 | };
10 | }

at __tests__/template.marko:11:3
9 | };
10 | }
> 11 | onFoo() {
| ^ Line has extra indentation at the beginning
12 | console.log("bar")
13 | }
14 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CompileErrors:
at __tests__/template.marko:7:9
5 | }};
6 |
> 7 | this.hello = {
| ^ Unexpected token
8 | world: true
9 | };
10 | }

at __tests__/template.marko:11:3
9 | };
10 | }
> 11 | onFoo() {
| ^ Line has extra indentation at the beginning
12 | console.log("bar")
13 | }
14 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class {
onCreate() {
this.state = {
v : null
}};
this.hello = {
world: true
};
}
onFoo() {
console.log("bar")
}
}

<div/>

0 comments on commit 6cf4978

Please sign in to comment.