mirror of
https://github.com/fooflington/selfdefined.git
synced 2025-08-01 02:29:05 +00:00
update
This commit is contained in:
87
node_modules/node-source-walk/Readme.md
generated
vendored
Normal file
87
node_modules/node-source-walk/Readme.md
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
### node-source-walk [](https://npmjs.org/package/node-source-walk) [](https://npmjs.org/package/node-source-walk)
|
||||
|
||||
> Synchronously execute a callback on every node of a file's AST and stop walking whenever you see fit.
|
||||
|
||||
`npm install --save node-source-walk`
|
||||
|
||||
### Usage
|
||||
|
||||
```javascript
|
||||
var Walker = require('node-source-walk');
|
||||
|
||||
var walker = new Walker();
|
||||
|
||||
// Assume src is the string contents of myfile.js
|
||||
// or the AST of an outside parse of myfile.js
|
||||
|
||||
walker.walk(src, function(node) {
|
||||
if (node.type === whateverImLookingFor) {
|
||||
// No need to keep traversing since we found what we wanted
|
||||
walker.stopWalking();
|
||||
}
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
By default, Walker will use `babylon` (supporting ES6, JSX, Flow, and all other available babylon plugins) and the `sourceType: module`, but you can change any of the defaults as follows:
|
||||
|
||||
```js
|
||||
var walker = new Walker({
|
||||
sourceType: 'script',
|
||||
// If you don't like experimental plugins
|
||||
plugins: [
|
||||
'jsx',
|
||||
'flow'
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
* The supplied options are passed through to the parser, so you can configure it according to babylon's documentation: https://github.com/babel/babylon
|
||||
|
||||
### Swap out the parser
|
||||
|
||||
If you want to supply your own parser, you can do:
|
||||
|
||||
```js
|
||||
var walker = new Walker({
|
||||
parser: mySweetParser
|
||||
});
|
||||
```
|
||||
|
||||
* The custom parser must have a `.parse` method that takes in a string and returns an object/AST.
|
||||
* All of the other options supplied to the Walker constructor will be passed along as parser options to your chosen parser.
|
||||
|
||||
### Public Members
|
||||
|
||||
`walk(src, cb)`
|
||||
|
||||
* Recursively walks the given `src` from top to bottom
|
||||
* `src`: the contents of a file **OR** its (already parsed) AST
|
||||
* `cb`: a function that is called for every visited node.
|
||||
* The argument passed to `cb` will be the currently visited node.
|
||||
|
||||
`moonwalk(node, cb)`
|
||||
|
||||
* Recursively walks up an AST starting from the given node. This is a traversal that's in the opposite direction of `walk` and `traverse`.
|
||||
* `node`: a valid AST node
|
||||
* `cb`: a function that is called for every node (specifically via visiting the parent(s) of every node recursively).
|
||||
* The argument passed to `cb` will be the currently visited node.
|
||||
|
||||
`stopWalking()`
|
||||
|
||||
* Halts further walking of the AST until another manual call of `walk` or `moonwalk`.
|
||||
* This is super-beneficial when dealing with large source files (or ASTs)
|
||||
|
||||
`traverse(node, cb)`
|
||||
|
||||
* Allows you to traverse an AST node and execute a callback on it
|
||||
* Callback should expect the first argument to be an AST node, similar to `walk`'s callback.
|
||||
|
||||
`parse(src)`
|
||||
|
||||
* Uses the options supplied to Walker to parse the given source code string and return its AST
|
||||
using the configured parser (or babylon by default).
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
130
node_modules/node-source-walk/index.js
generated
vendored
Normal file
130
node_modules/node-source-walk/index.js
generated
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
const parser = require('@babel/parser');
|
||||
|
||||
/**
|
||||
* @param {Object} options - Options to configure parser
|
||||
* @param {Object} options.parser - An object with a parse method that returns an AST
|
||||
*/
|
||||
module.exports = function(options = {}) {
|
||||
this.parser = options.parser || parser;
|
||||
|
||||
if (options.parser) {
|
||||
// We don't want to send that down to the actual parser
|
||||
delete options.parser;
|
||||
}
|
||||
|
||||
this.options = Object.assign({
|
||||
plugins: [
|
||||
'jsx',
|
||||
'flow',
|
||||
'doExpressions',
|
||||
'objectRestSpread',
|
||||
['decorators', {decoratorsBeforeExport: true}],
|
||||
'classProperties',
|
||||
'exportDefaultFrom',
|
||||
'exportNamespaceFrom',
|
||||
'asyncGenerators',
|
||||
'functionBind',
|
||||
'functionSent',
|
||||
'dynamicImport',
|
||||
'optionalChaining',
|
||||
'nullishCoalescingOperator'
|
||||
],
|
||||
allowHashBang: true,
|
||||
sourceType: 'module'
|
||||
}, options);
|
||||
|
||||
// We use global state to stop the recursive traversal of the AST
|
||||
this.shouldStop = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {String} src
|
||||
* @param {Object} [options] - Parser options
|
||||
* @return {Object} The AST of the given src
|
||||
*/
|
||||
module.exports.prototype.parse = function(src, options) {
|
||||
options = options || this.options;
|
||||
|
||||
// Keep around for consumers of parse that supply their own options
|
||||
if (typeof options.allowHashBang === 'undefined') {
|
||||
options.allowHashBang = true;
|
||||
}
|
||||
|
||||
return this.parser.parse(src, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Adapted from substack/node-detective
|
||||
* Executes cb on a non-array AST node
|
||||
*/
|
||||
module.exports.prototype.traverse = function(node, cb) {
|
||||
if (this.shouldStop) { return; }
|
||||
|
||||
if (Array.isArray(node)) {
|
||||
for (let i = 0, l = node.length; i < l; i++) {
|
||||
const x = node[i];
|
||||
if (x !== null) {
|
||||
// Mark that the node has been visited
|
||||
x.parent = node;
|
||||
this.traverse(x, cb);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (node && typeof node === 'object') {
|
||||
cb(node);
|
||||
|
||||
for (let key in node) {
|
||||
// Avoid visited nodes
|
||||
if (key === 'parent' || !node[key]) { continue; }
|
||||
|
||||
node[key].parent = node;
|
||||
this.traverse(node[key], cb);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes the passed callback for every traversed node of
|
||||
* the passed in src's ast
|
||||
*
|
||||
* @param {String|Object} src - The source code or AST to traverse
|
||||
* @param {Function} cb - Called for every node
|
||||
*/
|
||||
module.exports.prototype.walk = function(src, cb) {
|
||||
this.shouldStop = false;
|
||||
|
||||
const ast = typeof src === 'object' ? src : this.parse(src);
|
||||
|
||||
this.traverse(ast, cb);
|
||||
};
|
||||
|
||||
module.exports.prototype.moonwalk = function(node, cb) {
|
||||
this.shouldStop = false;
|
||||
|
||||
if (typeof node !== 'object') {
|
||||
throw new Error('node must be an object');
|
||||
}
|
||||
|
||||
reverseTraverse.call(this, node, cb);
|
||||
};
|
||||
|
||||
function reverseTraverse(node, cb) {
|
||||
if (this.shouldStop || !node.parent) { return; }
|
||||
|
||||
if (node.parent instanceof Array) {
|
||||
for (let i = 0, l = node.parent.length; i < l; i++) {
|
||||
cb(node.parent[i]);
|
||||
}
|
||||
} else {
|
||||
cb(node.parent);
|
||||
}
|
||||
|
||||
reverseTraverse.call(this, node.parent, cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Halts further traversal of the AST
|
||||
*/
|
||||
module.exports.prototype.stopWalking = function() {
|
||||
this.shouldStop = true;
|
||||
};
|
81
node_modules/node-source-walk/package.json
generated
vendored
Normal file
81
node_modules/node-source-walk/package.json
generated
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"node-source-walk@4.2.0",
|
||||
"/Users/tatiana/selfdefined"
|
||||
]
|
||||
],
|
||||
"_from": "node-source-walk@4.2.0",
|
||||
"_id": "node-source-walk@4.2.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-hPs/QMe6zS94f5+jG3kk9E7TNm4P2SulrKiLWMzKszBfNZvL/V6wseHlTd7IvfW0NZWqPtK3+9yYNr+3USGteA==",
|
||||
"_location": "/node-source-walk",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "node-source-walk@4.2.0",
|
||||
"name": "node-source-walk",
|
||||
"escapedName": "node-source-walk",
|
||||
"rawSpec": "4.2.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "4.2.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/detective-amd",
|
||||
"/detective-cjs",
|
||||
"/detective-es6",
|
||||
"/detective-less",
|
||||
"/detective-sass",
|
||||
"/detective-scss",
|
||||
"/detective-typescript",
|
||||
"/get-amd-module-type",
|
||||
"/module-definition",
|
||||
"/precinct"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/node-source-walk/-/node-source-walk-4.2.0.tgz",
|
||||
"_spec": "4.2.0",
|
||||
"_where": "/Users/tatiana/selfdefined",
|
||||
"author": {
|
||||
"name": "Joel Kemp",
|
||||
"email": "joel@mrjoelkemp.com",
|
||||
"url": "http://www.mrjoelkemp.com/"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/mrjoelkemp/node-source-walk/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/parser": "^7.0.0"
|
||||
},
|
||||
"description": "Execute a callback on every node of a source code's AST and stop walking when you see fit",
|
||||
"devDependencies": {
|
||||
"jscs": "~2.4.0",
|
||||
"jscs-preset-mrjoelkemp": "~1.0.0",
|
||||
"mocha": "^5.2.0",
|
||||
"sinon": "^6.1.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.0"
|
||||
},
|
||||
"homepage": "https://github.com/mrjoelkemp/node-source-walk",
|
||||
"keywords": [
|
||||
"ast",
|
||||
"traversal",
|
||||
"acorn",
|
||||
"static analysis",
|
||||
"source code",
|
||||
"walker",
|
||||
"jsx"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "node-source-walk",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mrjoelkemp/node-source-walk.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jscs index.js test && mocha test/test.js"
|
||||
},
|
||||
"version": "4.2.0"
|
||||
}
|
Reference in New Issue
Block a user