From 055fd52301af8f3509cfc67cc157ea490f746d60 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Thu, 24 Oct 2019 14:30:33 +0800 Subject: [PATCH 1/4] use periscopic --- package.json | 1 + src/compiler/compile/utils/scope.ts | 155 +--------------------------- 2 files changed, 5 insertions(+), 151 deletions(-) diff --git a/package.json b/package.json index a061cdf742..52deaaa99c 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "locate-character": "^2.0.5", "magic-string": "^0.25.3", "mocha": "^6.2.0", + "periscopic": "^1.0.2", "puppeteer": "^1.19.0", "rollup": "^1.21.4", "rollup-plugin-commonjs": "^10.1.0", diff --git a/src/compiler/compile/utils/scope.ts b/src/compiler/compile/utils/scope.ts index 239438ba9c..1f50560b66 100644 --- a/src/compiler/compile/utils/scope.ts +++ b/src/compiler/compile/utils/scope.ts @@ -1,156 +1,9 @@ -import { walk } from 'estree-walker'; -import is_reference from 'is-reference'; -import { Node, VariableDeclaration, ClassDeclaration, VariableDeclarator, ObjectPattern, Property, RestElement, ArrayPattern, Identifier } from 'estree'; -import get_object from './get_object'; +import { Node } from 'estree'; +import { analyze, Scope, extract_names, extract_identifiers } from 'periscopic'; // TODO replace this with periscopic? export function create_scopes(expression: Node) { - const map = new WeakMap(); - - const globals: Map = new Map(); - let scope = new Scope(null, false); - - walk(expression, { - enter(node, parent) { - if (node.type === 'ImportDeclaration') { - node.specifiers.forEach(specifier => { - scope.declarations.set(specifier.local.name, specifier); - }); - } else if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') { - if (node.type === 'FunctionDeclaration') { - scope.declarations.set(node.id.name, node); - scope = new Scope(scope, false); - map.set(node, scope); - } else { - scope = new Scope(scope, false); - map.set(node, scope); - if (node.type === 'FunctionExpression' && node.id) { - scope.declarations.set(node.id.name, node); - } - } - - node.params.forEach((param) => { - extract_names(param).forEach(name => { - scope.declarations.set(name, node); - }); - }); - } else if (/For(?:In|Of)?Statement/.test(node.type)) { - scope = new Scope(scope, true); - map.set(node, scope); - } else if (node.type === 'BlockStatement') { - scope = new Scope(scope, true); - map.set(node, scope); - } else if (node.type === 'ClassDeclaration' || node.type === 'VariableDeclaration') { - scope.add_declaration(node); - } else if (node.type === 'CatchClause') { - scope = new Scope(scope, true); - map.set(node, scope); - - extract_names(node.param).forEach(name => { - scope.declarations.set(name, node.param); - }); - } else if (node.type === 'Identifier' && is_reference(node as Node, parent as Node)) { - if (!scope.has(node.name) && !globals.has(node.name)) { - globals.set(node.name, node); - } - } - }, - - leave(node: Node) { - if (map.has(node)) { - scope = scope.parent; - } - } - }); - - scope.declarations.forEach((_node, name) => { - globals.delete(name); - }); - - return { map, scope, globals }; + return analyze(expression); } -export class Scope { - parent: Scope; - block: boolean; - - declarations: Map = new Map(); - initialised_declarations: Set = new Set(); - - constructor(parent: Scope, block: boolean) { - this.parent = parent; - this.block = block; - } - - add_declaration(node: VariableDeclaration | ClassDeclaration) { - if (node.type === 'VariableDeclaration') { - if (node.kind === 'var' && this.block && this.parent) { - this.parent.add_declaration(node); - } else { - node.declarations.forEach((declarator: VariableDeclarator) => { - extract_names(declarator.id).forEach(name => { - this.declarations.set(name, node); - if (declarator.init) this.initialised_declarations.add(name); - }); - }); - } - } else { - this.declarations.set(node.id.name, node); - } - } - - find_owner(name: string): Scope { - if (this.declarations.has(name)) return this; - return this.parent && this.parent.find_owner(name); - } - - has(name: string): boolean { - return ( - this.declarations.has(name) || (this.parent && this.parent.has(name)) - ); - } -} - -export function extract_names(param: Node): string[] { - return extract_identifiers(param).map((node: any) => node.name); -} - -export function extract_identifiers(param: Node): Identifier[] { - const nodes: Identifier[] = []; - extractors[param.type] && extractors[param.type](nodes, param); - return nodes; -} - -const extractors = { - Identifier(nodes: Node[], param: Node) { - nodes.push(param); - }, - - MemberExpression(nodes: Node[], param: Node) { - nodes.push(get_object(param)); - }, - - ObjectPattern(nodes: Node[], param: ObjectPattern) { - param.properties.forEach((prop: Property | RestElement) => { - if (prop.type === 'RestElement') { - nodes.push(prop.argument); - } else { - extractors[prop.value.type](nodes, prop.value); - } - }); - }, - - ArrayPattern(nodes: Node[], param: ArrayPattern) { - param.elements.forEach((element: Node) => { - if (element) extractors[element.type](nodes, element); - }); - }, - - RestElement(nodes: Node[], param: any) { - extractors[param.argument.type](nodes, param.argument); - }, - - AssignmentPattern(nodes: Node[], param: any) { - extractors[param.left.type](nodes, param.left); - } -}; +export { Scope, extract_names, extract_identifiers } From cdfddba2654f48343d357a99cf0aa0cbe2813df7 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Fri, 25 Oct 2019 17:38:13 -0400 Subject: [PATCH 2/4] upgrade periscopic --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index cc5bd88a6d..620aa4ef13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2713,9 +2713,9 @@ "dev": true }, "periscopic": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/periscopic/-/periscopic-1.0.2.tgz", - "integrity": "sha512-KpKBKadLf8THXOxswQBhOY8E1lVVhfUidacPtQBrq7KDXaNkQLUPiTmXagzqpJGECP3/0gDXYFO6CZHVbGvOSw==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/periscopic/-/periscopic-1.1.0.tgz", + "integrity": "sha512-sUdDgd8G35JjpBqHGnuc2MECoyUryHGfjtsKFPS6N8MEGHtxoIML8yEWydL1zf+W8EoChX4L7A9AvVRJuM6Lqg==", "dev": true, "requires": { "is-reference": "^1.1.4" diff --git a/package.json b/package.json index 52deaaa99c..0915078aac 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "locate-character": "^2.0.5", "magic-string": "^0.25.3", "mocha": "^6.2.0", - "periscopic": "^1.0.2", + "periscopic": "^1.1.0", "puppeteer": "^1.19.0", "rollup": "^1.21.4", "rollup-plugin-commonjs": "^10.1.0", From bad7443eb3c9e9e6f6b3c53ba255bab95c10f087 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Wed, 30 Oct 2019 22:31:46 +0800 Subject: [PATCH 3/4] fix lint --- src/compiler/compile/utils/scope.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/compile/utils/scope.ts b/src/compiler/compile/utils/scope.ts index 1f50560b66..e103defa4d 100644 --- a/src/compiler/compile/utils/scope.ts +++ b/src/compiler/compile/utils/scope.ts @@ -1,9 +1,8 @@ import { Node } from 'estree'; import { analyze, Scope, extract_names, extract_identifiers } from 'periscopic'; -// TODO replace this with periscopic? export function create_scopes(expression: Node) { return analyze(expression); } -export { Scope, extract_names, extract_identifiers } +export { Scope, extract_names, extract_identifiers }; From cc81f103e9e367009e50c2b4813e0ea071549dc8 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Wed, 6 Nov 2019 09:01:25 -0500 Subject: [PATCH 4/4] update periscopic --- package-lock.json | 17 ++++++++++++++--- package.json | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 620aa4ef13..8f3c39c654 100644 --- a/package-lock.json +++ b/package-lock.json @@ -499,6 +499,17 @@ "is-reference": "^1.1.4", "periscopic": "^1.0.2", "sourcemap-codec": "^1.4.6" + }, + "dependencies": { + "periscopic": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/periscopic/-/periscopic-1.1.0.tgz", + "integrity": "sha512-sUdDgd8G35JjpBqHGnuc2MECoyUryHGfjtsKFPS6N8MEGHtxoIML8yEWydL1zf+W8EoChX4L7A9AvVRJuM6Lqg==", + "dev": true, + "requires": { + "is-reference": "^1.1.4" + } + } } }, "codecov": { @@ -2713,9 +2724,9 @@ "dev": true }, "periscopic": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/periscopic/-/periscopic-1.1.0.tgz", - "integrity": "sha512-sUdDgd8G35JjpBqHGnuc2MECoyUryHGfjtsKFPS6N8MEGHtxoIML8yEWydL1zf+W8EoChX4L7A9AvVRJuM6Lqg==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/periscopic/-/periscopic-2.0.0.tgz", + "integrity": "sha512-2YVtztswd6ud5b0+IDD26UhEm1QvlTsR3s7G59CD0txGyhfvQ39YhNuueSmzT5VzHUxiIH0E8S04JSQpXMJ8/g==", "dev": true, "requires": { "is-reference": "^1.1.4" diff --git a/package.json b/package.json index 0915078aac..c60ceb2597 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "locate-character": "^2.0.5", "magic-string": "^0.25.3", "mocha": "^6.2.0", - "periscopic": "^1.1.0", + "periscopic": "^2.0.0", "puppeteer": "^1.19.0", "rollup": "^1.21.4", "rollup-plugin-commonjs": "^10.1.0",