-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(no-inspect): add
no-inspect
rule (#868)
Add rule that warns if `$inspect` is used. Closes #812 --------- Co-authored-by: Yosuke Ota <[email protected]>
- Loading branch information
Showing
11 changed files
with
121 additions
and
0 deletions.
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(no-inspect): add `no-inspect` 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,39 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/no-inspect' | ||
description: 'Warns against the use of `$inspect` directive' | ||
--- | ||
|
||
# svelte/no-inspect | ||
|
||
> Warns against the use of `$inspect` directive | ||
- :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 usages of `$inspect`. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-inspect: "error" */ | ||
// ✗ BAD | ||
$inspect(1); | ||
</script> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/no-inspect.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/no-inspect.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
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,31 @@ | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
|
||
import { createRule } from '../utils'; | ||
|
||
export default createRule('no-inspect', { | ||
meta: { | ||
docs: { | ||
description: 'Warns against the use of `$inspect` directive', | ||
category: 'Best Practices', | ||
// TODO: Enable recommended in major version | ||
recommended: false, | ||
default: 'warn' | ||
}, | ||
schema: [], | ||
messages: { | ||
unexpected: 'Do not use $inspect directive' | ||
}, | ||
type: 'suggestion' | ||
}, | ||
create(context) { | ||
return { | ||
Identifier(node: TSESTree.Identifier) { | ||
if (node.name !== '$inspect') { | ||
return; | ||
} | ||
|
||
context.report({ messageId: 'unexpected', node }); | ||
} | ||
}; | ||
} | ||
}); |
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
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/fixtures/rules/no-inspect/invalid/test01-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,12 @@ | ||
- message: Do not use $inspect directive | ||
line: 2 | ||
column: 3 | ||
suggestions: null | ||
- message: Do not use $inspect directive | ||
line: 5 | ||
column: 13 | ||
suggestions: null | ||
- message: Do not use $inspect directive | ||
line: 8 | ||
column: 5 | ||
suggestions: null |
10 changes: 10 additions & 0 deletions
10
packages/eslint-plugin-svelte/tests/fixtures/rules/no-inspect/invalid/test01-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,10 @@ | ||
<script> | ||
$inspect(1); | ||
$state(0); | ||
const a = $inspect(1); | ||
const _ = () => { | ||
$inspect(1); | ||
} | ||
</script> |
3 changes: 3 additions & 0 deletions
3
packages/eslint-plugin-svelte/tests/fixtures/rules/no-inspect/valid/test01-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 @@ | ||
<script> | ||
const _ = $state(1); | ||
</script> |
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/src/rules/no-inspect.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'; | ||
import rule from '../../../src/rules/no-inspect'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('no-inspect', rule as any, loadTestCases('no-inspect')); |