Skip to content

feat(no-inspect): add no-inspect rule #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [svelte/no-ignored-unsubscribe](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-ignored-unsubscribe/) | disallow ignoring the unsubscribe method returned by the `subscribe()` on Svelte stores. | |
| [svelte/no-immutable-reactive-statements](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-immutable-reactive-statements/) | disallow reactive statements that don't reference reactive values. | |
| [svelte/no-inline-styles](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inline-styles/) | disallow attributes and directives that produce inline styles | |
| [svelte/no-inspect](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inspect/) | (no description) | :star: |
| [svelte/no-reactive-functions](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-functions/) | it's not necessary to define functions in reactive statements | :bulb: |
| [svelte/no-reactive-literals](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-reactive-literals/) | don't assign literal values in reactive statements | :bulb: |
| [svelte/no-svelte-internal](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-svelte-internal/) | svelte/internal will be removed in Svelte 6. | |
Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [svelte/no-ignored-unsubscribe](./rules/no-ignored-unsubscribe.md) | disallow ignoring the unsubscribe method returned by the `subscribe()` on Svelte stores. | |
| [svelte/no-immutable-reactive-statements](./rules/no-immutable-reactive-statements.md) | disallow reactive statements that don't reference reactive values. | |
| [svelte/no-inline-styles](./rules/no-inline-styles.md) | disallow attributes and directives that produce inline styles | |
| [svelte/no-inspect](./rules/no-inspect.md) | (no description) | :star: |
| [svelte/no-reactive-functions](./rules/no-reactive-functions.md) | it's not necessary to define functions in reactive statements | :bulb: |
| [svelte/no-reactive-literals](./rules/no-reactive-literals.md) | don't assign literal values in reactive statements | :bulb: |
| [svelte/no-svelte-internal](./rules/no-svelte-internal.md) | svelte/internal will be removed in Svelte 6. | |
Expand Down
40 changes: 40 additions & 0 deletions docs/rules/no-inspect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
pageClass: 'rule-details'
sidebarDepth: 0
title: 'svelte/no-inspect'
description: ''
---

# svelte/no-inspect

>

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
- :gear: This rule is included in `"plugin:svelte/recommended"`.

## :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)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const config: Linter.Config[] = [
'svelte/no-dupe-style-properties': 'error',
'svelte/no-dynamic-slot-name': 'error',
'svelte/no-inner-declarations': 'error',
'svelte/no-inspect': 'warn',
'svelte/no-not-function-handler': 'error',
'svelte/no-object-in-text-mustaches': 'error',
'svelte/no-shorthand-style-property-overrides': 'error',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-svelte/src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const config: Linter.LegacyConfig = {
'svelte/no-dupe-style-properties': 'error',
'svelte/no-dynamic-slot-name': 'error',
'svelte/no-inner-declarations': 'error',
'svelte/no-inspect': 'warn',
'svelte/no-not-function-handler': 'error',
'svelte/no-object-in-text-mustaches': 'error',
'svelte/no-shorthand-style-property-overrides': 'error',
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-plugin-svelte/src/rule-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ export interface RuleOptions {
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inner-declarations/
*/
'svelte/no-inner-declarations'?: Linter.RuleEntry<SvelteNoInnerDeclarations>
/**
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-inspect/
*/
'svelte/no-inspect'?: Linter.RuleEntry<[]>
/**
* disallow use of not function in event handler
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-not-function-handler/
Expand Down
30 changes: 30 additions & 0 deletions packages/eslint-plugin-svelte/src/rules/no-inspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { TSESTree } from '@typescript-eslint/types';

import { createRule } from '../utils';

export default createRule('no-inspect', {
meta: {
docs: {
description: '',
category: 'Best Practices',
recommended: true,
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 });
}
};
}
});
2 changes: 2 additions & 0 deletions packages/eslint-plugin-svelte/src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import noIgnoredUnsubscribe from '../rules/no-ignored-unsubscribe';
import noImmutableReactiveStatements from '../rules/no-immutable-reactive-statements';
import noInlineStyles from '../rules/no-inline-styles';
import noInnerDeclarations from '../rules/no-inner-declarations';
import noInspect from '../rules/no-inspect';
import noNotFunctionHandler from '../rules/no-not-function-handler';
import noObjectInTextMustaches from '../rules/no-object-in-text-mustaches';
import noReactiveFunctions from '../rules/no-reactive-functions';
Expand Down Expand Up @@ -97,6 +98,7 @@ export const rules = [
noImmutableReactiveStatements,
noInlineStyles,
noInnerDeclarations,
noInspect,
noNotFunctionHandler,
noObjectInTextMustaches,
noReactiveFunctions,
Expand Down
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
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>
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 packages/eslint-plugin-svelte/tests/src/rules/no-inspect.ts
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'));
Loading