-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(no-shadow): ignore
{#snippet}
if it uses under component.
- Loading branch information
1 parent
2bd1799
commit 2dcd746
Showing
17 changed files
with
197 additions
and
6 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': patch | ||
--- | ||
|
||
fix(no-shadow): ignore `{#snippet}` if it uses under component. |
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,83 @@ | ||
import { createRule } from '../utils/index.js'; | ||
import { defineWrapperListener, getProxyContent, getCoreRule } from '../utils/eslint-core.js'; | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
import type { Scope } from '@typescript-eslint/scope-manager'; | ||
import type { Range } from 'svelte-eslint-parser/lib/ast/common.js'; | ||
import { getScope as getScopeUtil } from '../utils/ast-utils.js'; | ||
import { getSourceCode as getSourceCodeCompat } from '../utils/compat.js'; | ||
|
||
const coreRule = getCoreRule('no-shadow'); | ||
|
||
function removeSnippetIdentifiers(snippetIdentifierNodeLocations: Range[], scope: Scope): Scope { | ||
return { | ||
...scope, | ||
variables: scope.variables.filter((variable) => { | ||
return !snippetIdentifierNodeLocations.some(([start, end]) => { | ||
return variable.identifiers.every((identifier) => { | ||
const { range } = identifier; | ||
return range[0] === start && range[1] === end; | ||
}); | ||
}); | ||
}), | ||
childScopes: scope.childScopes.map((scope) => { | ||
return removeSnippetIdentifiers(snippetIdentifierNodeLocations, scope); | ||
}) | ||
} as Scope; | ||
} | ||
|
||
export default createRule('no-shadow', { | ||
meta: { | ||
...coreRule.meta, | ||
docs: { | ||
description: coreRule.meta.docs.description, | ||
category: 'Best Practices', | ||
recommended: false, | ||
extensionRule: 'no-shadow' | ||
} | ||
}, | ||
create(context) { | ||
const snippetIdentifierNodeLocations: Range[] = []; | ||
|
||
function getScope(node: TSESTree.Node) { | ||
const scope = getScopeUtil(context, node); | ||
return removeSnippetIdentifiers(snippetIdentifierNodeLocations, scope); | ||
} | ||
|
||
function getSourceCode() { | ||
const sourceCode = getSourceCodeCompat(context); | ||
return new Proxy(sourceCode, { | ||
get(target, key) { | ||
if (key === 'getScope') { | ||
return getScope; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore | ||
return (target as any)[key]; | ||
} | ||
}); | ||
} | ||
|
||
return defineWrapperListener( | ||
coreRule, | ||
getProxyContent(context, { | ||
sourceCode: getSourceCode() | ||
}), | ||
{ | ||
createListenerProxy(coreListener) { | ||
return { | ||
...coreListener, | ||
SvelteSnippetBlock(node) { | ||
const parent = node.parent; | ||
if (parent.type === 'SvelteElement' && parent.kind === 'component') { | ||
snippetIdentifierNodeLocations.push(node.id.range); | ||
} | ||
coreListener.SvelteSnippetBlock?.(node); | ||
}, | ||
'Program:exit'(node) { | ||
coreListener['Program:exit']?.(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
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
4 changes: 4 additions & 0 deletions
4
packages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/invalid/basic-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: "'x' is already declared in the upper scope on line 2 column 13." | ||
line: 4 | ||
column: 8 | ||
suggestions: null |
7 changes: 7 additions & 0 deletions
7
packages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/invalid/basic-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,7 @@ | ||
<script> | ||
function a(x) { | ||
var b = function c() { | ||
var x = 'foo'; | ||
}; | ||
} | ||
</script> |
4 changes: 4 additions & 0 deletions
4
packages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/invalid/const-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: "'foo' is already declared in the upper scope on line 6 column 10." | ||
line: 8 | ||
column: 11 | ||
suggestions: null |
10 changes: 10 additions & 0 deletions
10
packages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/invalid/const-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> | ||
import ComponentWithSnippet from './ComponentWithSnippet.svelte'; | ||
</script> | ||
|
||
<ComponentWithSnippet> | ||
{@const foo = 1} | ||
<ComponentWithSnippet> | ||
{@const foo = 2} | ||
</ComponentWithSnippet> | ||
</ComponentWithSnippet> |
10 changes: 10 additions & 0 deletions
10
packages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/valid/basic-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> | ||
var a = 3; | ||
var b = (x) => { | ||
a++; | ||
return x + a; | ||
}; | ||
setTimeout(() => { | ||
b(a); | ||
}, 0); | ||
</script> |
13 changes: 13 additions & 0 deletions
13
packages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/valid/snippet1-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 @@ | ||
<script> | ||
import ComponentWithSnippet from './ComponentWithSnippet.svelte'; | ||
</script> | ||
|
||
<ComponentWithSnippet> | ||
{#snippet children()} | ||
<AnotherComponentWithSnippet> | ||
{#snippet children()} | ||
Hello! | ||
{/snippet} | ||
</AnotherComponentWithSnippet> | ||
{/snippet} | ||
</ComponentWithSnippet> |
3 changes: 3 additions & 0 deletions
3
...ages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/valid/snippet1-requirements.json
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 @@ | ||
{ | ||
"svelte": ">=5.0.0-0" | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/valid/snippet2-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> | ||
import ComponentWithSnippet from './ComponentWithSnippet.svelte'; | ||
const children = 1; | ||
</script> | ||
|
||
<ComponentWithSnippet> | ||
{#snippet children()} | ||
Hello! | ||
{/snippet} | ||
</ComponentWithSnippet> |
3 changes: 3 additions & 0 deletions
3
...ages/eslint-plugin-svelte/tests/fixtures/rules/no-shadow/valid/snippet2-requirements.json
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 @@ | ||
{ | ||
"svelte": ">=5.0.0-0" | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/src/rules/no-shadow.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/no-shadow.js'; | ||
import { loadTestCases } from '../../utils/utils.js'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('no-shadow', rule as any, loadTestCases('no-shadow')); |