Skip to content

Commit

Permalink
extracted common and unsafe methods into plugins, formatted, removed …
Browse files Browse the repository at this point in the history
…old deps
  • Loading branch information
jsoverson committed Aug 3, 2020
1 parent a2c901c commit fd81f92
Show file tree
Hide file tree
Showing 31 changed files with 1,168 additions and 6,701 deletions.
66 changes: 63 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,19 @@ const names = $bindingIdentifiers.map(node => node.name);
- [`.codegen()`](#codegen)
- [`.declarations()`](#declarations)
- [`.delete()`](#delete)
- [`.filter(iterator)`](#filteriterator)
- [`.find(iterator)`](#finditerator)
- [`.findMatchingExpression(sampleSrc)`](#findmatchingexpressionsamplesrc)
- [`.findMatchingStatement(sampleSrc)`](#findmatchingstatementsamplesrc)
- [`.findOne(selectorOrNode)`](#findoneselectorornode)
- [`.first()`](#first)
- [`.first(selector)`](#firstselector)
- [`.forEach(iterator)`](#foreachiterator)
- [`.get(index)`](#getindex)
- [`.logOut()`](#logout)
- [`.lookupVariable()`](#lookupvariable)
- [`.lookupVariableByName(name)`](#lookupvariablebynamename)
- [`.map(iterator)`](#mapiterator)
- [`.nameString()`](#namestring)
- [`.parents()`](#parents)
- [`.prepend(replacer)`](#prependreplacer)
- [`.print()`](#print)
Expand All @@ -184,6 +186,7 @@ const names = $bindingIdentifiers.map(node => node.name);
- [`.replace(replacer)`](#replacereplacer)
- [`.replaceAsync(replacer)`](#replaceasyncreplacer)
- [`.replaceChildren(query, replacer)`](#replacechildrenquery-replacer)
- [`.statements()`](#statements)
- [`.toJSON()`](#tojson)

#### `.$(queryOrNodes)`
Expand Down Expand Up @@ -320,6 +323,27 @@ $script = refactor('foo();bar();');
$script('ExpressionStatement[expression.callee.name="foo"]').delete();
```

#### `.filter(iterator)`

Filter selected nodes via passed iterator

#### Example

```js
const { refactor } = require('shift-refactor');

const src = `
let doc = window.document;
function addListener(event, fn) {
doc.addEventListener(event, fn);
}
`

$script = refactor(src);

const values = $script('BindingIdentifier').filter(node => node.name === 'doc');
```

#### `.find(iterator)`

Finds node via the passed iterator iterator
Expand Down Expand Up @@ -418,9 +442,9 @@ $script = refactor(src);
const innerVariableDecl = $script('FunctionDeclaration').findOne('VariableDeclarator');
```

#### `.first()`
#### `.first(selector)`

Returns the first node in the list
Returns the first selected node. Optionally takes a selector and returns the first node that matches the selector.

#### Example

Expand Down Expand Up @@ -559,6 +583,23 @@ $script = refactor(src);
const values = $script('BindingIdentifier').map(node => node.name);
```

#### `.nameString()`

Retrieve the names of the first selected node. Returns undefined for nodes without names.

#### Example

```js
const { refactor } = require('shift-refactor');

const src = `
var first = 1, second = 2;
`

$script = refactor(src);
const firstName = $script('BindingIdentifier[name="first"]').nameString();
```

#### `.parents()`

Retrieve parent node(s)
Expand Down Expand Up @@ -757,6 +798,25 @@ $script.replaceChildren(
);
```

#### `.statements()`

Returns the selects the statements for the selected nodes. Note: it will "uplevel" the inner statements of nodes with a`.body`property.Does nothing for nodes that have no statements property.

#### Example

```js
const { refactor } = require('shift-refactor');

const src = `
console.log(1);
console.log(2);
`

$script = refactor(src);

const rootStatements = $script.statements();
```

#### `.toJSON()`

JSON-ifies the current selected nodes.
Expand Down
Loading

0 comments on commit fd81f92

Please sign in to comment.