Skip to content

Commit 5945c71

Browse files
marekdedicota-meshi
authored andcommitted
feat(valid-style-parse): implemented the rule
1 parent f170cab commit 5945c71

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

.changeset/stupid-penguins-smoke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': minor
3+
---
4+
5+
feat: added the valid-style-parse rule

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
335335
| [svelte/require-store-callbacks-use-set-param](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-store-callbacks-use-set-param/) | store callbacks must use `set` param | |
336336
| [svelte/require-store-reactive-access](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-store-reactive-access/) | disallow to use of the store itself as an operand. Need to use $ prefix or get function. | :star::wrench: |
337337
| [svelte/valid-compile](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-compile/) | disallow warnings when compiling. | |
338+
| [svelte/valid-style-parse](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-style-parse/) | require valid style element parsing | |
338339

339340
## Security Vulnerability
340341

docs/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
3232
| [svelte/require-store-callbacks-use-set-param](./rules/require-store-callbacks-use-set-param.md) | store callbacks must use `set` param | |
3333
| [svelte/require-store-reactive-access](./rules/require-store-reactive-access.md) | disallow to use of the store itself as an operand. Need to use $ prefix or get function. | :star::wrench: |
3434
| [svelte/valid-compile](./rules/valid-compile.md) | disallow warnings when compiling. | |
35+
| [svelte/valid-style-parse](./rules/valid-style-parse.md) | require valid style element parsing | |
3536

3637
## Security Vulnerability
3738

packages/eslint-plugin-svelte/src/rule-types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ export interface RuleOptions {
366366
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-prop-names-in-kit-pages/
367367
*/
368368
'svelte/valid-prop-names-in-kit-pages'?: Linter.RuleEntry<[]>
369+
/**
370+
* require valid style element parsing
371+
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-style-parse/
372+
*/
373+
'svelte/valid-style-parse'?: Linter.RuleEntry<[]>
369374
}
370375

371376
/* ======= Declarations ======= */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { createRule } from '../utils/index.js';
2+
import { getSourceCode } from '../utils/compat.js';
3+
4+
export default createRule('valid-style-parse', {
5+
meta: {
6+
docs: {
7+
description: 'require valid style element parsing',
8+
category: 'Possible Errors',
9+
recommended: false
10+
},
11+
schema: [],
12+
messages: {
13+
parseError: 'Error parsing style element'
14+
},
15+
type: 'problem'
16+
},
17+
create(context) {
18+
const sourceCode = getSourceCode(context);
19+
if (!sourceCode.parserServices.isSvelte) {
20+
return {};
21+
}
22+
23+
return {
24+
SvelteStyleElement(node) {
25+
const styleContext = sourceCode.parserServices.getStyleContext!();
26+
if (styleContext.status === 'parse-error') {
27+
context.report({
28+
loc: node.loc,
29+
messageId: 'parseError'
30+
});
31+
}
32+
if (styleContext.status === 'unknown-lang') {
33+
context.report({
34+
loc: node.loc,
35+
message: `Found unsupported style element language "${styleContext.sourceLang}"`
36+
});
37+
}
38+
}
39+
};
40+
}
41+
});

packages/eslint-plugin-svelte/src/utils/rules.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import system from '../rules/system.js';
7272
import validCompile from '../rules/valid-compile.js';
7373
import validEachKey from '../rules/valid-each-key.js';
7474
import validPropNamesInKitPages from '../rules/valid-prop-names-in-kit-pages.js';
75+
import validStyleParse from '../rules/valid-style-parse.js';
7576

7677
export const rules = [
7778
typescriptEslintNoUnnecessaryCondition,
@@ -143,5 +144,6 @@ export const rules = [
143144
system,
144145
validCompile,
145146
validEachKey,
146-
validPropNamesInKitPages
147+
validPropNamesInKitPages,
148+
validStyleParse
147149
] as RuleModule[];

0 commit comments

Comments
 (0)