diff --git a/docs-svelte-kit/.eslintrc.cjs b/docs-svelte-kit/.eslintrc.cjs index 4f0d7d4c5..e38d0514a 100644 --- a/docs-svelte-kit/.eslintrc.cjs +++ b/docs-svelte-kit/.eslintrc.cjs @@ -9,6 +9,8 @@ module.exports = { "@ota-meshi/svelte/button-has-type": "error", "@ota-meshi/svelte/no-useless-mustaches": "error", "@ota-meshi/svelte/prefer-class-directive": "error", + // TODO Still get an error with prettier-plugin-svelte. Probably fixed in v2.6. + // "@ota-meshi/svelte/prefer-style-directive": "error", "@ota-meshi/svelte/spaced-html-comment": "error", }, } diff --git a/package.json b/package.json index 715a826c7..5b9c70c18 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "@babel/types": "^7.16.0", "@fontsource/fira-mono": "^4.5.0", "@ota-meshi/eslint-plugin": "^0.10.0", - "@ota-meshi/eslint-plugin-svelte": "^0.21.0", + "@ota-meshi/eslint-plugin-svelte": "^0.22.0", "@sindresorhus/slugify": "^2.1.0", "@sveltejs/adapter-static": "^1.0.0-next.21", "@sveltejs/kit": "^1.0.0-next.201", @@ -113,7 +113,7 @@ "pako": "^2.0.3", "pirates": "^4.0.1", "prettier": "^2.2.1", - "prettier-plugin-svelte": "^2.2.0", + "prettier-plugin-svelte": "^2.5.1", "prism-svelte": "^0.4.7", "prismjs": "^1.25.0", "semver": "^7.3.5", diff --git a/src/rules/prefer-style-directive.ts b/src/rules/prefer-style-directive.ts index 2958da4df..5e1445dd1 100644 --- a/src/rules/prefer-style-directive.ts +++ b/src/rules/prefer-style-directive.ts @@ -2,6 +2,15 @@ import type { AST } from "svelte-eslint-parser" import { parse as parseCss } from "postcss" import { createRule } from "../utils" +/** Parse for CSS */ +function safeParseCss(cssCode: string) { + try { + return parseCss(cssCode) + } catch { + return null + } +} + export default createRule("prefer-style-directive", { meta: { docs: { @@ -39,7 +48,10 @@ export default createRule("prefer-style-directive", { return sourceCode.getText(value) }) .join("") - const root = parseCss(cssCode) + const root = safeParseCss(cssCode) + if (!root) { + return + } root.walkDecls((decl) => { if ( node.parent.attributes.some( diff --git a/tests/fixtures/rules/prefer-style-directive/valid/invalid-style01-input.svelte b/tests/fixtures/rules/prefer-style-directive/valid/invalid-style01-input.svelte new file mode 100644 index 000000000..5ab6c7591 --- /dev/null +++ b/tests/fixtures/rules/prefer-style-directive/valid/invalid-style01-input.svelte @@ -0,0 +1,7 @@ + + +
...
+ +
...