Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Nov 1, 2024
1 parent 0fa5a05 commit a598ff5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/languages/css-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export class CSSLanguage {
loc,
});
},
onParseError(error) {
throw error;
}
}),
);

Expand All @@ -111,18 +114,11 @@ export class CSSLanguage {
comments,
};
} catch (ex) {
// error messages end with (line:column) so we strip that off for ESLint
const message = ex.message
.slice(0, ex.message.lastIndexOf("("))
.trim();

return {
ok: false,
errors: [
{
...ex,
message,
},
ex
],
};
}
Expand Down
29 changes: 28 additions & 1 deletion tests/languages/css-language.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,38 @@ describe("CSSLanguage", () => {
assert.strictEqual(result.ast.type, "StyleSheet");
assert.strictEqual(result.ast.children[0].type, "Rule");
});

it("should return an error when parsing invalid CSS", () => {
const language = new CSSLanguage();
const result = language.parse({
body: "a { foo; bar: 1! }",
path: "test.css",
});

assert.strictEqual(result.ok, false);
assert.strictEqual(result.ast, undefined);
assert.strictEqual(result.errors.length, 1);
assert.strictEqual(result.errors[0].message, "Colon is expected");
});

// https://github.com/csstree/csstree/issues/301
it.skip("should return an error when EOF is discovered before block close", () => {
const language = new CSSLanguage();
const result = language.parse({
body: "a {",
path: "test.css",
});

assert.strictEqual(result.ok, false);
assert.strictEqual(result.ast, undefined);
assert.strictEqual(result.errors.length, 1);
assert.strictEqual(result.errors[0].message, "Colon is expected");
});
});

describe("createSourceCode()", () => {
it("should create a CSSSourceCode instance", () => {
const file = { body: "{\n\n}", path: "test.css" };
const file = { body: "a {\n\n}", path: "test.css" };
const language = new CSSLanguage();
const parseResult = language.parse(file);
const sourceCode = language.createSourceCode(file, parseResult);
Expand Down

0 comments on commit a598ff5

Please sign in to comment.