Files
react-native-devtools-frontend/node_modules/rollup-pluginutils/src/attachScopes.ts
T
Wolfgang Beyer cdedf581e5 Revert "Update Mocha and rollup-plugin-terser"
This reverts commit ecc4ecb65c.

Reason for revert: This CL is suspected of breaking the roll into
chromium (https://crbug.com/1121242#c2).

Original change's description:
> Update Mocha and rollup-plugin-terser
> 
> These two packages had NPM advisories filed against them. Luckily,
> we can upgrade to versions where they are fixed.
> 
> The upgrade of rollup-plugin-terser includes the upgrade to Terser 5.
> Looking at its changelog (https://github.com/terser/terser/blob/master/CHANGELOG.md#v500-beta0)
> we seem to not be affected. Our build also works as expected.
> 
> DISABLE_THIRD_PARTY_CHECK=Update node modules
> R=​petermarshall@chromium.org
> 
> Change-Id: I6974a73d92a1a210b705246053be5c2af268605b
> Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2372148
> Auto-Submit: Tim van der Lippe <tvanderlippe@chromium.org>
> Commit-Queue: Peter Marshall <petermarshall@chromium.org>
> Reviewed-by: Peter Marshall <petermarshall@chromium.org>

TBR=petermarshall@chromium.org,tvanderlippe@chromium.org

Change-Id: I969ca281c77d299038332f93ffd477925e191073
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2374207
Reviewed-by: Wolfgang Beyer <wolfi@chromium.org>
Commit-Queue: Wolfgang Beyer <wolfi@chromium.org>
2020-08-25 08:29:20 +00:00

126 lines
3.0 KiB
TypeScript

import { Node, walk } from 'estree-walker';
import extractAssignedNames from './extractAssignedNames';
import { AttachedScope, AttachScopes } from './pluginutils';
const blockDeclarations = {
const: true,
let: true
};
interface ScopeOptions {
parent?: AttachedScope;
block?: boolean;
params?: Array<Node>;
}
class Scope implements AttachedScope {
parent?: AttachedScope;
isBlockScope: boolean;
declarations: { [key: string]: boolean };
constructor(options: ScopeOptions = {}) {
this.parent = options.parent;
this.isBlockScope = !!options.block;
this.declarations = Object.create(null);
if (options.params) {
options.params.forEach(param => {
extractAssignedNames(param).forEach(name => {
this.declarations[name] = true;
});
});
}
}
addDeclaration(node: Node, isBlockDeclaration: boolean, isVar: boolean): void {
if (!isBlockDeclaration && this.isBlockScope) {
// it's a `var` or function node, and this
// is a block scope, so we need to go up
this.parent!.addDeclaration(node, isBlockDeclaration, isVar);
} else if (node.id) {
extractAssignedNames(node.id).forEach(name => {
this.declarations[name] = true;
});
}
}
contains(name: string): boolean {
return this.declarations[name] || (this.parent ? this.parent.contains(name) : false);
}
}
const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'scope') {
let scope = new Scope();
walk(ast, {
enter(node, parent) {
// function foo () {...}
// class Foo {...}
if (/(Function|Class)Declaration/.test(node.type)) {
scope.addDeclaration(node, false, false);
}
// var foo = 1
if (node.type === 'VariableDeclaration') {
const kind: keyof typeof blockDeclarations = node.kind;
const isBlockDeclaration = blockDeclarations[kind];
node.declarations.forEach((declaration: Node) => {
scope.addDeclaration(declaration, isBlockDeclaration, true);
});
}
let newScope: AttachedScope | undefined;
// create new function scope
if (/Function/.test(node.type)) {
newScope = new Scope({
parent: scope,
block: false,
params: node.params
});
// named function expressions - the name is considered
// part of the function's scope
if (node.type === 'FunctionExpression' && node.id) {
newScope.addDeclaration(node, false, false);
}
}
// create new block scope
if (node.type === 'BlockStatement' && !/Function/.test(parent!.type)) {
newScope = new Scope({
parent: scope,
block: true
});
}
// catch clause has its own block scope
if (node.type === 'CatchClause') {
newScope = new Scope({
parent: scope,
params: node.param ? [node.param] : [],
block: true
});
}
if (newScope) {
Object.defineProperty(node, propertyName, {
value: newScope,
configurable: true
});
scope = newScope;
}
},
leave(node) {
if (node[propertyName]) scope = scope.parent!;
}
});
return scope;
};
export { attachScopes as default };