diff --git a/packages/pluginutils/package.json b/packages/pluginutils/package.json index f0faa7ec6..3e2a4c01c 100644 --- a/packages/pluginutils/package.json +++ b/packages/pluginutils/package.json @@ -58,6 +58,7 @@ "@rollup/plugin-typescript": "^5.0.2", "@types/node": "^14.0.26", "@types/picomatch": "^2.2.1", + "acorn": "^8.0.4", "rollup": "^2.23.0" }, "types": "types/index.d.ts", diff --git a/packages/pluginutils/test/attachScopes.ts b/packages/pluginutils/test/attachScopes.ts index 73cae46ea..a67cbcf06 100755 --- a/packages/pluginutils/test/attachScopes.ts +++ b/packages/pluginutils/test/attachScopes.ts @@ -1,36 +1,10 @@ import test from 'ava'; +import { parse } from 'acorn'; import { attachScopes } from '../'; test('attaches a scope to the top level', (t) => { - const ast = { - type: 'Program', - start: 0, - end: 8, - body: [ - { - type: 'VariableDeclaration', - start: 0, - end: 8, - declarations: [ - { - type: 'VariableDeclarator', - start: 4, - end: 7, - id: { - type: 'Identifier', - start: 4, - end: 7, - name: 'foo' - }, - init: null - } - ], - kind: 'var' - } - ], - sourceType: 'module' - }; + const ast = parse('var foo;', { ecmaVersion: 2020, sourceType: 'module' }); const scope = attachScopes(ast, 'scope'); t.truthy(scope.contains('foo')); @@ -38,47 +12,7 @@ test('attaches a scope to the top level', (t) => { }); test('adds multiple declarators from a single var declaration', (t) => { - const ast = { - type: 'Program', - start: 0, - end: 13, - body: [ - { - type: 'VariableDeclaration', - start: 0, - end: 13, - declarations: [ - { - type: 'VariableDeclarator', - start: 4, - end: 7, - id: { - type: 'Identifier', - start: 4, - end: 7, - name: 'foo' - }, - init: null - }, - - { - type: 'VariableDeclarator', - start: 9, - end: 12, - id: { - type: 'Identifier', - start: 9, - end: 12, - name: 'bar' - }, - init: null - } - ], - kind: 'var' - } - ], - sourceType: 'module' - }; + const ast = parse('var foo, bar;', { ecmaVersion: 2020, sourceType: 'module' }); const scope = attachScopes(ast, 'scope'); t.truthy(scope.contains('foo')); @@ -86,129 +20,10 @@ test('adds multiple declarators from a single var declaration', (t) => { }); test('adds named declarators from a deconstructed declaration', (t) => { - const ast = { - type: 'Program', - start: 0, - end: 13, - body: [ - { - type: 'VariableDeclaration', - start: 0, - end: 42, - declarations: [ - { - type: 'VariableDeclarator', - start: 4, - end: 41, - id: { - type: 'ObjectPattern', - start: 4, - end: 15, - properties: [ - { - type: 'Property', - start: 6, - end: 10, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Literal', - start: 6, - end: 7, - value: 1, - raw: '1' - }, - value: { - type: 'Identifier', - start: 9, - end: 10, - name: 'a' - }, - kind: 'init' - }, - { - type: 'Property', - start: 12, - end: 13, - method: false, - shorthand: true, - computed: false, - key: { - type: 'Identifier', - start: 12, - end: 13, - name: 'b' - }, - kind: 'init', - value: { - type: 'Identifier', - start: 12, - end: 13, - name: 'b' - } - } - ] - }, - init: { - type: 'ObjectExpression', - start: 18, - end: 41, - properties: [ - { - type: 'Property', - start: 22, - end: 28, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Literal', - start: 22, - end: 23, - value: 1, - raw: '1' - }, - value: { - type: 'Literal', - start: 25, - end: 28, - value: 'a', - raw: "'a'" - }, - kind: 'init' - }, - { - type: 'Property', - start: 32, - end: 38, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 32, - end: 33, - name: 'b' - }, - value: { - type: 'Literal', - start: 35, - end: 38, - value: 'b', - raw: "'b'" - }, - kind: 'init' - } - ] - } - } - ], - kind: 'var' - } - ], + const ast = parse("var {1: a, b} = {1: 'a', b: 'b'};", { + ecmaVersion: 2020, sourceType: 'module' - }; + }); const scope = attachScopes(ast, 'scope'); t.truthy(scope.contains('a')); @@ -216,204 +31,10 @@ test('adds named declarators from a deconstructed declaration', (t) => { }); test('adds rest elements from a deconstructed object declaration', (t) => { - const ast = { - type: 'Program', - start: 0, - end: 66, - body: [ - { - type: 'VariableDeclaration', - start: 0, - end: 66, - declarations: [ - { - type: 'VariableDeclarator', - start: 6, - end: 66, - id: { - type: 'ObjectPattern', - start: 6, - end: 26, - properties: [ - { - type: 'Property', - start: 8, - end: 9, - method: false, - shorthand: true, - computed: false, - key: { - type: 'Identifier', - start: 8, - end: 9, - name: 'x' - }, - kind: 'init', - value: { - type: 'Identifier', - start: 8, - end: 9, - name: 'x' - } - }, - { - type: 'Property', - start: 11, - end: 15, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 11, - end: 12, - name: 'y' - }, - value: { - type: 'Identifier', - start: 14, - end: 15, - name: 'z' - }, - kind: 'init' - }, - { - type: 'RestElement', - start: 17, - end: 24, - argument: { - type: 'Identifier', - start: 20, - end: 24, - name: 'rest' - } - } - ] - }, - init: { - type: 'ObjectExpression', - start: 29, - end: 66, - properties: [ - { - type: 'Property', - start: 31, - end: 36, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 31, - end: 32, - name: 'x' - }, - value: { - type: 'Literal', - start: 34, - end: 36, - value: 10, - raw: '10' - }, - kind: 'init' - }, - { - type: 'Property', - start: 38, - end: 43, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 38, - end: 39, - name: 'y' - }, - value: { - type: 'Literal', - start: 41, - end: 43, - value: 20, - raw: '20' - }, - kind: 'init' - }, - { - type: 'Property', - start: 45, - end: 50, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 45, - end: 46, - name: 'z' - }, - value: { - type: 'Literal', - start: 48, - end: 50, - value: 30, - raw: '30' - }, - kind: 'init' - }, - { - type: 'Property', - start: 52, - end: 57, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 52, - end: 53, - name: 'w' - }, - value: { - type: 'Literal', - start: 55, - end: 57, - value: 40, - raw: '40' - }, - kind: 'init' - }, - { - type: 'Property', - start: 59, - end: 64, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 59, - end: 60, - name: 'k' - }, - value: { - type: 'Literal', - start: 62, - end: 64, - value: 50, - raw: '50' - }, - kind: 'init' - } - ] - } - } - ], - kind: 'const' - } - ], + const ast = parse('const {x, y: z, ...rest} = {x: 10, y: 20, z: 30, w: 40, k: 50};', { + ecmaVersion: 2020, sourceType: 'module' - }; + }); const scope = attachScopes(ast, 'scope'); t.truthy(scope.contains('x')); @@ -423,128 +44,10 @@ test('adds rest elements from a deconstructed object declaration', (t) => { }); test('adds nested declarators from a deconstructed declaration', (t) => { - const ast = { - type: 'Program', - start: 0, - end: 40, - body: [ - { - type: 'VariableDeclaration', - start: 0, - end: 40, - declarations: [ - { - type: 'VariableDeclarator', - start: 4, - end: 39, - id: { - type: 'ObjectPattern', - start: 4, - end: 19, - properties: [ - { - type: 'Property', - start: 6, - end: 17, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 6, - end: 7, - name: 'a' - }, - value: { - type: 'ObjectPattern', - start: 9, - end: 17, - properties: [ - { - type: 'Property', - start: 11, - end: 15, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 11, - end: 12, - name: 'b' - }, - value: { - type: 'Identifier', - start: 14, - end: 15, - name: 'c' - }, - kind: 'init' - } - ] - }, - kind: 'init' - } - ] - }, - init: { - type: 'ObjectExpression', - start: 22, - end: 39, - properties: [ - { - type: 'Property', - start: 24, - end: 37, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 24, - end: 25, - name: 'a' - }, - value: { - type: 'ObjectExpression', - start: 27, - end: 37, - properties: [ - { - type: 'Property', - start: 29, - end: 35, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', - start: 29, - end: 30, - name: 'b' - }, - value: { - type: 'Literal', - start: 32, - end: 35, - value: 'b', - raw: "'b'" - }, - kind: 'init' - } - ] - }, - kind: 'init' - } - ] - } - } - ], - kind: 'let' - } - ], + const ast = parse("let {a: {b: c}} = {a: {b: 'b'}};", { + ecmaVersion: 2020, sourceType: 'module' - }; + }); const scope = attachScopes(ast, 'scope'); t.falsy(scope.contains('a')); @@ -553,35 +56,7 @@ test('adds nested declarators from a deconstructed declaration', (t) => { }); test('supports FunctionDeclarations without id', (t) => { - const ast = { - type: 'Program', - start: 0, - end: 33, - body: [ - { - type: 'ExportDefaultDeclaration', - start: 0, - end: 32, - declaration: { - type: 'FunctionDeclaration', - start: 15, - end: 32, - id: null, - generator: false, - expression: false, - async: false, - params: [], - body: { - type: 'BlockStatement', - start: 26, - end: 32, - body: [] - } - } - } - ], - sourceType: 'module' - }; + const ast = parse('export default function () {}', { ecmaVersion: 2020, sourceType: 'module' }); t.notThrows(() => { attachScopes(ast, 'scope'); @@ -589,38 +64,8 @@ test('supports FunctionDeclarations without id', (t) => { }); test('supports catch without a parameter', (t) => { - const ast = { - type: 'Program', - start: 0, - end: 23, - body: [ - { - type: 'TryStatement', - start: 0, - end: 23, - block: { - type: 'BlockStatement', - start: 4, - end: 10, - body: [] - }, - handler: { - type: 'CatchClause', - start: 11, - end: 23, - param: null, - body: { - type: 'BlockStatement', - start: 17, - end: 23, - body: [] - } - }, - finalizer: null - } - ], - sourceType: 'script' - }; + const ast = parse('try {} catch {}', { ecmaVersion: 2020, sourceType: 'script' }); + t.notThrows(() => { attachScopes(ast, 'scope'); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2a07aa0c0..0a6af048d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -331,6 +331,7 @@ importers: '@rollup/plugin-typescript': 5.0.2_rollup@2.23.0 '@types/node': 14.0.26 '@types/picomatch': 2.2.1 + acorn: 8.0.4 rollup: 2.23.0 specifiers: '@rollup/plugin-commonjs': ^14.0.0 @@ -339,6 +340,7 @@ importers: '@types/estree': 0.0.45 '@types/node': ^14.0.26 '@types/picomatch': ^2.2.1 + acorn: ^8.0.4 estree-walker: ^2.0.1 picomatch: ^2.2.2 rollup: ^2.23.0