identify function parameters as binding parameters
This commit is contained in:
parent
0bf747c214
commit
1d31db29a5
@ -12,7 +12,7 @@ export function getBindingIdentifiers(node: Object, duplicates?): Object {
|
||||
var id = search.shift();
|
||||
if (!id) continue;
|
||||
|
||||
var key = t.getBindingIdentifiers.keys[id.type];
|
||||
var keys = t.getBindingIdentifiers.keys[id.type];
|
||||
|
||||
if (t.isIdentifier(id)) {
|
||||
if (duplicates) {
|
||||
@ -25,8 +25,13 @@ export function getBindingIdentifiers(node: Object, duplicates?): Object {
|
||||
if (t.isDeclaration(node.declaration)) {
|
||||
search.push(node.declaration);
|
||||
}
|
||||
} else if (key && id[key]) {
|
||||
search = search.concat(id[key]);
|
||||
} else if (keys) {
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
if (id[key]) {
|
||||
search = search.concat(id[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,42 +43,42 @@ export function getBindingIdentifiers(node: Object, duplicates?): Object {
|
||||
*/
|
||||
|
||||
getBindingIdentifiers.keys = {
|
||||
DeclareClass: "id",
|
||||
DeclareFunction: "id",
|
||||
DeclareModule: "id",
|
||||
DeclareVariable: "id",
|
||||
InterfaceDeclaration: "id",
|
||||
TypeAlias: "id",
|
||||
DeclareClass: ["id"],
|
||||
DeclareFunction: ["id"],
|
||||
DeclareModule: ["id"],
|
||||
DeclareVariable: ["id"],
|
||||
InterfaceDeclaration: ["id"],
|
||||
TypeAlias: ["id"],
|
||||
|
||||
ComprehensionExpression: "blocks",
|
||||
ComprehensionBlock: "left",
|
||||
ComprehensionExpression: ["blocks"],
|
||||
ComprehensionBlock: ["left"],
|
||||
|
||||
CatchClause: "param",
|
||||
LabeledStatement: "label",
|
||||
UnaryExpression: "argument",
|
||||
AssignmentExpression: "left",
|
||||
CatchClause: ["param"],
|
||||
LabeledStatement: ["label"],
|
||||
UnaryExpression: ["argument"],
|
||||
AssignmentExpression: ["left"],
|
||||
|
||||
ImportSpecifier: "local",
|
||||
ImportNamespaceSpecifier: "local",
|
||||
ImportDefaultSpecifier: "local",
|
||||
ImportDeclaration: "specifiers",
|
||||
ImportSpecifier: ["local"],
|
||||
ImportNamespaceSpecifier: ["local"],
|
||||
ImportDefaultSpecifier: ["local"],
|
||||
ImportDeclaration: ["specifiers"],
|
||||
|
||||
FunctionDeclaration: "id",
|
||||
FunctionExpression: "id",
|
||||
FunctionDeclaration: ["id", "params"],
|
||||
FunctionExpression: ["id", "params"],
|
||||
|
||||
ClassDeclaration: "id",
|
||||
ClassExpression: "id",
|
||||
ClassDeclaration: ["id"],
|
||||
ClassExpression: ["id"],
|
||||
|
||||
RestElement: "argument",
|
||||
UpdateExpression: "argument",
|
||||
RestElement: ["argument"],
|
||||
UpdateExpression: ["argument"],
|
||||
|
||||
SpreadProperty: "argument",
|
||||
Property: "value",
|
||||
SpreadProperty: ["argument"],
|
||||
Property: ["value"],
|
||||
|
||||
AssignmentPattern: "left",
|
||||
ArrayPattern: "elements",
|
||||
ObjectPattern: "properties",
|
||||
AssignmentPattern: ["left"],
|
||||
ArrayPattern: ["elements"],
|
||||
ObjectPattern: ["properties"],
|
||||
|
||||
VariableDeclaration: "declarations",
|
||||
VariableDeclarator: "id"
|
||||
VariableDeclaration: ["declarations"],
|
||||
VariableDeclarator: ["id"]
|
||||
};
|
||||
|
||||
@ -7,12 +7,20 @@ import * as t from "./index";
|
||||
*/
|
||||
|
||||
export function isBinding(node: Object, parent: Object): boolean {
|
||||
var bindingKey = getBindingIdentifiers.keys[parent.type];
|
||||
if (bindingKey) {
|
||||
return parent[bindingKey] === node;
|
||||
} else {
|
||||
return false;
|
||||
var keys = getBindingIdentifiers.keys[parent.type];
|
||||
if (keys) {
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
var val = parent[key];
|
||||
if (Array.isArray(val)) {
|
||||
if (val.indexOf(node) >= 0) return true;
|
||||
} else {
|
||||
if (val === node) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,15 +206,7 @@ export function isScope(node: Object, parent: Object): boolean {
|
||||
export function isImmutable(node: Object): boolean {
|
||||
if (t.isType(node.type, "Immutable")) return true;
|
||||
|
||||
if (t.isLiteral(node)) {
|
||||
if (node.regex) {
|
||||
// regexs are mutable
|
||||
return false;
|
||||
} else {
|
||||
// immutable!
|
||||
return true;
|
||||
}
|
||||
} else if (t.isIdentifier(node)) {
|
||||
if (t.isIdentifier(node)) {
|
||||
if (node.name === "undefined") {
|
||||
// immutable!
|
||||
return true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user