diff --git a/src/languages/css-language.js b/src/languages/css-language.js index e0cd0b9..5f7c4b7 100644 --- a/src/languages/css-language.js +++ b/src/languages/css-language.js @@ -102,6 +102,9 @@ export class CSSLanguage { loc, }); }, + onParseError(error) { + throw error; + } }), ); @@ -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 ], }; } diff --git a/tests/languages/css-language.test.js b/tests/languages/css-language.test.js index 8cf1fad..2bd81e0 100644 --- a/tests/languages/css-language.test.js +++ b/tests/languages/css-language.test.js @@ -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);