refactor

Simple refactor for es6 code.

Source:

Methods

(static) addExportFrom(ast, moduleSource)

Source:

Export from a given module source. This methods operates export ... from statement:

Parameters:
Name Type Description
ast string

Which module to manage export from statement.

moduleSource string

From which module source to add import from. If not found, the create an import line.

(static) addImportFrom(ast, moduleSource)

Source:

Import from a given module source. This methods operates import statement: import defaultImmport, { namedImport ... } from './module-source'; It noly supports es6 modules import but not commonJS or AMD or others...

Example
const refactor = require('rekit-core').refactor;
refactor.addImportFrom(file, './some-module', 'SomeModule', ['method1', 'method2']);
// it generates: import SomeModule, { method1, method2 } from './some-module';
Parameters:
Name Type Description
ast string

Which module to manage import statement.

moduleSource string

From which module source to add import from. If not found, the create an import line.

(static) addToArray(ast, varName, identifierName)

Source:

Add an identifier to the array of the name 'varName'. It only finds the first matched array in the top scope of ast.

Example
const refactor = require('rekit-core').refactor;
// code in ast: const arr = [a, b, c];
refactor.addToArray(file, 'arr', 'd');
// code changes to: const arr = [a, b, c, d];
Parameters:
Name Type Description
ast object | string

The ast tree or the js file path to find the array.

varName string

The variable name of the array definition.

identifierName string

The identifier to append to array.

(static) removeFromArray(ast, varName, identifierName)

Source:

Remove an identifier from the array of the name 'varName'. It only finds the first matched array in the global scope of ast.

Example
const refactor = require('rekit-core').refactor;
// code in ast: const arr = [a, b, c];
refactor.removeFromArray(file, 'arr', 'a');
// code changes to: const arr = [b, c];
Parameters:
Name Type Description
ast object | string

The ast tree or the js file path to find the array.

varName string

The variable name of the array definition.

identifierName string

The identifier to append to array.

(static) renameClassName(ast, oldName)

Source:

Rename a es6 class name in a module. Only rename the class definition and its reference in the module. It will not find other files to rename reference.

Example
// class MyClass {
//   ...
// }
const refactor = require('rekit-core').refactor;
refactor.renameClassName(file, 'MyClass', 'NewMyClass');
// => class NewMyClass {
// =>   ...
// => }
Parameters:
Name Type Description
ast string

Which module to rename class name.

oldName string

The old class name.

(static) renameFunctionName(ast, oldName)

Source:

Rename a function name in a module.

Example
// function MyFunc() {
//   ...
// }
const refactor = require('rekit-core').refactor;
refactor.renameFunctionName(file, 'MyFunc', 'NewMyFunc');
// => function NewMyFunc {
// =>   ...
// => }
Parameters:
Name Type Description
ast string

Which module to rename function.

oldName string

The old function name.

(static) renameIdentifier(ast, oldName)

Source:

Rename an top scope identifier in a module. If first finds the definition node of the given name. Then rename all identifiers those refer to that definition node.

Example
// import { m1 } from './some-module';
// m1.doSomething();
// function () { const m1 = 'abc'; }
const refactor = require('rekit-core').refactor;
refactor.renameIdentifier(file, 'm1', 'm2');
// => import { m2 } from './some-module';
// => m2.doSomething();
// => function () { const m1 = 'abc'; } // m1 is not renamed.
Parameters:
Name Type Description
ast string

Which module to rename an identifier.

oldName string

The old identifier name.