NodeMutation

NodeMutation

new NodeMutation(source, options)

Initialize a NodeMutation
Source:
Parameters:
Name Type Description
source string file source.
options object
Name Type Description
adapter Adapter.<T> adapter to parse the node

Methods

(static) configure(options)

Configure NodeMutation
Source:
Parameters:
Name Type Description
options Object
Name Type Description
strategy Strategy strategy, default is Strategy.THROW_ERROR
tabWidth Number tab width, default is 2

append(node, code)

Append code to the ast node.
Source:
Parameters:
Name Type Description
node T ast node
code string new code to append
Example
source code of the ast node is
```
class FooBar {
  foo() {}
}
```
then we call
```
mutation.append(node, "bar() {}");
```
the source code will be rewritten to
```
class FooBar {
  foo() {}
  bar() {}
}
```

delete(node, selectors, options)

Delete source code of the child ast node
Source:
Parameters:
Name Type Description
node T current ast node
selectors string | Array.<string> selectors to find chid ast nodes
options DeleteOptions
Example
source code of the ast node is
```
this.foo.bind(this)
```
then we call
```
mutation.delete(["expression.expression.dot", "expression.expression.name", "expression.arguments"])
```
the source code will be rewritten to
```
this.foo
```

group(func)

Group actions, it works both sync and async.
Source:
Parameters:
Name Type Description
func function actions in the function will be grouped

indent(node, options)

Indent the ast node.
Source:
Parameters:
Name Type Description
node T ast node
options IndentOptions
Example
source code of the ast node is
```
class FooBar {
}
```
then we call
```
mutation.indent(node, { tabSize: 1 });
```
the source code will be rewritten to
```
  class FooBar {
  }
```

insert(node, code, options)

Insert code to the ast node.
Source:
Parameters:
Name Type Description
node T ast node
code string new code to insert
options InsertOptions
Example
source code of the ast node is
```
this.foo
```
then we call
```
mutation.insert(node, "::", { at: "beginning" });
```
the source code will be rewritten to
```
::this.foo
```
if we call
```
mutation.insert(node, ".bar", { to: "expression.expression" })
```
the source code will be rewritten to
```
this.foo.bar
```

noop(node)

No operation.
Source:
Parameters:
Name Type Description
node T ast node

prepend(node, code)

Prepend code to the ast node.
Source:
Parameters:
Name Type Description
node T ast node
code string new code to prepend
Example
source code of the ast node is
```
class FooBar {
  foo() {}
}
```
then we call
```
mutation.prepend(node, "bar() {}");
```
the source code will be rewritten to
```
class FooBar {
  bar() {}
  foo() {}
}
```

process() → {ProcessResult}

Rewrite the source code based on all actions. If there's an action range conflict, it will raise a ConflictActionError if strategy is set to THROW_ERROR, it will process all non conflicted actions and return `{ conflicted: true }`
Source:
Returns:
Type:
ProcessResult

remove(node, options)

Remove source code of the ast node
Source:
Parameters:
Name Type Description
node T ast node
options RemoveOptions
Example
source code of the ast node is
```
this.foo.bind(this)
```
then we call
```
mutation.remove()
```
the source code will be completely removed

replace(node, selectors, options)

Replace child node of the ast node with new code
Source:
Parameters:
Name Type Description
node T current ast node
selectors string | Array.<string> selectors to find chid ast nodes
options ReplaceOptions
Example
source code of the ast node is
```
class FooBar {}
```
then we call
```
mutation.replace(node, "name", { with: "Synvert" });
```
the source code will be rewritten to
```
class Synvert {}
```

replaceWith(node, code)

Replace the ast node with new code
Source:
Parameters:
Name Type Description
node T ast node
code string new code to replace
Example
source code of the ast node is
```
!!foobar
```
then we call
```
mutation.replaceWith(node, "Boolean({{expression.operand.operand}})");
```
the source code will be rewritten to
```
Boolean(foobar)
```

rewriteSource(source, actions) → {string}

Rewrite source code with actions.
Source:
Parameters:
Name Type Description
source string source code
actions Array.<Action> actions
Returns:
Type:
string
new source code

test() → {TestResult}

Return the actions. If there's an action range conflict, it will raise a ConflictActionError if strategy is set to THROW_ERROR, it will process all non conflicted actions and return `{ conflicted: true }`
Source:
Returns:
Type:
TestResult
if actions are conflicted and the actions

wrap(node, prefix, suffix, newLine)

Wrap the ast node with prefix and suffix.
Source:
Parameters:
Name Type Description
node T ast node
prefix string prefix
suffix string suffix
newLine boolean if true, the prefix and suffix will be wrapped in a new line
Example
source code of the ast node is
```
console.log('foo)
```
then we call
```
mutation.wrap(node, { prefix: "function logFoo() {", suffix: "}", newLine: true });
```
the source code will be rewritten to
```
function logFoo() {
  console.log('foo')
}
```