Skip to content

Commit e8ab658

Browse files
committed
fix(prefer-const): corrections
- Removed unnecessary if branches - Updated tests cases - Since we are now using the default implementation of the rule, there's no need to keep the original tests, since the default rule is already tested. - Removed unnecessary test cases
1 parent 1b1b4f7 commit e8ab658

File tree

8 files changed

+11
-766
lines changed

8 files changed

+11
-766
lines changed

packages/eslint-plugin-svelte/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"cover": "nyc --reporter=lcov pnpm run test",
3131
"debug": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
3232
"lint": "run-p lint:*",
33-
"lint:fix": "pnpm run lint:es --fix",
33+
"lint-fix": "pnpm run lint:es --fix",
3434
"lint:es": "eslint --cache .",
3535
"mocha": "pnpm run ts ./node_modules/mocha/bin/mocha.js",
3636
"new": "pnpm run ts ./tools/new-rule.ts",

packages/eslint-plugin-svelte/src/rules/prefer-const.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,24 @@ import { defineWrapperListener, getCoreRule } from '../utils/eslint-core';
55

66
const coreRule = getCoreRule('prefer-const');
77

8-
type Declaration = TSESTree.Expression;
9-
108
/**
119
* Finds and returns the callee of a declaration node within variable declarations or object patterns.
1210
*/
13-
function findDeclarationCallee(node: Declaration) {
11+
function findDeclarationCallee(node: TSESTree.Expression) {
1412
const { parent } = node;
1513
if (parent.type === 'VariableDeclarator' && parent.init?.type === 'CallExpression') {
1614
return parent.init.callee;
1715
}
1816

19-
if (
20-
parent.type === 'Property' &&
21-
parent.parent.type === 'ObjectPattern' &&
22-
parent.parent.parent.type === 'VariableDeclarator' &&
23-
parent.parent.parent.init?.type === 'CallExpression'
24-
) {
25-
return parent.parent.parent.init.callee;
26-
}
27-
2817
return null;
2918
}
3019

3120
/**
3221
* Determines if a declaration should be skipped in the const preference analysis.
3322
* Specifically checks for Svelte's state management utilities ($state, $props, $derived).
3423
*/
35-
function shouldSkipDeclaration(declaration: Declaration | null) {
36-
if (!declaration || !declaration.parent) {
24+
function shouldSkipDeclaration(declaration: TSESTree.Expression | null) {
25+
if (!declaration) {
3726
return false;
3827
}
3928

@@ -60,7 +49,7 @@ function shouldSkipDeclaration(declaration: Declaration | null) {
6049
if (
6150
callee.object.name === '$state' &&
6251
callee.property.type === 'Identifier' &&
63-
callee.property.name === 'frozen'
52+
callee.property.name === 'raw'
6453
) {
6554
return true;
6655
}

packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test01-input.svelte

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
let { prop1, prop2 } = $props();
33
let zero = 0;
44
let state = $state(0);
5-
let frozen = $state.frozen(0);
5+
let raw = $state.raw(0);
66
let doubled = state * 2;
77
let derived = $derived(state * 2);
88
let calculated = calc();
99
let derivedBy = $derived.by(calc());
10+
let noInit;
1011
</script>

packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test01-output.svelte

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
let { prop1, prop2 } = $props();
33
const zero = 0;
44
let state = $state(0);
5-
let frozen = $state.frozen(0);
5+
let raw = $state.raw(0);
66
const doubled = state * 2;
77
let derived = $derived(state * 2);
88
const calculated = calc();
99
let derivedBy = $derived.by(calc());
10+
let noInit;
1011
</script>

packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test02-errors.yaml

-4
This file was deleted.

packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test02-input.svelte

-7
This file was deleted.

packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/invalid/test02-output.svelte

-7
This file was deleted.

0 commit comments

Comments
 (0)