|
1 | 1 | import assert from "assert";
|
2 |
| -import { parseForESLint } from "../../../src"; |
3 |
| -import { getScopeFromNode } from "../../../src/scope"; |
4 |
| - |
5 |
| -describe('getScopeFromNode', () => { |
6 |
| - it('returns the global scope for the root node', () => { |
7 |
| - const { ast, scopeManager } = parseForESLint(''); |
8 |
| - |
9 |
| - assert.strictEqual(getScopeFromNode(scopeManager, ast), scopeManager.globalScope); |
| 2 | +import * as svelte from "../../../src"; |
| 3 | +import { FlatESLint } from 'eslint/use-at-your-own-risk'; |
| 4 | + |
| 5 | +async function generateScopeTestCase(code, selector, type) { |
| 6 | + const eslint = new FlatESLint({ |
| 7 | + overrideConfigFile: true, |
| 8 | + overrideConfig: { |
| 9 | + languageOptions: { |
| 10 | + parser: svelte, |
| 11 | + }, |
| 12 | + plugins: { |
| 13 | + local: { |
| 14 | + rules: { |
| 15 | + rule: generateScopeRule(selector, type), |
| 16 | + } |
| 17 | + } |
| 18 | + }, |
| 19 | + rules: { |
| 20 | + 'local/rule': 'error', |
| 21 | + } |
| 22 | + } |
10 | 23 | });
|
11 |
| - |
12 |
| - it('returns the global scope for the script element', () => { |
13 |
| - const { ast, scopeManager } = parseForESLint('<script></script>'); |
14 |
| - const script = ast.body[0]; |
15 |
| - |
16 |
| - assert.strictEqual(getScopeFromNode(scopeManager, script), scopeManager.globalScope); |
| 24 | + await eslint.lintText(code); |
| 25 | +} |
| 26 | + |
| 27 | +function generateScopeRule(selector, type) { |
| 28 | + return { |
| 29 | + create(context) { |
| 30 | + return { |
| 31 | + [selector]() { |
| 32 | + const scope = context.getScope(); |
| 33 | + |
| 34 | + assert.strictEqual(scope.type, type); |
| 35 | + } |
| 36 | + }; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +describe('context.getScope', () => { |
| 42 | + it('returns the global scope for the root node', async () => { |
| 43 | + await generateScopeTestCase('', 'Program', 'global'); |
17 | 44 | });
|
18 | 45 |
|
19 |
| - it('returns the module scope for nodes for top level nodes of script', () => { |
20 |
| - const { ast, scopeManager } = parseForESLint('<script>import mod from "mod";</script>'); |
21 |
| - const importStatement = ast.body[0].body[0]; |
22 |
| - |
23 |
| - assert.strictEqual(getScopeFromNode(scopeManager, importStatement), scopeManager.globalScope.childScopes[0]); |
| 46 | + it('returns the global scope for the script element', async () => { |
| 47 | + await generateScopeTestCase('<script></script>', 'SvelteScriptElement', 'global'); |
24 | 48 | });
|
25 | 49 |
|
26 |
| - it('returns the module scope for nested nodes without their own scope', () => { |
27 |
| - const { ast, scopeManager } = parseForESLint('<script>a || b</script>'); |
28 |
| - const importStatement = ast.body[0].body[0].expression.right; |
29 |
| - |
30 |
| - assert.strictEqual(getScopeFromNode(scopeManager, importStatement), scopeManager.globalScope.childScopes[0]); |
| 50 | + it.only('returns the module scope for nodes for top level nodes of script', async () => { |
| 51 | + await generateScopeTestCase('<script>import mod from "mod";</script>', 'ImportDeclaration', 'module'); |
31 | 52 | });
|
32 | 53 |
|
33 |
| - it('returns the module scope for nested nodes for non-modules', () => { |
34 |
| - const { ast, scopeManager } = parseForESLint('<script>a || b</script>', { sourceType: 'script' }); |
35 |
| - const importStatement = ast.body[0].body[0].expression.right; |
36 |
| - |
37 |
| - assert.strictEqual(getScopeFromNode(scopeManager, importStatement), scopeManager.globalScope.childScopes[0]); |
| 54 | + it('returns the module scope for nested nodes without their own scope', async () => { |
| 55 | + await generateScopeTestCase('<script>a || b</script>', 'LogicalExpression', 'module'); |
38 | 56 | });
|
39 | 57 |
|
40 |
| - it('returns the the child scope of top level nodes with their own scope', () => { |
41 |
| - const { ast, scopeManager } = parseForESLint('<script>function fn() {}</script>'); |
42 |
| - const fnNode = ast.body[0].body[0]; |
43 |
| - |
44 |
| - assert.strictEqual(getScopeFromNode(scopeManager, fnNode), scopeManager.globalScope.childScopes[0].childScopes[0]); |
| 58 | + it('returns the the child scope of top level nodes with their own scope', async () => { |
| 59 | + await generateScopeTestCase('<script>function fn() {}</script>', 'FunctionDeclaration', 'function'); |
45 | 60 | });
|
46 | 61 |
|
47 |
| - it('returns the own scope for nested nodes', () => { |
48 |
| - const { ast, scopeManager } = parseForESLint('<script>a || (() => {})</script>'); |
49 |
| - const importStatement = ast.body[0].body[0].expression.right; |
50 |
| - |
51 |
| - assert.strictEqual(getScopeFromNode(scopeManager, importStatement), scopeManager.globalScope.childScopes[0].childScopes[0]); |
| 62 | + it('returns the own scope for nested nodes', async () => { |
| 63 | + await generateScopeTestCase('<script>a || (() => {})</script>', 'ArrowFunctionExpression', 'function'); |
52 | 64 | });
|
53 | 65 |
|
54 |
| - it('returns the the nearest child scope for statements inside non-global scopes', () => { |
55 |
| - const { ast, scopeManager } = parseForESLint('<script>function fn() { nested; }</script>'); |
56 |
| - const fnNode = ast.body[0].body[0]; |
57 |
| - const nestedStatement = fnNode.body.body[0]; |
58 |
| - |
59 |
| - assert.strictEqual(getScopeFromNode(scopeManager, nestedStatement), scopeManager.globalScope.childScopes[0].childScopes[0]); |
| 66 | + it('returns the the nearest child scope for statements inside non-global scopes', async () => { |
| 67 | + await generateScopeTestCase('<script>function fn() { nested; }</script>', 'ExpressionStatement', 'function'); |
60 | 68 | });
|
61 | 69 | });
|
0 commit comments