Instance

Instance

Instance is an execution unit, it finds specified ast nodes, checks if the nodes match some conditions, then insert, replace or delete code. One instance can contains one or many Scope and Condition.

Constructor

new Instance(filePath, func)

Create an Instance
Properties:
Name Type Description
filePath string file path to run instance
Source:
Parameters:
Name Type Description
filePath string file path
func function a function to find nodes, match conditions and rewrite code.

Members

mutationAdapter

Get currentMutation's adapter.
Source:

parser

Get rewriter's parser.
Source:

Methods

addLeadingSpaces(str, options) → {string}

Add leading spaces to the str according to Configuration.tabWidth.
Source:
Parameters:
Name Type Description
str string string
options object
Name Type Description
tabSize number tab size, default is 1
Returns:
Type:
string

append(code)

Append the code to the bottom of current node body.
Source:
Parameters:
Name Type Description
code string need to be appended.
Example
// foo() => {}
// will be converted to
// foo() => {}
// bar() => {}
// after executing
withNode({ nodeType: "MethodDefinition", key: "foo" }, () => {
  append("bar() => {}")
})

appendSemicolon(str) → {string}

Append semicolon to str if Configuration.semi is true.
Source:
Parameters:
Name Type Description
str string string
Returns:
Type:
string

(async) callHelper(helperName, options)

Async to call a helper to run shared code.
Source:
Parameters:
Name Type Description
helperName string snippet helper name, it can be a http url, file path or a helper name
options options can be anything it needs to be passed to the helper

callHelperSync(helperName, options)

Sync to call a helper to run shared code.
Source:
Parameters:
Name Type Description
helperName string snippet helper name, it can be a http url, file path or a helper name
options options can be anything it needs to be passed to the helper

delete(selectors, options)

Delete child nodes.
Source:
Parameters:
Name Type Attributes Description
selectors string | Array.<string> name of child nodes
options Object
Name Type Attributes Default Description
wholeLine boolean <optional>
false remove the whole line
option.andComma boolean <optional>
delete additional comma
Example
// const someObject = { cat: cat, dog: dog, bird: bird }
// will be converted to
// const someObject = { cat, dog, bird }
// after executing
withNode({ nodeType: "Property", key: { nodeType: "Identifier" }, value: { nodeType: "Identifier" } }, () => {
  delete(["semicolon", "value"]);
});

(async) gotoNode(child_node_name, func)

Create a GotoScope to go to a child node, then continue operating on the child node.
Source:
Parameters:
Name Type Description
child_node_name string the name of the child nodes.
func function to continue operating on the matching nodes.
Example
// `$.ajax({ ... })` goes to `$.ajax`
await gotoNode('callee', async () => {})

gotoNodeSync(child_node_name, func)

Create a GotoScope to go to a child node, then continue operating on the child node.
Source:
Parameters:
Name Type Description
child_node_name string the name of the child nodes.
func function to continue operating on the matching nodes.
Example
// `$.ajax({ ... })` goes to `$.ajax`
gotoNodeSync('callee', () => { })

(async) group(func)

Group actions.
Source:
Parameters:
Name Type Description
func function
Example
group(() => {
  delete("leftCurlyBracket");
  delete("rightCurlyBracket", { wholeLine: true });
});

(async) ifAllNodes(nqlOrRules, options, func, elseFunc)

Create a IfAllCondition to check if all matching nodes match options.match, if so, then call the func, else call the elseFunc.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOptions.<T> | function { match: nqlOrRules, in: 'callee' }
func function call the function if the matching nodes match options.match.
elseFunc function call the else function if no matching node matches options.match.
Example
// `class Foobar { foo() {}; bar() {}; }` matches and call foobar
await ifAllNodes({ nodeType: "MethodDefinition" }, { match: { key: { in: ["foo", "bar"] } } }, () => { foo }, async () => { bar });

ifAllNodesSync(nqlOrRules, options, func, elseFunc)

Create a IfAllCondition to check if all matching nodes match options.match, if so, then call the func, else call the elseFunc.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOptions.<T> | function { match: nqlOrRules, in: 'callee' }
func function call the function if the matching nodes match options.match.
elseFunc function call the else function if no matching node matches options.match.
Example
// `class Foobar { foo() {}; bar() {}; }` matches and call foobar
ifAllNodesSync({ nodeType: "MethodDefinition" }, { match: { key: { in: ["foo", "bar"] } } }, () => { foo }, () => { bar });

(async) ifExistNode(nqlOrRules, options, func, elseFunc)

Create a IfExistCondition to check if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOptions.<T> | function to do find in specific child node, e.g. { in: 'callee' }
func function call the function if the matching nodes exist in the child nodes.
elseFunc function call the else function if no matching node exists in the child nodes.
Example
// `class Foobar extends React.Component` matches and call `foobar`.
await ifExistNode({ nodeType: "ClassDeclaration", superClass: { nodeType: "MemberExpression", object: "React", property: "Component" } }, async () => { foobar })

ifExistNodeSync(nqlOrRules, options, func, elseFunc)

Create a IfExistCondition to check if matching nodes exist in the child nodes, if so, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOptions.<T> | function to do find in specific child node, e.g. { in: 'callee' }
func function call the function if the matching nodes exist in the child nodes.
elseFunc function call the else function if no matching node exists in the child nodes.
Example
// `class Foobar extends React.Component` matches and call `foobar`.
ifExistNodeSync({ nodeType: "ClassDeclaration", superClass: { nodeType: "MemberExpression", object: "React", property: "Component" } }, () => { foobar })

(async) ifOnlyExistNode(nqlOrRules, options, func, elseFunc)

Create a IfOnlyExistCondition to check if current node has only one child node and the child node matches rules, if so, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOption | function to do find in specific child node, e.g. { in: 'callee' }
func function call the function if the matching nodes exist in the child nodes.
elseFunc function call the else function if no matching node exists in the child nodes.
Example
// `class Foobar { foo() {} }` matches and call foobar, `class Foobar { foo() {}; bar() {}; }` does not match
await ifOnlyExistNode({ nodeType: "MethodDefinition", key: "foo" }, async () => { foobar })

ifOnlyExistNodeSync(nqlOrRules, options, func, elseFunc)

Create a IfOnlyExistCondition to check if current node has only one child node and the child node matches rules, if so, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOptions.<T> | function to do find in specific child node, e.g. { in: 'callee' }
func function call the function if the matching nodes exist in the child nodes.
elseFunc function call the else function if no matching node exists in the child nodes.
Example
// `class Foobar { foo() {} }` matches and call foobar, `class Foobar { foo() {}; bar() {}; }` does not match
ifOnlyExistNodeSync({ nodeType: "MethodDefinition", key: "foo" }, () => { foobar })

indent(str, spaceCount, options) → {string}

Indent each line in a string.
Source:
Parameters:
Name Type Description
str string
spaceCount number
options object
Name Type Description
skipFirstLine number skip first line, default is false
Returns:
Type:
string
indented str
Example
//   foo
//   bar
indent("foo\nbar", 2)

insert(code, options)

Insert code to the beginning or end of the current node.
Source:
Parameters:
Name Type Attributes Description
code string code need to be inserted
options Object
Name Type Attributes Default Description
at string <optional>
"end" insert position, beginning or end
option.to string <optional>
selector to find the child ast node
option.andComma boolean <optional>
insert additional comma
option.andSpace boolean <optional>
insert additional space
Example
// import React, { Component } from 'react'
// will be converted to
// import React, { Component, useState } from 'react'
// after executing
withNode({ nodeType: "ImportSpecifier", name: "Component" }, () => {
  insert(", useState", { at: "end" });
});

insertAfter(code, options)

Insert the code next to the current node.
Source:
Parameters:
Name Type Attributes Description
code string code need to be inserted
options Object
Name Type Attributes Description
to string <optional>
selector to find the child ast node
option.andComma boolean <optional>
insert additional comma
option.andSpace boolean <optional>
insert additional space
Example
// import React from 'react'
// will be converted to
// import React from 'react'
// import PropTypes from 'prop-types'
// after executing
withNode({ nodeType: "ImportClause", name: "React" }, () => {
  insertAfter("import PropTypes from 'prop-types'");
});

insertBefore(code, options)

Insert the code previous to the current node.
Source:
Parameters:
Name Type Attributes Description
code string code need to be inserted
options Object
Name Type Attributes Description
to string <optional>
selector to find the child ast node
option.andComma boolean <optional>
insert additional comma
option.andSpace boolean <optional>
insert additional space
Example
// import React from 'react'
// will be converted to
// import PropTypes from 'prop-types'
// import React from 'react'
// after executing
withNode({ nodeType: "ImportClause", name: "React" }, () => {
  insertBefore("import PropTypes from 'prop-types'");
});

noop()

No operation.
Source:

prepend(code)

Prepend the code to the top of current node body.
Source:
Parameters:
Name Type Description
code string need to be prepended.
Example
// const foo = bar
// will be converted to
// 'use strict'
// const foo = bar
// after executing
prepend("'use strict'");

(async) process()

Process one file.
Source:

processSync()

Process one file.
Source:

(async) processWithNode(node, func)

Set currentNode to node and process.
Source:
Parameters:
Name Type Description
node T set to current node
func function

processWithNodeSync(node, func)

Set currentNode to node and process.
Source:
Parameters:
Name Type Description
node T set to current node
func function

(async) processWithOtherNode(node, func)

Set currentNode properly, process and set currentNode back to original currentNode.
Source:
Parameters:
Name Type Description
node T set to other node
func function

processWithOtherNodeSync(node, func)

Set currentNode properly, process and set currentNode back to original currentNode.
Source:
Parameters:
Name Type Description
node T set to other node
func function

remove(options)

Remove current node.
Source:
Parameters:
Name Type Attributes Description
options Object
option.andComma boolean <optional>
remove additional comma
Example
// class A {
//   constructor(props) {
//     super(props)
//   }
// }
// will be converted to
// class A {
// }
// after executing
withNode({ nodeType: "MethodDefinition", kind: "constructor" }, () => {
  remove();
});

replace(selectors, options)

Replace child nodes with code.
Source:
Parameters:
Name Type Description
selectors string | Array.<string> name of child nodes.
options Object
Name Type Description
with string new code to replace with
Example
// $form.submit();
// will be converted to
// $form.trigger('submit');
// after executing
withNode({ nodeType: "CallExpression", callee: { nodeType: "MemberExpression", object: /^\$/, property: 'submit' }, arguments: { length: 0 } }, () => {
  replace(["callee.property", "arguments"], { with: "trigger('submit')" });
});

replaceWith(code)

Replace current node with code.
Source:
Parameters:
Name Type Description
code string code need to be replaced.
Example
// module.exports = Rewriter
// will be converted to
// export default Rewriter
// after executing
withNode({ nodeType: "ExpressionStatement", expression: { nodeType: "AssignmentExpression", left: { nodeType: "MemberExpression", object: "module", property: "exports" }, right: { nodeType: "Identifier" } } }, () => {
  replaceWith("export default {{expression.right}}");
});

(async) test() → {Promise.<TestResultExt>}

Test one file.
Source:
Returns:
Type:
Promise.<TestResultExt>

testSync() → {TestResultExt}

Test one file.
Source:
Returns:
Type:
TestResultExt

(async) unlessExistNode(nqlOrRules, options, func, elseFunc)

Create a UnlessExistCondition to check if matching nodes does not exist in the child nodes, if so, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOptions.<T> to do find in specific child node, e.g. { in: 'callee' }
func function call the function if no matching node exists in the child nodes.
elseFunc function call the else function if the matching nodes exists in the child nodes.
Example
// `class Foobar extends Component` matches and call `foobar`.
await unlessExistNode({ nodeType: "ClassDeclaration", superClass: { nodeType: "MemberExpression", object: "React", property: "Component" } }, async () => {})

unlessExistNodeSync(nqlOrRules, options, func, elseFunc)

Create a UnlessExistCondition to check if matching nodes does not exist in the child nodes, if so, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to check matching ast nodes.
options ConditionOptions.<T> to do find in specific child node, e.g. { in: 'callee' }
func function call the function if no matching node exists in the child nodes.
elseFunc function call the else function if the matching nodes exists in the child nodes.
Example
// `class Foobar extends Component` matches and call `foobar`.
unlessExistNodeSync({ nodeType: "ClassDeclaration", superClass: { nodeType: "MemberExpression", object: "React", property: "Component" } }, () => {})

(async) withinNode(nqlOrRules, options, func)

Create a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to find matching ast nodes.
options QueryOptions | function query options.
func function to be called on the matching nodes.
Example
// `$.ajax({ ... })` matches and call `foobar`
await withinNode({ nodeType: "CallExpression", callee: { nodeType: "MemberExpression", object: "$", property: "ajax" } }, async () => { foobar })
await withinNode(".CallExpression[callee=.MemberExpression[object=$][property=ajax]]", async () => { foobar });

withinNodeSync(nqlOrRules, options, func)

Create a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to find matching ast nodes.
options QueryOptions | function query options.
func function to be called on the matching nodes.
Example
// `$.ajax({ ... })` matches and call `foobar`
withinNodeSync({ nodeType: "CallExpression", callee: { nodeType: "MemberExpression", object: "$", property: "ajax" } }, () => { foobar })
withinNodeSync(".CallExpression[callee=.MemberExpression[object=$][property=ajax]]", () => { foobar });

(async) withNode(nqlOrRules, options, func)

Create a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to find matching ast nodes.
options QueryOptions | function query options.
func function to be called on the matching nodes.
Example
// `$.ajax({ ... })` matches and call `foobar`
await withinNode({ nodeType: "CallExpression", callee: { nodeType: "MemberExpression", object: "$", property: "ajax" } }, async () => { foobar })
await withinNode(".CallExpression[callee=.MemberExpression[object=$][property=ajax]]", async () => { foobar });

(async) withNode(nqlOrRules, options, func)

Create a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to find matching ast nodes.
options QueryOptions | function query options.
func function to be called on the matching nodes.
Example
// `$.ajax({ ... })` matches and call `foobar`
await withinNode({ nodeType: "CallExpression", callee: { nodeType: "MemberExpression", object: "$", property: "ajax" } }, async () => { foobar })
await withinNode(".CallExpression[callee=.MemberExpression[object=$][property=ajax]]", async () => { foobar });

withNodeSync(nqlOrRules, options, func)

Create a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to find matching ast nodes.
options QueryOptions | function query options.
func function to be called on the matching nodes.
Example
// `$.ajax({ ... })` matches and call `foobar`
withinNodeSync({ nodeType: "CallExpression", callee: { nodeType: "MemberExpression", object: "$", property: "ajax" } }, () => { foobar })
withinNodeSync(".CallExpression[callee=.MemberExpression[object=$][property=ajax]]", () => { foobar });

withNodeSync(nqlOrRules, options, func)

Create a WithinScope to recursively find matching ast nodes, then continue operating on each matching ast node.
Source:
Parameters:
Name Type Description
nqlOrRules string | Object to find matching ast nodes.
options QueryOptions | function query options.
func function to be called on the matching nodes.
Example
// `$.ajax({ ... })` matches and call `foobar`
withinNodeSync({ nodeType: "CallExpression", callee: { nodeType: "MemberExpression", object: "$", property: "ajax" } }, () => { foobar })
withinNodeSync(".CallExpression[callee=.MemberExpression[object=$][property=ajax]]", () => { foobar });

wrapWithQuotes(str) → {string}

Wrap str string with single or double quotes based on Configuration.singleQuote.
Source:
Parameters:
Name Type Description
str string string
Returns:
Type:
string
quoted string