123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 'use strict';
- const Collection = require('../Collection');
- const NodeCollection = require('./Node');
- const once = require('../utils/once');
- const recast = require('recast');
- const astNodesAreEquivalent = recast.types.astNodesAreEquivalent;
- const b = recast.types.builders;
- var types = recast.types.namedTypes;
- const VariableDeclarator = recast.types.namedTypes.VariableDeclarator;
- const globalMethods = {
-
- findVariableDeclarators: function(name) {
- const filter = name ? {id: {name: name}} : null;
- return this.find(VariableDeclarator, filter);
- }
- };
- const filterMethods = {
-
- requiresModule: function(names) {
- if (names && !Array.isArray(names)) {
- names = [names];
- }
- const requireIdentifier = b.identifier('require');
- return function(path) {
- const node = path.value;
- if (!VariableDeclarator.check(node) ||
- !types.CallExpression.check(node.init) ||
- !astNodesAreEquivalent(node.init.callee, requireIdentifier)) {
- return false;
- }
- return !names ||
- names.some(
- n => astNodesAreEquivalent(node.init.arguments[0], b.literal(n))
- );
- };
- }
- };
- const transformMethods = {
-
- renameTo: function(newName) {
-
- return this.forEach(function(path) {
- const node = path.value;
- const oldName = node.id.name;
- const rootScope = path.scope;
- const rootPath = rootScope.path;
- Collection.fromPaths([rootPath])
- .find(types.Identifier, {name: oldName})
- .filter(function(path) {
- const parent = path.parent.node;
- if (
- types.MemberExpression.check(parent) &&
- parent.property === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- if (
- types.Property.check(parent) &&
- parent.key === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- if (
- types.ObjectProperty.check(parent) &&
- parent.key === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- if (
- types.ObjectMethod.check(parent) &&
- parent.key === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- if (
- types.MethodDefinition.check(parent) &&
- parent.key === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- if (
- types.ClassMethod.check(parent) &&
- parent.key === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- if (
- types.ClassProperty.check(parent) &&
- parent.key === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- if (
- types.JSXAttribute.check(parent) &&
- parent.name === path.node &&
- !parent.computed
- ) {
-
- return false;
- }
- return true;
- })
- .forEach(function(path) {
- let scope = path.scope;
- while (scope && scope !== rootScope) {
- if (scope.declares(oldName)) {
- return;
- }
- scope = scope.parent;
- }
- if (scope) {
-
-
-
-
- const parent = path.parent.node;
- if (
- types.Property.check(parent) &&
- parent.shorthand &&
- !parent.method
- ) {
- path.parent.get('shorthand').replace(false);
- }
- path.get('name').replace(newName);
- }
- });
- });
- }
- };
- function register() {
- NodeCollection.register();
- Collection.registerMethods(globalMethods);
- Collection.registerMethods(transformMethods, VariableDeclarator);
- }
- exports.register = once(register);
- exports.filters = filterMethods;
|