enable prefer const (#5113)
This commit is contained in:
@@ -24,10 +24,10 @@ export function toComputedKey(node: Object, key: Object = node.key || node.prope
|
||||
export function toSequenceExpression(nodes: Array<Object>, scope: Scope): ?Object {
|
||||
if (!nodes || !nodes.length) return;
|
||||
|
||||
let declars = [];
|
||||
const declars = [];
|
||||
let bailed = false;
|
||||
|
||||
let result = convert(nodes);
|
||||
const result = convert(nodes);
|
||||
if (bailed) return;
|
||||
|
||||
for (let i = 0; i < declars.length; i++) {
|
||||
@@ -38,9 +38,9 @@ export function toSequenceExpression(nodes: Array<Object>, scope: Scope): ?Objec
|
||||
|
||||
function convert(nodes) {
|
||||
let ensureLastUndefined = false;
|
||||
let exprs = [];
|
||||
const exprs = [];
|
||||
|
||||
for (let node of (nodes: Array)) {
|
||||
for (const node of (nodes: Array)) {
|
||||
if (t.isExpression(node)) {
|
||||
exprs.push(node);
|
||||
} else if (t.isExpressionStatement(node)) {
|
||||
@@ -48,9 +48,9 @@ export function toSequenceExpression(nodes: Array<Object>, scope: Scope): ?Objec
|
||||
} else if (t.isVariableDeclaration(node)) {
|
||||
if (node.kind !== "var") return bailed = true; // bailed
|
||||
|
||||
for (let declar of (node.declarations: Array)) {
|
||||
let bindings = t.getBindingIdentifiers(declar);
|
||||
for (let key in bindings) {
|
||||
for (const declar of (node.declarations: Array)) {
|
||||
const bindings = t.getBindingIdentifiers(declar);
|
||||
for (const key in bindings) {
|
||||
declars.push({
|
||||
kind: node.kind,
|
||||
id: bindings[key]
|
||||
@@ -65,8 +65,8 @@ export function toSequenceExpression(nodes: Array<Object>, scope: Scope): ?Objec
|
||||
ensureLastUndefined = true;
|
||||
continue;
|
||||
} else if (t.isIfStatement(node)) {
|
||||
let consequent = node.consequent ? convert([node.consequent]) : scope.buildUndefinedNode();
|
||||
let alternate = node.alternate ? convert([node.alternate]) : scope.buildUndefinedNode();
|
||||
const consequent = node.consequent ? convert([node.consequent]) : scope.buildUndefinedNode();
|
||||
const alternate = node.alternate ? convert([node.alternate]) : scope.buildUndefinedNode();
|
||||
if (!consequent || !alternate) return bailed = true;
|
||||
|
||||
exprs.push(t.conditionalExpression(node.test, consequent, alternate));
|
||||
@@ -283,8 +283,8 @@ export function valueToNode(value: any): Object {
|
||||
|
||||
// regexes
|
||||
if (isRegExp(value)) {
|
||||
let pattern = value.source;
|
||||
let flags = value.toString().match(/\/([a-z]+|)$/)[1];
|
||||
const pattern = value.source;
|
||||
const flags = value.toString().match(/\/([a-z]+|)$/)[1];
|
||||
return t.regExpLiteral(pattern, flags);
|
||||
}
|
||||
|
||||
@@ -295,8 +295,8 @@ export function valueToNode(value: any): Object {
|
||||
|
||||
// object
|
||||
if (isPlainObject(value)) {
|
||||
let props = [];
|
||||
for (let key in value) {
|
||||
const props = [];
|
||||
for (const key in value) {
|
||||
let nodeKey;
|
||||
if (t.isValidIdentifier(key)) {
|
||||
nodeKey = t.identifier(key);
|
||||
|
||||
@@ -425,7 +425,7 @@ defineType("MemberExpression", {
|
||||
},
|
||||
property: {
|
||||
validate(node, key, val) {
|
||||
let expectedType = node.computed ? "Expression" : "Identifier";
|
||||
const expectedType = node.computed ? "Expression" : "Identifier";
|
||||
assertNodeType(expectedType)(node, key, val);
|
||||
}
|
||||
},
|
||||
@@ -489,7 +489,7 @@ defineType("ObjectMethod", {
|
||||
},
|
||||
key: {
|
||||
validate(node, key, val) {
|
||||
let expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"];
|
||||
const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"];
|
||||
assertNodeType(...expectedTypes)(node, key, val);
|
||||
}
|
||||
},
|
||||
@@ -521,7 +521,7 @@ defineType("ObjectProperty", {
|
||||
},
|
||||
key: {
|
||||
validate(node, key, val) {
|
||||
let expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"];
|
||||
const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"];
|
||||
assertNodeType(...expectedTypes)(node, key, val);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -264,7 +264,7 @@ defineType("ClassMethod", {
|
||||
},
|
||||
key: {
|
||||
validate(node, key, val) {
|
||||
let expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"];
|
||||
const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"];
|
||||
assertNodeType(...expectedTypes)(node, key, val);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import * as t from "../index";
|
||||
|
||||
export let VISITOR_KEYS = {};
|
||||
export let ALIAS_KEYS = {};
|
||||
export let NODE_FIELDS = {};
|
||||
export let BUILDER_KEYS = {};
|
||||
export let DEPRECATED_KEYS = {};
|
||||
export const VISITOR_KEYS = {};
|
||||
export const ALIAS_KEYS = {};
|
||||
export const NODE_FIELDS = {};
|
||||
export const BUILDER_KEYS = {};
|
||||
export const DEPRECATED_KEYS = {};
|
||||
|
||||
function getType(val) {
|
||||
if (Array.isArray(val)) {
|
||||
@@ -48,7 +48,7 @@ export function assertNodeType(...types: Array<string>): Function {
|
||||
function validate(node, key, val) {
|
||||
let valid = false;
|
||||
|
||||
for (let type of types) {
|
||||
for (const type of types) {
|
||||
if (t.is(type, val)) {
|
||||
valid = true;
|
||||
break;
|
||||
@@ -72,7 +72,7 @@ export function assertNodeOrValueType(...types: Array<string>): Function {
|
||||
function validate(node, key, val) {
|
||||
let valid = false;
|
||||
|
||||
for (let type of types) {
|
||||
for (const type of types) {
|
||||
if (getType(val) === type || t.is(type, val)) {
|
||||
valid = true;
|
||||
break;
|
||||
@@ -94,7 +94,7 @@ export function assertNodeOrValueType(...types: Array<string>): Function {
|
||||
|
||||
export function assertValueType(type: string): Function {
|
||||
function validate(node, key, val) {
|
||||
let valid = getType(val) === type;
|
||||
const valid = getType(val) === type;
|
||||
|
||||
if (!valid) {
|
||||
throw new TypeError(`Property ${key} expected type of ${type} but got ${getType(val)}`);
|
||||
@@ -108,7 +108,7 @@ export function assertValueType(type: string): Function {
|
||||
|
||||
export function chain(...fns: Array<Function>): Function {
|
||||
function validate(...args) {
|
||||
for (let fn of fns) {
|
||||
for (const fn of fns) {
|
||||
fn(...args);
|
||||
}
|
||||
}
|
||||
@@ -127,7 +127,7 @@ export default function defineType(
|
||||
deprecatedAlias?: string;
|
||||
} = {},
|
||||
) {
|
||||
let inherits = (opts.inherits && store[opts.inherits]) || {};
|
||||
const inherits = (opts.inherits && store[opts.inherits]) || {};
|
||||
|
||||
opts.fields = opts.fields || inherits.fields || {};
|
||||
opts.visitor = opts.visitor || inherits.visitor || [];
|
||||
@@ -139,12 +139,12 @@ export default function defineType(
|
||||
}
|
||||
|
||||
// ensure all field keys are represented in `fields`
|
||||
for (let key of (opts.visitor.concat(opts.builder): Array<string>)) {
|
||||
for (const key of (opts.visitor.concat(opts.builder): Array<string>)) {
|
||||
opts.fields[key] = opts.fields[key] || {};
|
||||
}
|
||||
|
||||
for (let key in opts.fields) {
|
||||
let field = opts.fields[key];
|
||||
for (const key in opts.fields) {
|
||||
const field = opts.fields[key];
|
||||
|
||||
if (opts.builder.indexOf(key) === -1) {
|
||||
field.optional = true;
|
||||
@@ -164,4 +164,4 @@ export default function defineType(
|
||||
store[type] = opts;
|
||||
}
|
||||
|
||||
let store = {};
|
||||
const store = {};
|
||||
|
||||
@@ -6,7 +6,7 @@ import * as t from "./index";
|
||||
*/
|
||||
|
||||
export function createUnionTypeAnnotation(types: Array<Object>) {
|
||||
let flattened = removeTypeDuplicates(types);
|
||||
const flattened = removeTypeDuplicates(types);
|
||||
|
||||
if (flattened.length === 1) {
|
||||
return flattened[0];
|
||||
@@ -20,16 +20,16 @@ export function createUnionTypeAnnotation(types: Array<Object>) {
|
||||
*/
|
||||
|
||||
export function removeTypeDuplicates(nodes: Array<Object>): Array<Object> {
|
||||
let generics = {};
|
||||
let bases = {};
|
||||
const generics = {};
|
||||
const bases = {};
|
||||
|
||||
// store union type groups to circular references
|
||||
let typeGroups = [];
|
||||
const typeGroups = [];
|
||||
|
||||
let types = [];
|
||||
const types = [];
|
||||
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
let node = nodes[i];
|
||||
const node = nodes[i];
|
||||
if (!node) continue;
|
||||
|
||||
// detect duplicates
|
||||
@@ -59,7 +59,7 @@ export function removeTypeDuplicates(nodes: Array<Object>): Array<Object> {
|
||||
|
||||
// find a matching generic type and merge and deduplicate the type parameters
|
||||
if (t.isGenericTypeAnnotation(node)) {
|
||||
let name = node.id.name;
|
||||
const name = node.id.name;
|
||||
|
||||
if (generics[name]) {
|
||||
let existing = generics[name];
|
||||
@@ -83,12 +83,12 @@ export function removeTypeDuplicates(nodes: Array<Object>): Array<Object> {
|
||||
}
|
||||
|
||||
// add back in bases
|
||||
for (let type in bases) {
|
||||
for (const type in bases) {
|
||||
types.push(bases[type]);
|
||||
}
|
||||
|
||||
// add back in generics
|
||||
for (let name in generics) {
|
||||
for (const name in generics) {
|
||||
types.push(generics[name]);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import loClone from "lodash/clone";
|
||||
import each from "lodash/each";
|
||||
import uniq from "lodash/uniq";
|
||||
|
||||
let t = exports;
|
||||
const t = exports;
|
||||
|
||||
/**
|
||||
* Registers `is[Type]` and `assert[Type]` generated functions for a given `type`.
|
||||
@@ -62,7 +62,7 @@ export { _react as react };
|
||||
* Registers `is[Type]` and `assert[Type]` for all types.
|
||||
*/
|
||||
|
||||
for (let type in t.VISITOR_KEYS) {
|
||||
for (const type in t.VISITOR_KEYS) {
|
||||
registerType(type);
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ t.FLIPPED_ALIAS_KEYS = {};
|
||||
|
||||
each(t.ALIAS_KEYS, function (aliases, type) {
|
||||
each(aliases, function (alias) {
|
||||
let types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || [];
|
||||
const types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || [];
|
||||
types.push(type);
|
||||
});
|
||||
});
|
||||
@@ -102,7 +102,7 @@ export const TYPES = Object.keys(t.VISITOR_KEYS)
|
||||
export function is(type: string, node: Object, opts?: Object): boolean {
|
||||
if (!node) return false;
|
||||
|
||||
let matches = isType(node.type, type);
|
||||
const matches = isType(node.type, type);
|
||||
if (!matches) return false;
|
||||
|
||||
if (typeof opts === "undefined") {
|
||||
@@ -123,11 +123,11 @@ export function isType(nodeType: string, targetType: string): boolean {
|
||||
// targetType was a primary node type, so there's no need to check the aliases.
|
||||
if (t.ALIAS_KEYS[targetType]) return false;
|
||||
|
||||
let aliases: ?Array<string> = t.FLIPPED_ALIAS_KEYS[targetType];
|
||||
const aliases: ?Array<string> = t.FLIPPED_ALIAS_KEYS[targetType];
|
||||
if (aliases) {
|
||||
if (aliases[0] === nodeType) return true;
|
||||
|
||||
for (let alias of aliases) {
|
||||
for (const alias of aliases) {
|
||||
if (nodeType === alias) return true;
|
||||
}
|
||||
}
|
||||
@@ -148,13 +148,13 @@ each(t.BUILDER_KEYS, function (keys, type) {
|
||||
);
|
||||
}
|
||||
|
||||
let node = {};
|
||||
const node = {};
|
||||
node.type = type;
|
||||
|
||||
let i = 0;
|
||||
|
||||
for (let key of (keys: Array<string>)) {
|
||||
let field = t.NODE_FIELDS[type][key];
|
||||
for (const key of (keys: Array<string>)) {
|
||||
const field = t.NODE_FIELDS[type][key];
|
||||
|
||||
let arg = arguments[i++];
|
||||
if (arg === undefined) arg = loClone(field.default);
|
||||
@@ -162,7 +162,7 @@ each(t.BUILDER_KEYS, function (keys, type) {
|
||||
node[key] = arg;
|
||||
}
|
||||
|
||||
for (let key in node) {
|
||||
for (const key in node) {
|
||||
validate(node, key, node[key]);
|
||||
}
|
||||
|
||||
@@ -177,8 +177,8 @@ each(t.BUILDER_KEYS, function (keys, type) {
|
||||
* Description
|
||||
*/
|
||||
|
||||
for (let type in t.DEPRECATED_KEYS) {
|
||||
let newType = t.DEPRECATED_KEYS[type];
|
||||
for (const type in t.DEPRECATED_KEYS) {
|
||||
const newType = t.DEPRECATED_KEYS[type];
|
||||
|
||||
function proxy(fn) {
|
||||
return function () {
|
||||
@@ -199,10 +199,10 @@ for (let type in t.DEPRECATED_KEYS) {
|
||||
export function validate(node?: Object, key: string, val: any) {
|
||||
if (!node) return;
|
||||
|
||||
let fields = t.NODE_FIELDS[node.type];
|
||||
const fields = t.NODE_FIELDS[node.type];
|
||||
if (!fields) return;
|
||||
|
||||
let field = fields[key];
|
||||
const field = fields[key];
|
||||
if (!field || !field.validate) return;
|
||||
if (field.optional && val == null) return;
|
||||
|
||||
@@ -214,9 +214,9 @@ export function validate(node?: Object, key: string, val: any) {
|
||||
*/
|
||||
|
||||
export function shallowEqual(actual: Object, expected: Object): boolean {
|
||||
let keys = Object.keys(expected);
|
||||
const keys = Object.keys(expected);
|
||||
|
||||
for (let key of (keys: Array<string>)) {
|
||||
for (const key of (keys: Array<string>)) {
|
||||
if (actual[key] !== expected[key]) {
|
||||
return false;
|
||||
}
|
||||
@@ -260,8 +260,8 @@ export function ensureBlock(node: Object, key: string = "body"): Object {
|
||||
|
||||
export function clone(node: Object): Object {
|
||||
if (!node) return node;
|
||||
let newNode = {};
|
||||
for (let key in node) {
|
||||
const newNode = {};
|
||||
for (const key in node) {
|
||||
if (key[0] === "_") continue;
|
||||
newNode[key] = node[key];
|
||||
}
|
||||
@@ -273,7 +273,7 @@ export function clone(node: Object): Object {
|
||||
*/
|
||||
|
||||
export function cloneWithoutLoc(node: Object): Object {
|
||||
let newNode = clone(node);
|
||||
const newNode = clone(node);
|
||||
delete newNode.loc;
|
||||
return newNode;
|
||||
}
|
||||
@@ -285,9 +285,9 @@ export function cloneWithoutLoc(node: Object): Object {
|
||||
|
||||
export function cloneDeep(node: Object): Object {
|
||||
if (!node) return node;
|
||||
let newNode = {};
|
||||
const newNode = {};
|
||||
|
||||
for (let key in node) {
|
||||
for (const key in node) {
|
||||
if (key[0] === "_") continue;
|
||||
|
||||
let val = node[key];
|
||||
@@ -315,17 +315,17 @@ export function cloneDeep(node: Object): Object {
|
||||
*/
|
||||
|
||||
export function buildMatchMemberExpression(match:string, allowPartial?: boolean): Function {
|
||||
let parts = match.split(".");
|
||||
const parts = match.split(".");
|
||||
|
||||
return function (member) {
|
||||
// not a member expression
|
||||
if (!t.isMemberExpression(member)) return false;
|
||||
|
||||
let search = [member];
|
||||
const search = [member];
|
||||
let i = 0;
|
||||
|
||||
while (search.length) {
|
||||
let node = search.shift();
|
||||
const node = search.shift();
|
||||
|
||||
if (allowPartial && i === parts.length) {
|
||||
return true;
|
||||
@@ -366,7 +366,7 @@ export function buildMatchMemberExpression(match:string, allowPartial?: boolean)
|
||||
*/
|
||||
|
||||
export function removeComments(node: Object): Object {
|
||||
for (let key of t.COMMENT_KEYS) {
|
||||
for (const key of t.COMMENT_KEYS) {
|
||||
delete node[key];
|
||||
}
|
||||
return node;
|
||||
@@ -409,19 +409,19 @@ export function inherits(child: Object, parent: Object): Object {
|
||||
if (!child || !parent) return child;
|
||||
|
||||
// optionally inherit specific properties if not null
|
||||
for (let key of (t.INHERIT_KEYS.optional: Array<string>)) {
|
||||
for (const key of (t.INHERIT_KEYS.optional: Array<string>)) {
|
||||
if (child[key] == null) {
|
||||
child[key] = parent[key];
|
||||
}
|
||||
}
|
||||
|
||||
// force inherit "private" properties
|
||||
for (let key in parent) {
|
||||
for (const key in parent) {
|
||||
if (key[0] === "_") child[key] = parent[key];
|
||||
}
|
||||
|
||||
// force inherit select properties
|
||||
for (let key of (t.INHERIT_KEYS.force: Array<string>)) {
|
||||
for (const key of (t.INHERIT_KEYS.force: Array<string>)) {
|
||||
child[key] = parent[key];
|
||||
}
|
||||
|
||||
@@ -460,17 +460,17 @@ toFastProperties(t.VISITOR_KEYS);
|
||||
export function traverseFast(node: Node, enter: (node: Node) => void, opts?: Object) {
|
||||
if (!node) return;
|
||||
|
||||
let keys = t.VISITOR_KEYS[node.type];
|
||||
const keys = t.VISITOR_KEYS[node.type];
|
||||
if (!keys) return;
|
||||
|
||||
opts = opts || {};
|
||||
enter(node, opts);
|
||||
|
||||
for (let key of keys) {
|
||||
let subNode = node[key];
|
||||
for (const key of keys) {
|
||||
const subNode = node[key];
|
||||
|
||||
if (Array.isArray(subNode)) {
|
||||
for (let node of subNode) {
|
||||
for (const node of subNode) {
|
||||
traverseFast(node, enter, opts);
|
||||
}
|
||||
} else {
|
||||
@@ -496,17 +496,17 @@ const CLEAR_KEYS_PLUS_COMMENTS: Array = t.COMMENT_KEYS.concat([
|
||||
|
||||
export function removeProperties(node: Node, opts?: Object): void {
|
||||
opts = opts || {};
|
||||
let map = opts.preserveComments ? CLEAR_KEYS : CLEAR_KEYS_PLUS_COMMENTS;
|
||||
for (let key of map) {
|
||||
const map = opts.preserveComments ? CLEAR_KEYS : CLEAR_KEYS_PLUS_COMMENTS;
|
||||
for (const key of map) {
|
||||
if (node[key] != null) node[key] = undefined;
|
||||
}
|
||||
|
||||
for (let key in node) {
|
||||
for (const key in node) {
|
||||
if (key[0] === "_" && node[key] != null) node[key] = undefined;
|
||||
}
|
||||
|
||||
let syms: Array<Symbol> = Object.getOwnPropertySymbols(node);
|
||||
for (let sym of syms) {
|
||||
const syms: Array<Symbol> = Object.getOwnPropertySymbols(node);
|
||||
for (const sym of syms) {
|
||||
node[sym] = null;
|
||||
}
|
||||
}
|
||||
|
||||
14
packages/babel-types/src/react.js
vendored
14
packages/babel-types/src/react.js
vendored
@@ -1,6 +1,6 @@
|
||||
import * as t from "./index";
|
||||
|
||||
export let isReactComponent = t.buildMatchMemberExpression("React.Component");
|
||||
export const isReactComponent = t.buildMatchMemberExpression("React.Component");
|
||||
|
||||
export function isCompatTag(tagName?: string): boolean {
|
||||
return !!tagName && /^[a-z]|\-/.test(tagName);
|
||||
@@ -10,7 +10,7 @@ function cleanJSXElementLiteralChild(
|
||||
child: { value: string },
|
||||
args: Array<Object>,
|
||||
) {
|
||||
let lines = child.value.split(/\r\n|\n|\r/);
|
||||
const lines = child.value.split(/\r\n|\n|\r/);
|
||||
|
||||
let lastNonEmptyLine = 0;
|
||||
|
||||
@@ -23,11 +23,11 @@ function cleanJSXElementLiteralChild(
|
||||
let str = "";
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
let line = lines[i];
|
||||
const line = lines[i];
|
||||
|
||||
let isFirstLine = i === 0;
|
||||
let isLastLine = i === lines.length - 1;
|
||||
let isLastNonEmptyLine = i === lastNonEmptyLine;
|
||||
const isFirstLine = i === 0;
|
||||
const isLastLine = i === lines.length - 1;
|
||||
const isLastNonEmptyLine = i === lastNonEmptyLine;
|
||||
|
||||
// replace rendered whitespace tabs with spaces
|
||||
let trimmedLine = line.replace(/\t/g, " ");
|
||||
@@ -55,7 +55,7 @@ function cleanJSXElementLiteralChild(
|
||||
}
|
||||
|
||||
export function buildChildren(node: Object): Array<Object> {
|
||||
let elems = [];
|
||||
const elems = [];
|
||||
|
||||
for (let i = 0; i < node.children.length; i++) {
|
||||
let child = node.children[i];
|
||||
|
||||
@@ -10,17 +10,17 @@ export function getBindingIdentifiers(
|
||||
outerOnly?: boolean
|
||||
): Object {
|
||||
let search = [].concat(node);
|
||||
let ids = Object.create(null);
|
||||
const ids = Object.create(null);
|
||||
|
||||
while (search.length) {
|
||||
let id = search.shift();
|
||||
const id = search.shift();
|
||||
if (!id) continue;
|
||||
|
||||
let keys = t.getBindingIdentifiers.keys[id.type];
|
||||
const keys = t.getBindingIdentifiers.keys[id.type];
|
||||
|
||||
if (t.isIdentifier(id)) {
|
||||
if (duplicates) {
|
||||
let _ids = ids[id.name] = ids[id.name] || [];
|
||||
const _ids = ids[id.name] = ids[id.name] || [];
|
||||
_ids.push(id);
|
||||
} else {
|
||||
ids[id.name] = id;
|
||||
@@ -48,7 +48,7 @@ export function getBindingIdentifiers(
|
||||
|
||||
if (keys) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
const key = keys[i];
|
||||
if (id[key]) {
|
||||
search = search.concat(id[key]);
|
||||
}
|
||||
|
||||
@@ -10,11 +10,11 @@ import { BLOCK_SCOPED_SYMBOL } from "./constants";
|
||||
*/
|
||||
|
||||
export function isBinding(node: Object, parent: Object): boolean {
|
||||
let keys = getBindingIdentifiers.keys[parent.type];
|
||||
const keys = getBindingIdentifiers.keys[parent.type];
|
||||
if (keys) {
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
let val = parent[key];
|
||||
const key = keys[i];
|
||||
const val = parent[key];
|
||||
if (Array.isArray(val)) {
|
||||
if (val.indexOf(node) >= 0) return true;
|
||||
} else {
|
||||
@@ -73,7 +73,7 @@ export function isReferenced(node: Object, parent: Object): boolean {
|
||||
case "ArrowFunctionExpression":
|
||||
case "FunctionDeclaration":
|
||||
case "FunctionExpression":
|
||||
for (let param of (parent.params: Array<any>)) {
|
||||
for (const param of (parent.params: Array<any>)) {
|
||||
if (param === node) return false;
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ export function isNodesEquivalent(a, b) {
|
||||
|
||||
const fields = Object.keys(t.NODE_FIELDS[a.type] || a.type);
|
||||
|
||||
for (let field of fields) {
|
||||
for (const field of fields) {
|
||||
if (typeof a[field] !== typeof b[field]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
let t = require("../lib");
|
||||
let assert = require("assert");
|
||||
let parse = require("babylon").parse;
|
||||
const t = require("../lib");
|
||||
const assert = require("assert");
|
||||
const parse = require("babylon").parse;
|
||||
|
||||
suite("cloning", function () {
|
||||
suite("clone", function () {
|
||||
it("should handle undefined", function () {
|
||||
let node = undefined;
|
||||
let cloned = t.clone(node);
|
||||
const node = undefined;
|
||||
const cloned = t.clone(node);
|
||||
assert(cloned === undefined);
|
||||
});
|
||||
|
||||
it("should handle null", function () {
|
||||
let node = null;
|
||||
let cloned = t.clone(node);
|
||||
const node = null;
|
||||
const cloned = t.clone(node);
|
||||
assert(cloned === null);
|
||||
});
|
||||
|
||||
it("should handle simple cases", function () {
|
||||
let node = t.arrayExpression([null, t.identifier("a")]);
|
||||
let cloned = t.clone(node);
|
||||
const node = t.arrayExpression([null, t.identifier("a")]);
|
||||
const cloned = t.clone(node);
|
||||
assert(node !== cloned);
|
||||
assert(t.isNodesEquivalent(node, cloned) === true);
|
||||
});
|
||||
@@ -26,42 +26,42 @@ suite("cloning", function () {
|
||||
|
||||
suite("cloneDeep", function () {
|
||||
it("should handle undefined", function () {
|
||||
let node = undefined;
|
||||
let cloned = t.cloneDeep(node);
|
||||
const node = undefined;
|
||||
const cloned = t.cloneDeep(node);
|
||||
assert(cloned === undefined);
|
||||
});
|
||||
|
||||
it("should handle null", function () {
|
||||
let node = null;
|
||||
let cloned = t.cloneDeep(node);
|
||||
const node = null;
|
||||
const cloned = t.cloneDeep(node);
|
||||
assert(cloned === null);
|
||||
});
|
||||
|
||||
it("should handle simple cases", function () {
|
||||
let node = t.arrayExpression([null, t.identifier("a")]);
|
||||
let cloned = t.cloneDeep(node);
|
||||
const node = t.arrayExpression([null, t.identifier("a")]);
|
||||
const cloned = t.cloneDeep(node);
|
||||
assert(node !== cloned);
|
||||
assert(t.isNodesEquivalent(node, cloned) === true);
|
||||
});
|
||||
|
||||
it("should handle full programs", function () {
|
||||
let node = parse("1 + 1");
|
||||
let cloned = t.cloneDeep(node);
|
||||
const node = parse("1 + 1");
|
||||
const cloned = t.cloneDeep(node);
|
||||
assert(node !== cloned);
|
||||
assert(t.isNodesEquivalent(node, cloned) === true);
|
||||
});
|
||||
|
||||
it("should handle complex programs", function () {
|
||||
let program = "'use strict'; function lol() { wow();return 1; }";
|
||||
let node = parse(program);
|
||||
let cloned = t.cloneDeep(node);
|
||||
const program = "'use strict'; function lol() { wow();return 1; }";
|
||||
const node = parse(program);
|
||||
const cloned = t.cloneDeep(node);
|
||||
assert(node !== cloned);
|
||||
assert(t.isNodesEquivalent(node, cloned) === true);
|
||||
});
|
||||
|
||||
it("should handle missing array element", function () {
|
||||
let node = parse("[,0]");
|
||||
let cloned = t.cloneDeep(node);
|
||||
const node = parse("[,0]");
|
||||
const cloned = t.cloneDeep(node);
|
||||
assert(node !== cloned);
|
||||
assert(t.isNodesEquivalent(node, cloned) === true);
|
||||
});
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
let t = require("../lib");
|
||||
let assert = require("assert");
|
||||
let parse = require("babylon").parse;
|
||||
const t = require("../lib");
|
||||
const assert = require("assert");
|
||||
const parse = require("babylon").parse;
|
||||
|
||||
suite("validators", function () {
|
||||
suite("isNodesEquivalent", function () {
|
||||
it("should handle simple cases", function () {
|
||||
let mem = t.memberExpression(t.identifier("a"), t.identifier("b"));
|
||||
const mem = t.memberExpression(t.identifier("a"), t.identifier("b"));
|
||||
assert(t.isNodesEquivalent(mem, mem) === true);
|
||||
|
||||
let mem2 = t.memberExpression(t.identifier("a"), t.identifier("c"));
|
||||
const mem2 = t.memberExpression(t.identifier("a"), t.identifier("c"));
|
||||
assert(t.isNodesEquivalent(mem, mem2) === false);
|
||||
});
|
||||
|
||||
@@ -18,11 +18,11 @@ suite("validators", function () {
|
||||
});
|
||||
|
||||
it("should handle complex programs", function () {
|
||||
let program = "'use strict'; function lol() { wow();return 1; }";
|
||||
const program = "'use strict'; function lol() { wow();return 1; }";
|
||||
|
||||
assert(t.isNodesEquivalent(parse(program), parse(program)) === true);
|
||||
|
||||
let program2 = "'use strict'; function lol() { wow();return -1; }";
|
||||
const program2 = "'use strict'; function lol() { wow();return -1; }";
|
||||
|
||||
assert(t.isNodesEquivalent(parse(program), parse(program2)) === false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user