Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

abstract-syntax-tree

Package Overview
Dependencies
Maintainers
2
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abstract-syntax-tree - npm Package Compare versions

Comparing version 2.20.7 to 2.21.0

src/traverse/estraverse

18

package.json
{
"name": "abstract-syntax-tree",
"version": "2.20.7",
"version": "2.21.0",
"description": "abstract syntax tree",

@@ -8,3 +8,2 @@ "main": "index.js",

"test": "ava 'test/**/*.js'",
"lint": "standard",
"coverage": "c8 npm test"

@@ -36,5 +35,4 @@ },

"devDependencies": {
"ava": "^5.3.0",
"c8": "^8.0.0",
"standard": "^17.1.0"
"ava": "^5.3.1",
"c8": "^8.0.0"
},

@@ -45,16 +43,6 @@ "dependencies": {

"esquery": "^1.5.0",
"estraverse": "^5.3.0",
"meriyah": "^4.3.7",
"pure-conditions": "^1.2.1",
"source-map": "^0.7.4"
},
"standard": {
"ignore": [
"src/optimize/**/*",
"src/template/estemplate.js",
"test/optimize/**/*",
"test/nodes/**/*",
"test/template.js"
]
}
}

@@ -70,13 +70,13 @@ # abstract-syntax-tree

```js
const { parse, find } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, find } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
console.log(find(tree, 'Literal')) // [ { type: 'Literal', value: 42 } ]
console.log(find(tree, "Literal")) // [ { type: 'Literal', value: 42 } ]
```
```js
const AbstractSyntaxTree = require('abstract-syntax-tree')
const source = 'const answer = 42'
const AbstractSyntaxTree = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = new AbstractSyntaxTree(source)
console.log(tree.find('Literal')) // [ { type: 'Literal', value: 42 } ]
console.log(tree.find("Literal")) // [ { type: 'Literal', value: 42 } ]
```

@@ -93,4 +93,4 @@

```js
const { parse } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)

@@ -101,7 +101,7 @@ console.log(tree) // { type: 'Program', body: [ ... ] }

```js
const { parse } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source, {
loc: true,
ranges: true
ranges: true,
})

@@ -116,4 +116,4 @@ console.log(tree) // { type: 'Program', body: [ ... ], loc: {...} }

```js
const { parse, generate } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, generate } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)

@@ -128,4 +128,4 @@ console.log(generate(tree)) // 'const answer = 42;'

```js
const { parse, walk } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, walk } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)

@@ -150,7 +150,7 @@ walk(tree, (node, parent) => {

```js
const { parse, find } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, find } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
console.log(find(tree, 'VariableDeclaration')) // [ { type: 'VariableDeclaration', ... } ]
console.log(find(tree, { type: 'VariableDeclaration' })) // [ { type: 'VariableDeclaration', ... } ]
console.log(find(tree, "VariableDeclaration")) // [ { type: 'VariableDeclaration', ... } ]
console.log(find(tree, { type: "VariableDeclaration" })) // [ { type: 'VariableDeclaration', ... } ]
```

@@ -163,12 +163,12 @@

```js
const { serialize } = require('abstract-syntax-tree')
const { serialize } = require("abstract-syntax-tree")
const node = {
type: 'ArrayExpression',
type: "ArrayExpression",
elements: [
{ type: 'Literal', value: 1 },
{ type: 'Literal', value: 2 },
{ type: 'Literal', value: 3 },
{ type: 'Literal', value: 4 },
{ type: 'Literal', value: 5 }
]
{ type: "Literal", value: 1 },
{ type: "Literal", value: 2 },
{ type: "Literal", value: 3 },
{ type: "Literal", value: 4 },
{ type: "Literal", value: 5 },
],
}

@@ -183,8 +183,8 @@ const array = serialize(node) // [1, 2, 3, 4, 5]

```js
const { parse, traverse } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, traverse } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
traverse(tree, {
enter (node) {},
leave (node) {}
enter(node) {},
leave(node) {},
})

@@ -198,8 +198,8 @@ ```

```js
const { parse, replace } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, replace } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
replace(tree, node => {
if (node.type === 'VariableDeclaration') {
node.kind = 'let'
replace(tree, (node) => {
if (node.type === "VariableDeclaration") {
node.kind = "let"
}

@@ -215,3 +215,3 @@ return node

```js
const { parse, remove, generate } = require('abstract-syntax-tree')
const { parse, remove, generate } = require("abstract-syntax-tree")
const source = '"use strict"; const b = 4;'

@@ -236,6 +236,6 @@ const tree = parse(source)

```js
const { parse, each } = require('abstract-syntax-tree')
const source = 'const foo = 1; const bar = 2;'
const { parse, each } = require("abstract-syntax-tree")
const source = "const foo = 1; const bar = 2;"
const tree = parse(source)
each(tree, 'VariableDeclaration', node => {
each(tree, "VariableDeclaration", (node) => {
console.log(node)

@@ -248,6 +248,6 @@ })

```js
const { parse, first } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, first } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
console.log(first(tree, 'VariableDeclaration')) // { type: 'VariableDeclaration', ... }
console.log(first(tree, "VariableDeclaration")) // { type: 'VariableDeclaration', ... }
```

@@ -258,6 +258,6 @@

```js
const { parse, last } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, last } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
console.log(last(tree, 'VariableDeclaration')) // { type: 'VariableDeclaration', ... }
console.log(last(tree, "VariableDeclaration")) // { type: 'VariableDeclaration', ... }
```

@@ -268,11 +268,15 @@

```js
const { parse, reduce } = require('abstract-syntax-tree')
const source = 'const a = 1, b = 2'
const { parse, reduce } = require("abstract-syntax-tree")
const source = "const a = 1, b = 2"
const tree = parse(source)
const value = reduce(tree, (sum, node) => {
if (node.type === 'Literal') {
sum += node.value
}
return sum
}, 0)
const value = reduce(
tree,
(sum, node) => {
if (node.type === "Literal") {
sum += node.value
}
return sum
},
0
)
console.log(value) // 3

@@ -284,7 +288,7 @@ ```

```js
const { parse, has } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, has } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
console.log(has(tree, 'VariableDeclaration')) // true
console.log(has(tree, { type: 'VariableDeclaration' })) // true
console.log(has(tree, "VariableDeclaration")) // true
console.log(has(tree, { type: "VariableDeclaration" })) // true
```

@@ -295,7 +299,7 @@

```js
const { parse, count } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, count } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
console.log(count(tree, 'VariableDeclaration')) // 1
console.log(count(tree, { type: 'VariableDeclaration' })) // 1
console.log(count(tree, "VariableDeclaration")) // 1
console.log(count(tree, { type: "VariableDeclaration" })) // 1
```

@@ -308,28 +312,28 @@

```js
const { parse, append } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, append } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
append(tree, {
type: 'ExpressionStatement',
expression: {
type: "ExpressionStatement",
expression: {
type: "CallExpression",
callee: {
type: 'MemberExpression',
type: "MemberExpression",
object: {
type: 'Identifier',
name: 'console'
type: "Identifier",
name: "console",
},
property: {
type: 'Identifier',
name: 'log'
type: "Identifier",
name: "log",
},
computed: false
computed: false,
},
arguments: [
{
type: 'Identifier',
name: 'answer'
}
]
}
type: "Identifier",
name: "answer",
},
],
},
})

@@ -341,6 +345,6 @@ ```

```js
const { parse, append } = require('abstract-syntax-tree')
const source = 'const answer = 42'
const { parse, append } = require("abstract-syntax-tree")
const source = "const answer = 42"
const tree = parse(source)
append(tree, 'console.log(answer)')
append(tree, "console.log(answer)")
```

@@ -353,11 +357,11 @@

```js
const { parse, prepend } = require('abstract-syntax-tree')
const source = 'const a = 1;'
const { parse, prepend } = require("abstract-syntax-tree")
const source = "const a = 1;"
const tree = parse(source)
prepend(tree, {
type: 'ExpressionStatement',
type: "ExpressionStatement",
expression: {
type: 'Literal',
value: 'use strict'
}
type: "Literal",
value: "use strict",
},
})

@@ -369,6 +373,10 @@ ```

```js
const { equal } = require('abstract-syntax-tree')
console.log(equal({ type: 'Literal', value: 42 }, { type: 'Literal', value: 42 })) // true
console.log(equal({ type: 'Literal', value: 41 }, { type: 'Literal', value: 42 })) // false
````
const { equal } = require("abstract-syntax-tree")
console.log(
equal({ type: "Literal", value: 42 }, { type: "Literal", value: 42 })
) // true
console.log(
equal({ type: "Literal", value: 41 }, { type: "Literal", value: 42 })
) // false
```

@@ -378,8 +386,7 @@ #### match

```js
const { match } = require('abstract-syntax-tree')
console.log(match({ type: 'Literal', value: 42 }, 'Literal[value=42]')) // true
console.log(match({ type: 'Literal', value: 41 }, 'Literal[value=42]')) // false
````
const { match } = require("abstract-syntax-tree")
console.log(match({ type: "Literal", value: 42 }, "Literal[value=42]")) // true
console.log(match({ type: "Literal", value: 41 }, "Literal[value=42]")) // false
```
#### template

@@ -390,15 +397,16 @@

```js
const { template } = require('abstract-syntax-tree')
const { template } = require("abstract-syntax-tree")
const literal = template(42)
const nodes = template('const foo = <%= bar %>;', { bar: { type: 'Literal', value: 1 } })
const nodes = template("const foo = <%= bar %>;", {
bar: { type: "Literal", value: 1 },
})
```
```js
const { template } = require('abstract-syntax-tree')
const nodes = template('function foo(%= bar %) {}', {
const { template } = require("abstract-syntax-tree")
const nodes = template("function foo(%= bar %) {}", {
bar: [
{ type: 'Identifier', name: 'baz' },
{ type: 'Identifier', name: 'qux' }
]
{ type: "Identifier", name: "baz" },
{ type: "Identifier", name: "qux" },
],
})

@@ -412,3 +420,3 @@ ```

```js
const { program } = require('abstract-syntax-tree')
const { program } = require("abstract-syntax-tree")
const tree = program() // { type: 'Program', sourceType: 'module', body: [] }

@@ -422,7 +430,6 @@ ```

```js
const { iife } = require('abstract-syntax-tree')
const { iife } = require("abstract-syntax-tree")
const node = iife() // { type: 'ExpressionStatement', expression: { ... } }
```
### Instance Methods

@@ -435,7 +442,7 @@

```js
const AbstractSyntaxTree = require('abstract-syntax-tree')
const tree = new AbstractSyntaxTree('const a = 1')
const AbstractSyntaxTree = require("abstract-syntax-tree")
const tree = new AbstractSyntaxTree("const a = 1")
tree.mark()
console.log(tree.first('Program').cid) // 1
console.log(tree.first('VariableDeclaration').cid) // 2
console.log(tree.first("Program").cid) // 1
console.log(tree.first("VariableDeclaration").cid) // 2
```

@@ -446,23 +453,23 @@

```js
const AbstractSyntaxTree = require('abstract-syntax-tree')
const source = 'const a = 1'
const AbstractSyntaxTree = require("abstract-syntax-tree")
const source = "const a = 1"
const tree = new AbstractSyntaxTree(source)
tree.wrap(body => {
return [
{
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'FunctionExpression',
params: [],
body: {
type: 'BlockStatement',
body
}
tree.wrap((body) => {
return [
{
type: "ExpressionStatement",
expression: {
type: "CallExpression",
callee: {
type: "FunctionExpression",
params: [],
body: {
type: "BlockStatement",
body,
},
arguments: []
}
}
]
},
arguments: [],
},
},
]
})

@@ -474,4 +481,4 @@ ```

```js
const AbstractSyntaxTree = require('abstract-syntax-tree')
const source = '(function () { console.log(1); }())'
const AbstractSyntaxTree = require("abstract-syntax-tree")
const source = "(function () { console.log(1); }())"
const tree = new AbstractSyntaxTree(source)

@@ -493,3 +500,3 @@ tree.unwrap()

```js
const AbstractSyntaxTree = require('abstract-syntax-tree')
const AbstractSyntaxTree = require("abstract-syntax-tree")
const source = 'const foo = "bar";'

@@ -515,10 +522,10 @@ const tree = new AbstractSyntaxTree(source)

```js
const { toBinaryExpression } = require('abstract-syntax-tree')
const { toBinaryExpression } = require("abstract-syntax-tree")
const expression = {
type: 'ArrayExpression',
type: "ArrayExpression",
elements: [
{ type: 'Literal', value: 'foo' },
{ type: 'Literal', value: 'bar' },
{ type: 'Literal', value: 'baz' }
]
{ type: "Literal", value: "foo" },
{ type: "Literal", value: "bar" },
{ type: "Literal", value: "baz" },
],
}

@@ -533,7 +540,7 @@ console.log(toBinaryExpression(expression)) // { type: 'BinaryExpression', ... }

```js
const { ArrayExpression, Literal } = require('abstract-syntax-tree')
const { ArrayExpression, Literal } = require("abstract-syntax-tree")
const expression = new ArrayExpression([
new Literal('foo'),
new Literal('bar'),
new Literal('baz')
new Literal("foo"),
new Literal("bar"),
new Literal("baz"),
])

@@ -544,72 +551,72 @@ ```

| Type | Example |
|--------------------------|:----------------------------------------------------------------:|
| ArrayExpression | <code>const foo = []</code> |
| ArrayPattern | <code>const [foo, bar] = bar</code> |
| ArrowFunctionExpression | <code>(() => {})</code> |
| AssignmentExpression | <code>foo = bar</code> |
| Type | Example |
| ------------------------ | :--------------------------------------------------------------: |
| ArrayExpression | <code>const foo = []</code> |
| ArrayPattern | <code>const [foo, bar] = bar</code> |
| ArrowFunctionExpression | <code>(() => {})</code> |
| AssignmentExpression | <code>foo = bar</code> |
| AssignmentOperator | |
| AssignmentPattern | <code>function foo(bar = baz) {} </code> |
| AwaitExpression | <code>(async () => { await foo() })()</code> |
| BigIntLiteral | <code>const foo = 9007199254740991n</code> |
| BinaryExpression | <code>foo + bar</code> |
| AssignmentPattern | <code>function foo(bar = baz) {} </code> |
| AwaitExpression | <code>(async () => { await foo() })()</code> |
| BigIntLiteral | |
| BinaryExpression | <code>foo + bar</code> |
| BinaryOperator | |
| BlockStatement | <code>{ console.log(foo) }</code> |
| BreakStatement | <code>for (foo in bar) break</code> |
| CallExpression | <code>foo()</code> |
| CatchClause | <code>try {} catch (error) {}</code> |
| BlockStatement | <code>{ console.log(foo) }</code> |
| BreakStatement | <code>for (foo in bar) break</code> |
| CallExpression | <code>foo()</code> |
| CatchClause | <code>try {} catch (error) {}</code> |
| ChainElement | |
| ChainExpression | <code>foo?.()</code> |
| ChainExpression | <code>foo?.()</code> |
| Class | |
| ClassBody | <code>class Foo {}</code> |
| ClassDeclaration | <code>class Foo {}</code> |
| ClassExpression | <code>(class {})</code> |
| ConditionalExpression | <code>foo ? bar : baz</code> |
| ContinueStatement | <code>while(true) { continue }</code> |
| DebuggerStatement | <code>debugger</code> |
| ClassBody | <code>class Foo {}</code> |
| ClassDeclaration | <code>class Foo {}</code> |
| ClassExpression | <code>(class {})</code> |
| ConditionalExpression | <code>foo ? bar : baz</code> |
| ContinueStatement | <code>while(true) { continue }</code> |
| DebuggerStatement | <code>debugger</code> |
| Declaration | |
| Directive | |
| DoWhileStatement | <code>do {} while (true) {}</code> |
| EmptyStatement | <code>;</code> |
| ExportAllDeclaration | <code>export * from "foo"</code> |
| ExportDefaultDeclaration | <code>export default foo</code> |
| ExportNamedDeclaration | <code>export { foo as bar }</code> |
| ExportSpecifier | <code>export { foo }</code> |
| DoWhileStatement | <code>do {} while (true) {}</code> |
| EmptyStatement | <code>;</code> |
| ExportAllDeclaration | <code>export \* from "foo"</code> |
| ExportDefaultDeclaration | <code>export default foo</code> |
| ExportNamedDeclaration | <code>export { foo as bar }</code> |
| ExportSpecifier | <code>export { foo }</code> |
| Expression | |
| ExpressionStatement | <code>foo</code> |
| ForInStatement | <code>for (foo in bar) {}</code> |
| ForOfStatement | <code>for (foo of bar) {}</code> |
| ForStatement | <code>for (let i = 0; i < 10; i ++) {}</code> |
| ExpressionStatement | <code>foo</code> |
| ForInStatement | <code>for (foo in bar) {}</code> |
| ForOfStatement | <code>for (foo of bar) {}</code> |
| ForStatement | <code>for (let i = 0; i < 10; i ++) {}</code> |
| Function | |
| FunctionBody | |
| FunctionDeclaration | <code>function foo () {}</code> |
| FunctionExpression | <code>(function () {})</code> |
| Identifier | <code>foo</code> |
| IfStatement | <code>if (foo) {}</code> |
| ImportDeclaration | <code>import "foo"</code> |
| ImportDefaultSpecifier | <code>import foo from "bar"</code> |
| ImportExpression | <code>import(foo).then(bar)</code> |
| ImportNamespaceSpecifier | <code>import * as foo from "bar"</code> |
| ImportSpecifier | <code>import { foo } from "bar"</code> |
| LabeledStatement | <code>label: foo</code> |
| Literal | <code>42</code> |
| LogicalExpression | <code>true && false</code> |
| FunctionDeclaration | <code>function foo () {}</code> |
| FunctionExpression | <code>(function () {})</code> |
| Identifier | <code>foo</code> |
| IfStatement | <code>if (foo) {}</code> |
| ImportDeclaration | <code>import "foo"</code> |
| ImportDefaultSpecifier | <code>import foo from "bar"</code> |
| ImportExpression | <code>import(foo).then(bar)</code> |
| ImportNamespaceSpecifier | <code>import \* as foo from "bar"</code> |
| ImportSpecifier | <code>import { foo } from "bar"</code> |
| LabeledStatement | <code>label: foo</code> |
| Literal | <code>42</code> |
| LogicalExpression | <code>true && false</code> |
| LogicalOperator | |
| MemberExpression | <code>foo.bar</code> |
| MetaProperty | <code>function foo () { new.target }</code> |
| MethodDefinition | <code>class Foo { bar() {} }</code> |
| MemberExpression | <code>foo.bar</code> |
| MetaProperty | <code>function foo () { new.target }</code> |
| MethodDefinition | <code>class Foo { bar() {} }</code> |
| ModuleDeclaration | |
| ModuleSpecifier | |
| NewExpression | <code>new Foo()</code> |
| NewExpression | <code>new Foo()</code> |
| Node | |
| ObjectExpression | <code>({})</code> |
| ObjectPattern | <code>function foo ({}) {}</code> |
| ObjectExpression | <code>({})</code> |
| ObjectPattern | <code>function foo ({}) {}</code> |
| Pattern | |
| Position | |
| Program | <code>42</code> |
| Program | <code>42</code> |
| Property | |
| RegExpLiteral | |
| RestElement | <code>function foo (...bar) {}</code> |
| ReturnStatement | <code>function foo () { return bar }</code> |
| SequenceExpression | <code>foo, bar</code> |
| RestElement | <code>function foo (...bar) {}</code> |
| ReturnStatement | <code>function foo () { return bar }</code> |
| SequenceExpression | <code>foo, bar</code> |
| SourceLocation | |

@@ -619,18 +626,18 @@ | SpreadElement | |

| Super | <code>class Foo extends Bar { constructor() { super() } }</code> |
| SwitchCase | <code>switch (foo) { case 'bar': }</code> |
| SwitchStatement | <code>switch(foo) {}</code> |
| TaggedTemplateExpression | <code>css`.foo { color: red; }`</code> |
| TemplateLiteral | <code>css`.foo { color: red; }`</code> |
| ThisExpression | <code>this.foo = 'bar'</code> |
| ThrowStatement | <code>throw new Error("foo")</code> |
| TryStatement | <code>try { foo() } catch (exception) { bar() }</code> |
| UnaryExpression | <code>!foo</code> |
| SwitchCase | <code>switch (foo) { case 'bar': }</code> |
| SwitchStatement | <code>switch(foo) {}</code> |
| TaggedTemplateExpression | <code>css`.foo { color: red; }`</code> |
| TemplateLiteral | <code>css`.foo { color: red; }`</code> |
| ThisExpression | <code>this.foo = 'bar'</code> |
| ThrowStatement | <code>throw new Error("foo")</code> |
| TryStatement | <code>try { foo() } catch (exception) { bar() }</code> |
| UnaryExpression | <code>!foo</code> |
| UnaryOperator | |
| UpdateExpression | <code>foo++</code> |
| UpdateExpression | <code>foo++</code> |
| UpdateOperator | |
| VariableDeclaration | <code>const answer = 42</code> |
| VariableDeclarator | <code>const foo = 'bar'</code> |
| WhileStatement | <code>while (true) {}</code> |
| WithStatement | <code>with (foo) {}</code> |
| YieldExpression | <code>function* foo() { yield bar }</code> |
| VariableDeclaration | <code>const answer = 42</code> |
| VariableDeclarator | <code>const foo = 'bar'</code> |
| WhileStatement | <code>while (true) {}</code> |
| WithStatement | <code>with (foo) {}</code> |
| YieldExpression | <code>function\* foo() { yield bar }</code> |

@@ -646,3 +653,3 @@ ## Optimizations

```js
const { binaryExpressionReduction } = require('abstract-syntax-tree')
const { binaryExpressionReduction } = require("abstract-syntax-tree")
```

@@ -685,5 +692,5 @@

if (true) {
console.log('foo')
console.log("foo")
} else {
console.log('bar')
console.log("bar")
}

@@ -695,3 +702,3 @@ ```

```js
console.log('foo')
console.log("foo")
```

@@ -703,66 +710,66 @@

{
"type": "IfStatement",
"test": {
"type": "Literal",
"value": true
},
"consequent": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"value": "foo"
}
]
"type": "IfStatement",
"test": {
"type": "Literal",
"value": true
},
"consequent": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"value": "foo"
}
}
]
},
"alternate": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"value": "bar"
}
]
]
}
}
]
},
"alternate": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"value": "bar"
}
}
]
]
}
}
}
]
}
}
```

@@ -800,3 +807,3 @@

if (!(foo === bar)) {
console.log('foo')
console.log("foo")
}

@@ -808,4 +815,4 @@ ```

```js
if (foo !== bar) {
console.log('foo')
if (foo !== bar) {
console.log("foo")
}

@@ -894,3 +901,3 @@ ```

```js
const foo = true ? "bar": "baz"
const foo = true ? "bar" : "baz"
```

@@ -971,3 +978,3 @@

```js
const foo = ({ bar: "baz" }).bar
const foo = { bar: "baz" }.bar
```

@@ -974,0 +981,0 @@

@@ -1,5 +0,62 @@

const astring = require('astring')
const astring = require("astring")
module.exports = function generate (tree, options) {
return astring.generate(tree, options)
const generator = {
JSXElement(node, state) {
state.write("<")
this[node.openingElement.type](node.openingElement, state)
if (node.closingElement) {
state.write(">")
for (let i = 0; i < node.children.length; i += 1) {
const child = node.children[i]
this[child.type](child, state)
}
state.write("</")
this[node.closingElement.type](node.closingElement, state)
state.write(">")
} else {
state.write(" />")
}
},
JSXOpeningElement(node, state) {
this[node.name.type](node.name, state)
for (let i = 0; i < node.attributes.length; i += 1) {
const attribute = node.attributes[i]
this[attribute.type](attribute, state)
}
},
JSXClosingElement(node, state) {
this[node.name.type](node.name, state)
},
JSXIdentifier(node, state) {
state.write(node.name)
},
JSXText(node, state) {
state.write(node.value)
},
JSXMemberExpression(node, state) {
this[node.object.type](node.object, state)
state.write(".")
this[node.property.type](node.property, state)
},
JSXAttribute(node, state) {
state.write(" ")
this[node.name.type](node.name, state)
state.write("=")
this[node.value.type](node.value, state)
},
JSXNamespacedName(node, state) {
this[node.namespace.type](node.namespace, state)
state.write(":")
this[node.name.type](node.name, state)
},
JSXExpressionContainer(node, state) {
state.write("{")
this[node.expression.type](node.expression, state)
state.write("}")
},
...astring.GENERATOR,
}
module.exports = function generate(tree, options = {}) {
return astring.generate(tree, { generator, ...options })
}

@@ -1,20 +0,24 @@

const estraverse = require('estraverse')
const find = require('./find')
const equal = require('./equal')
const estraverse = require("./traverse/estraverse")
const find = require("./find")
const equal = require("./equal")
function removeBySelector (tree, selector, options) {
function removeBySelector(tree, selector, options) {
const nodes = find(tree, selector)
removeByNode(tree, leaf => {
for (let i = 0, ilen = nodes.length; i < ilen; i += 1) {
if (equal(leaf, nodes[i])) {
return true
removeByNode(
tree,
(leaf) => {
for (let i = 0, ilen = nodes.length; i < ilen; i += 1) {
if (equal(leaf, nodes[i])) {
return true
}
}
}
return false
}, options)
return false
},
options
)
}
function removeByCallback (tree, callback, options) {
function removeByCallback(tree, callback, options) {
estraverse.replace(tree, {
enter (current, parent) {
enter(current, parent) {
if (callback(current, parent) === null) {

@@ -24,14 +28,14 @@ return this.remove()

},
leave (current, parent) {
leave(current, parent) {
if (isNodeEmpty(current, parent)) {
return this.remove()
}
}
},
})
}
function removeByNode (tree, compare, options) {
function removeByNode(tree, compare, options) {
let count = 0
estraverse.replace(tree, {
enter (current) {
enter(current) {
if (options.first && count === 1) {

@@ -45,29 +49,29 @@ return this.break()

},
leave (current) {
leave(current) {
if (isNodeEmpty(current)) {
return this.remove()
}
}
},
})
}
function isExpressionEmpty (node) {
function isExpressionEmpty(node) {
return node.expression === null
}
function isVariableDeclarationEmpty (node) {
return node.type === 'VariableDeclaration' && node.declarations.length === 0
function isVariableDeclarationEmpty(node) {
return node.type === "VariableDeclaration" && node.declarations.length === 0
}
function isNodeEmpty (node) {
function isNodeEmpty(node) {
return isExpressionEmpty(node) || isVariableDeclarationEmpty(node)
}
module.exports = function remove (tree, handle, options = {}) {
if (typeof handle === 'string') {
module.exports = function remove(tree, handle, options = {}) {
if (typeof handle === "string") {
return removeBySelector(tree, handle, options)
} else if (typeof handle === 'function') {
} else if (typeof handle === "function") {
return removeByCallback(tree, handle, options)
}
removeByNode(tree, node => equal(node, handle), options)
removeByNode(tree, (node) => equal(node, handle), options)
}

@@ -1,4 +0,4 @@

const estraverse = require('estraverse')
const estraverse = require("./traverse/estraverse")
function complexReplace (method, node, parent) {
function complexReplace(method, node, parent) {
const replacement = method(node, parent)

@@ -12,25 +12,31 @@ if (Array.isArray(replacement)) {

} else if (replacement === null) {
parent.body = parent.body.reduce((result, leaf) => {
return result.concat(node === leaf ? null : leaf)
}, []).filter(Boolean)
parent.body = parent.body
.reduce((result, leaf) => {
return result.concat(node === leaf ? null : leaf)
}, [])
.filter(Boolean)
}
}
module.exports = function replace (tree, options) {
const enter = typeof options === 'function' ? options : options.enter
module.exports = function replace(tree, options) {
const enter = typeof options === "function" ? options : options.enter
const leave = options && options.leave
return estraverse.replace(tree, {
enter (node, parent) {
enter(node, parent) {
if (enter) {
const replacement = complexReplace(enter, node, parent)
if (replacement) { return replacement }
if (replacement) {
return replacement
}
}
},
leave (node, parent) {
leave(node, parent) {
if (leave) {
const replacement = complexReplace(leave, node, parent)
if (replacement) { return replacement }
if (replacement) {
return replacement
}
}
}
},
})
}

@@ -1,5 +0,5 @@

const estraverse = require('estraverse')
const estraverse = require("./traverse/estraverse")
module.exports = function traverse (tree, options) {
module.exports = function traverse(tree, options) {
return estraverse.traverse(tree, options)
}

@@ -1,5 +0,5 @@

const estraverse = require('estraverse')
const traverse = require("./traverse")
module.exports = function walk (tree, callback) {
return estraverse.traverse(tree, { enter: callback })
module.exports = function walk(tree, callback) {
return traverse(tree, { enter: callback })
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc