-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented the
valid-style-parse
rule (#1054)
- Loading branch information
1 parent
1395d5d
commit c587629
Showing
17 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': minor | ||
--- | ||
|
||
feat: added the valid-style-parse rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/valid-style-parse' | ||
description: 'require valid style element parsing' | ||
--- | ||
|
||
# svelte/valid-style-parse | ||
|
||
> require valid style element parsing | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports issues with parsing of the `<style>` element by the svelte-eslint-parser. | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/valid-style-parse: ["error"] */ | ||
</script> | ||
<!-- ✓ GOOD --> | ||
<style> | ||
.class { | ||
font-weight: bold; | ||
} | ||
</style> | ||
``` | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/valid-style-parse: ["error"] */ | ||
</script> | ||
<!-- ✓ GOOD --> | ||
<style lang="scss"> | ||
.class { | ||
font-weight: bold; | ||
} | ||
</style> | ||
``` | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/valid-style-parse: ["error"] */ | ||
</script> | ||
<!-- ✗ BAD --> | ||
<style> | ||
.class | ||
font-weight: bold; | ||
</style> | ||
``` | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/valid-style-parse: ["error"] */ | ||
</script> | ||
<!-- ✗ BAD --> | ||
<style lang="unknown"> | ||
.class { | ||
font-weight: bold; | ||
} | ||
</style> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/valid-style-parse.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/valid-style-parse.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/eslint-plugin-svelte/src/rules/valid-style-parse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createRule } from '../utils/index.js'; | ||
import { getCwd, getSourceCode } from '../utils/compat.js'; | ||
|
||
export default createRule('valid-style-parse', { | ||
meta: { | ||
docs: { | ||
description: 'require valid style element parsing', | ||
category: 'Possible Errors', | ||
recommended: false | ||
}, | ||
schema: [], | ||
messages: {}, | ||
type: 'problem' | ||
}, | ||
create(context) { | ||
const sourceCode = getSourceCode(context); | ||
if (!sourceCode.parserServices.isSvelte) { | ||
return {}; | ||
} | ||
const cwd = `${getCwd(context)}/`; | ||
|
||
return { | ||
SvelteStyleElement(node) { | ||
const styleContext = sourceCode.parserServices.getStyleContext!(); | ||
if (styleContext.status === 'parse-error') { | ||
context.report({ | ||
loc: node.loc, | ||
message: `Error parsing style element. Error message: "${styleContext.error.message.replace(cwd, '')}"` | ||
}); | ||
} | ||
if (styleContext.status === 'unknown-lang') { | ||
context.report({ | ||
loc: node.loc, | ||
message: `Found unsupported style element language "${styleContext.sourceLang}"` | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...nt-plugin-svelte/tests/fixtures/rules/valid-style-parse/invalid/invalid-css01-errors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- message: 'Error parsing style element. Error message: | ||
"tests/fixtures/rules/valid-style-parse/invalid/invalid-css01-input.svelte:4:11: | ||
Unknown word"' | ||
line: 7 | ||
column: 1 | ||
suggestions: null |
18 changes: 18 additions & 0 deletions
18
...t-plugin-svelte/tests/fixtures/rules/valid-style-parse/invalid/invalid-css01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style> | ||
// This syntax is intentionally invalid CSS - this is to be used to test resiliency against invalid input | ||
.container { | ||
class .div-class/35 | ||
# Weird comment | ||
color: red; | ||
.span-class begin | ||
font-weight: bold; | ||
end | ||
} | ||
</style> |
6 changes: 6 additions & 0 deletions
6
...t-plugin-svelte/tests/fixtures/rules/valid-style-parse/invalid/invalid-scss01-errors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- message: 'Error parsing style element. Error message: | ||
"tests/fixtures/rules/valid-style-parse/invalid/invalid-scss01-input.svelte:4:11: | ||
Unknown word"' | ||
line: 7 | ||
column: 1 | ||
suggestions: null |
18 changes: 18 additions & 0 deletions
18
...-plugin-svelte/tests/fixtures/rules/valid-style-parse/invalid/invalid-scss01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style lang="scss"> | ||
// This syntax is intentionally invalid CSS - this is to be used to test resiliency against invalid input | ||
.container { | ||
class .div-class/35 | ||
# Weird comment | ||
color: red; | ||
.span-class begin | ||
font-weight: bold; | ||
end | ||
} | ||
</style> |
4 changes: 4 additions & 0 deletions
4
...t-plugin-svelte/tests/fixtures/rules/valid-style-parse/invalid/unknown-lang01-errors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- message: Found unsupported style element language "invalid-lang" | ||
line: 7 | ||
column: 1 | ||
suggestions: null |
18 changes: 18 additions & 0 deletions
18
...-plugin-svelte/tests/fixtures/rules/valid-style-parse/invalid/unknown-lang01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style lang="invalid-lang"> | ||
// This syntax is intentionally invalid CSS - this is to be used to test resiliency against unknown style languages. | ||
.container { | ||
class .div-class/35 | ||
# Weird comment | ||
color: red; | ||
.span-class begin | ||
font-weight: bold; | ||
end | ||
} | ||
</style> |
3 changes: 3 additions & 0 deletions
3
...eslint-plugin-svelte/tests/fixtures/rules/valid-style-parse/valid/no-style01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<a href="https://svelte.dev">Hello</a> | ||
|
||
<span style="font-weight: bold;">World!</span> |
13 changes: 13 additions & 0 deletions
13
...slint-plugin-svelte/tests/fixtures/rules/valid-style-parse/valid/valid-css01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
|
||
<style> | ||
.div-class { | ||
color: red; | ||
} | ||
.span-class { | ||
font-weight: bold; | ||
} | ||
</style> |
18 changes: 18 additions & 0 deletions
18
...lint-plugin-svelte/tests/fixtures/rules/valid-style-parse/valid/valid-scss01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div class="container"> | ||
<div class="div-class">Hello</div> | ||
|
||
<span class="span-class">World!</span> | ||
</div> | ||
|
||
<style lang="scss"> | ||
.container { | ||
.div-class { | ||
// This is an inline comment | ||
color: red; | ||
} | ||
.span-class { | ||
font-weight: bold; | ||
} | ||
} | ||
</style> |
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/src/rules/valid-style-parse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { RuleTester } from '../../utils/eslint-compat.js'; | ||
import rule from '../../../src/rules/valid-style-parse.js'; | ||
import { loadTestCases } from '../../utils/utils.js'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('valid-style-parse', rule as any, loadTestCases('valid-style-parse')); |