Comparing version 1.3.3 to 1.3.4
{ | ||
"name": "flast", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "Flatten JS AST", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
279
README.md
@@ -5,4 +5,36 @@ # flAST - FLat Abstract Syntax Tree | ||
Flatten an Abstract Syntax Tree by placing all of the nodes in a single flat array. | ||
Flatten an Abstract Syntax Tree by placing all the nodes in a single flat array. | ||
For comments and suggestions feel free to open an issue or find me on Twitter - [@ctrl__esc](https://twitter.com/ctrl__esc) | ||
## Table of Contents | ||
* [Installation](#installation) | ||
* [npm](#npm) | ||
* [Clone The Repo](#clone-the-repo) | ||
* [Features](#features) | ||
* [flAST Data Structure](#flast-data-structure) | ||
* [Usage](#usage) | ||
* [flAST](#flast) | ||
* [generateFlatAST Options](#generateflatast-options) | ||
* [generateCode Options](#generatecode-options) | ||
* [Arborist](#arborist) | ||
* [How to Contribute](#how-to-contribute) | ||
*** | ||
## Installation | ||
### npm | ||
```bash | ||
npm install flast | ||
``` | ||
### Clone The Repo | ||
Requires Node 16 or newer. | ||
```bash | ||
git clone git@github.com:PerimeterX/flast.git | ||
cd flast | ||
npm install | ||
``` | ||
*** | ||
## Features | ||
@@ -15,8 +47,5 @@ - Keeps all relations between parent and child nodes. | ||
## Installation | ||
`npm install flast` | ||
### Expected Data Structure | ||
### flAST Data Structure | ||
<details> | ||
<summary>Example of how a flat AST would look like.</summary> | ||
<summary>Example of how a flat AST would look like.</summary> | ||
@@ -27,108 +56,108 @@ Input code: `console.log('flAST');`. | ||
const tree = [ | ||
{ | ||
type: 'program', | ||
start: 0, | ||
end: 21, | ||
range: [0, 21], | ||
body: [ | ||
'<ref to nodeId#2>' | ||
], | ||
sourceType: 'script', | ||
comments: [], | ||
nodeId: 0, | ||
src: "console.log('flAST');", | ||
childNodes: [ | ||
'<ref to nodeId#1>' | ||
], | ||
parentNode: null, | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'ExpressionStatement', | ||
start: 0, | ||
end: 21, | ||
range: [0, 21], | ||
expression: '<ref to nodeId#2>', | ||
nodeId: 1, | ||
src: "console.log('flAST');", | ||
childNodes: [ | ||
'<ref to nodeId#2>' | ||
], | ||
parentNode: '<ref to nodeId#0>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'CallExpression', | ||
start: 0, | ||
end: 20, | ||
range: [0, 20], | ||
callee: '<ref to nodeId#3>', | ||
arguments: [ | ||
'<ref to nodeId#6>' | ||
], | ||
optional: false, | ||
nodeId: 2, | ||
src: "console.log('flAST')", | ||
childNodes: [ | ||
'<ref to nodeId#3>', | ||
'<ref to nodeId#6>' | ||
], | ||
parentNode: '<ref to nodeId#1>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'MemberExpression', | ||
start: 0, | ||
end: 11, | ||
range: [0, 11], | ||
object: '<ref to nodeId#4>', | ||
property: '<ref to nodeId#5>', | ||
computed: false, | ||
optional: false, | ||
nodeId: 3, | ||
src: 'console.log', | ||
childNodes: [ | ||
'<ref to nodeId#4>', | ||
'<ref to nodeId#5>' | ||
], | ||
parentNode: '<ref to nodeId#2>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'Identifier', | ||
start: 0, | ||
end: 7, | ||
range: [0, 7], | ||
name: 'console', | ||
nodeId: 4, | ||
src: 'console', | ||
childNodes: [], | ||
parentNode: '<ref to nodeId#3>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'Identifier', | ||
start: 8, | ||
end: 11, | ||
range: [8, 11], | ||
name: 'log', | ||
nodeId: 5, | ||
src: 'log', | ||
childNodes: [], | ||
parentNode: '<ref to nodeId#3>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'Literal', | ||
start: 12, | ||
end: 19, | ||
range: [12, 19], | ||
value: "flAST", | ||
raw: "'flAST'", | ||
nodeId: 6, | ||
src: "'flAST'", | ||
childNodes: [], | ||
parentNode: '<ref to nodeId#2>', | ||
scope: '<GlobalScope scopeId#0>' | ||
} | ||
{ | ||
type: 'program', | ||
start: 0, | ||
end: 21, | ||
range: [0, 21], | ||
body: [ | ||
'<ref to nodeId#2>' | ||
], | ||
sourceType: 'script', | ||
comments: [], | ||
nodeId: 0, | ||
src: "console.log('flAST');", | ||
childNodes: [ | ||
'<ref to nodeId#1>' | ||
], | ||
parentNode: null, | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'ExpressionStatement', | ||
start: 0, | ||
end: 21, | ||
range: [0, 21], | ||
expression: '<ref to nodeId#2>', | ||
nodeId: 1, | ||
src: "console.log('flAST');", | ||
childNodes: [ | ||
'<ref to nodeId#2>' | ||
], | ||
parentNode: '<ref to nodeId#0>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'CallExpression', | ||
start: 0, | ||
end: 20, | ||
range: [0, 20], | ||
callee: '<ref to nodeId#3>', | ||
arguments: [ | ||
'<ref to nodeId#6>' | ||
], | ||
optional: false, | ||
nodeId: 2, | ||
src: "console.log('flAST')", | ||
childNodes: [ | ||
'<ref to nodeId#3>', | ||
'<ref to nodeId#6>' | ||
], | ||
parentNode: '<ref to nodeId#1>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'MemberExpression', | ||
start: 0, | ||
end: 11, | ||
range: [0, 11], | ||
object: '<ref to nodeId#4>', | ||
property: '<ref to nodeId#5>', | ||
computed: false, | ||
optional: false, | ||
nodeId: 3, | ||
src: 'console.log', | ||
childNodes: [ | ||
'<ref to nodeId#4>', | ||
'<ref to nodeId#5>' | ||
], | ||
parentNode: '<ref to nodeId#2>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'Identifier', | ||
start: 0, | ||
end: 7, | ||
range: [0, 7], | ||
name: 'console', | ||
nodeId: 4, | ||
src: 'console', | ||
childNodes: [], | ||
parentNode: '<ref to nodeId#3>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'Identifier', | ||
start: 8, | ||
end: 11, | ||
range: [8, 11], | ||
name: 'log', | ||
nodeId: 5, | ||
src: 'log', | ||
childNodes: [], | ||
parentNode: '<ref to nodeId#3>', | ||
scope: '<GlobalScope scopeId#0>' | ||
}, | ||
{ | ||
type: 'Literal', | ||
start: 12, | ||
end: 19, | ||
range: [12, 19], | ||
value: "flAST", | ||
raw: "'flAST'", | ||
nodeId: 6, | ||
src: "'flAST'", | ||
childNodes: [], | ||
parentNode: '<ref to nodeId#2>', | ||
scope: '<GlobalScope scopeId#0>' | ||
} | ||
]; | ||
@@ -149,4 +178,4 @@ ``` | ||
const generateFlatASTDefaultOptions = { | ||
detailed: true, // If false, include only original node without any further details | ||
includeSrc: true, // If false, do not include node src. Only available when `detailed` option is true | ||
detailed: true, // If false, include only original node without any further details | ||
includeSrc: true, // If false, do not include node src. Only available when `detailed` option is true | ||
}; | ||
@@ -159,12 +188,12 @@ ``` | ||
const generateCodeDefaultOptions = { | ||
format: { | ||
indent: { | ||
style: ' ', | ||
adjustMultilineComment: true, | ||
}, | ||
quotes: 'auto', | ||
escapeless: true, | ||
compact: false, | ||
}, | ||
comment: true, | ||
format: { | ||
indent: { | ||
style: ' ', | ||
adjustMultilineComment: true, | ||
}, | ||
quotes: 'auto', | ||
escapeless: true, | ||
compact: false, | ||
}, | ||
comment: true, | ||
}; | ||
@@ -195,3 +224,3 @@ ``` | ||
## Contribution | ||
## How to Contribute | ||
To contribute to this project see our [contribution guide](CONTRIBUTING.md) |
@@ -1,2 +0,1 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const estraverse = require('estraverse'); | ||
@@ -3,0 +2,0 @@ const {generateCode, generateFlatAST,} = require(__dirname + '/flast'); |
@@ -1,2 +0,1 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const {Scope} = require('eslint-scope'); | ||
@@ -7,3 +6,3 @@ | ||
* @property {string} type | ||
* @property {ASTNode|null} [alternate] | ||
* @property {ASTNode} [alternate] | ||
* @property {ASTNode} [argument] | ||
@@ -28,4 +27,4 @@ * @property {ASTNode[]} [arguments] | ||
* @property {boolean} [generator] | ||
* @property {ASTNode|null} [id] | ||
* @property {ASTNode|null} [init] | ||
* @property {ASTNode} [id] | ||
* @property {ASTNode} [init] | ||
* @property {boolean} [isMarked] | ||
@@ -45,3 +44,3 @@ * @property {ASTNode} [key] | ||
* @property {string} [parentKey] | ||
* @property {ASTNode|null} [parentNode] | ||
* @property {ASTNode} [parentNode] | ||
* @property {boolean} [prefix] | ||
@@ -56,3 +55,3 @@ * @property {ASTNode} [property] | ||
* @property {ASTNode} [right] | ||
* @property {Scope} [scope] | ||
* @property {ASTScope} [scope] | ||
* @property {string} [scriptHash] | ||
@@ -64,11 +63,21 @@ * @property {boolean} [shorthand] | ||
* @property {string|function} [src] | ||
* @property {ASTNode|null} [superClass] | ||
* @property {ASTNode} [superClass] | ||
* @property {boolean} [tail] | ||
* @property {ASTNode|null} [test] | ||
* @property {ASTNode|null} [update] | ||
* @property {ASTNode|string|number|boolean|null} [value] | ||
* @property {ASTNode} [test] | ||
* @property {ASTNode} [update] | ||
* @property {ASTNode|string|number|boolean} [value] | ||
*/ | ||
class ASTNode {} | ||
/** | ||
* @typedef ASTScope | ||
* @extends Scope | ||
* @property {number} scopeId | ||
* @property {ASTScope[]} childScopes | ||
*/ | ||
class ASTScope extends Scope {} | ||
module.exports = { | ||
ASTNode, | ||
ASTScope, | ||
}; |
@@ -29,2 +29,27 @@ const assert = require('node:assert'); | ||
enabled: true, | ||
name: 'Verify Available Imports', | ||
description: 'Verify the expected functions and classes can be imported.', | ||
run() { | ||
const {resolve} = require('node:path'); | ||
const availableImports = [ | ||
'Arborist', | ||
'ASTNode', | ||
'ASTScope', | ||
'estraverse', | ||
'generateCode', | ||
'generateFlatAST', | ||
'parseCode', | ||
]; | ||
function tryImporting(importName) { | ||
const {[importName]: tempImport} = require(importSource); | ||
return tempImport; | ||
} | ||
const importSource = resolve(__dirname + '/../src/index'); | ||
for (const importName of availableImports) { | ||
assert.ok(tryImporting(importName), `Failed to import "${importName}" from ${importSource}`); | ||
} | ||
}, | ||
}, | ||
{ | ||
enabled: true, | ||
name: 'Number of nodes', | ||
@@ -31,0 +56,0 @@ description: 'Verify the code breakdown generates the expected nodes by checking the number of nodes for each expected type.', |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
43910
849
220
0
9