New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ast-grep

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-grep - npm Package Compare versions

Comparing version 0.0.0-development to 0.0.0

dist/bin/ast-grep.js

88

dist/index.js

@@ -1,3 +0,87 @@

"use strict";
'use strict';
process.stdin.pipe(process.stdout);
Object.defineProperty(exports, "__esModule", {
value: true
});
require('./polyfills');
var _traverse = require('@babel/traverse');
var _traverse2 = _interopRequireDefault(_traverse);
var _omitDeepLodash = require('omit-deep-lodash');
var _omitDeepLodash2 = _interopRequireDefault(_omitDeepLodash);
var _deepEqual = require('deep-equal');
var _deepEqual2 = _interopRequireDefault(_deepEqual);
var _parse = require('./parse');
var _parse2 = _interopRequireDefault(_parse);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = (text, { pattern, anonymous }) => {
const patternAsts = [].concat(getMeaningfulNode((0, _parse2.default)(pattern)));
const ast = (0, _parse2.default)(text);
return patternAsts.flatMap(patternAst => matchAsts(patternAst, ast, { anonymous })).map(match => {
const code = readLineToStart(text, match.start) + text.substring(match.start, match.end) + readLineToEnd(text, match.end);
return { text: code, node: match };
});
};
// import preprocess from './preprocess';
const getMeaningfulNode = ast => {
switch (ast.type) {
case 'File':
return getMeaningfulNode(ast.program);
case 'Program':
return ast.body.map(getMeaningfulNode);
case 'ExpressionStatement':
return getMeaningfulNode(ast.expression);
default:
return ast;
}
};
const omitKeysDefault = ['start', 'end', 'loc'];
const matchAsts = (smaller, bigger, { anonymous }) => {
const omitKeys = anonymous ? [...omitKeysDefault, 'name'] : omitKeysDefault;
const matches = [];
smaller = (0, _omitDeepLodash2.default)(smaller, ...omitKeys);
(0, _traverse2.default)(bigger, {
enter(path) {
if ((0, _deepEqual2.default)((0, _omitDeepLodash2.default)(path.node, ...omitKeys), smaller)) {
matches.push(path.node);
}
}
});
return matches;
};
const readLineToStart = (text, index) => {
const range = text.substring(0, index);
const match = /\r?\n(.*)$/.exec(range);
if (!match) {
return '';
}
return match[1];
};
const readLineToEnd = (text, index) => {
const range = text.substring(index);
const match = /^(.*)\r?\n/.exec(range);
if (!match) {
return '';
}
return match[1];
};
{
"name": "ast-grep",
"version": "0.0.0-development",
"version": "0.0.0",
"description": "Like grep, but more powerful than you can possibly imagine.",

@@ -8,5 +8,19 @@ "repository": "azz/ast-grep",

"main": "./dist",
"bin": "./dist/bin/ast-grep.js",
"license": "MIT",
"files": ["dist"],
"dependencies": {},
"files": [
"dist"
],
"dependencies": {
"@babel/code-frame": "7.0.0-beta.37",
"@babel/traverse": "^7.0.0-beta.37",
"babylon": "7.0.0-beta.37",
"core-js": "^2.5.3",
"deep-equal": "^1.0.1",
"get-stream": "^3.0.0",
"globby": "^7.1.1",
"mem": "^3.0.0",
"mri": "^1.1.0",
"omit-deep-lodash": "^1.0.0"
},
"scripts": {

@@ -16,3 +30,4 @@ "prepublishOnly": "yarn build",

"test": "jest",
"lint": "eslint . --ignore-path=.gitignore",
"test:generate": "babel-node test/generate",
"lint": "eslint .",
"semantic-release": "semantic-release",

@@ -24,5 +39,8 @@ "precommit": "pretty-quick --staged"

"babel-core": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"common-tags": "^1.7.2",
"eslint": "^4.15.0",
"eslint-config-prettier": "^2.9.0",
"eslint-config-xo": "^0.19.0",
"eslint-plugin-jest": "^21.5.0",

@@ -34,3 +52,3 @@ "eslint-plugin-prettier": "^2.4.0",

"pretty-quick": "^1.2.0",
"semantic-release": "^11.0.2"
"semantic-release": "^12.2.2"
},

@@ -43,2 +61,3 @@ "prettier": {

"extends": [
"xo/esnext",
"eslint:recommended",

@@ -54,7 +73,27 @@ "plugin:jest/recommended",

"node": true
},
"rules": {
"no-console": "off"
}
},
"babel": {
"presets": [["env", { "targets": { "node": "6.10" } }]]
"presets": [
[
"env",
{
"targets": {
"node": "6.10"
}
}
]
],
"plugins": [
"transform-object-rest-spread"
]
},
"jest": {
"snapshotSerializers": [
"<rootDir>/test/raw-serializer.js"
]
}
}

@@ -11,2 +11,4 @@ # `ast-grep`

Search your JavaScript files for patterns based on AST shape, rather than substrings or regular expressions.
## Install

@@ -17,3 +19,3 @@

```shellsession
yarn add ast-grep
yarn global add ast-grep
```

@@ -24,5 +26,44 @@

```shellsession
npm install --save ast-grep
npm install --global --save ast-grep
```
## Usage
On standard in:
```shellsession
$ echo 'code();' | ast-grep 'code'
code();
$ ast-grep 'foo()' < file.js
foo();
```
On a set of files:
```shellsession
$ ast-grep 'yield* foo();' '**/*.js'
```
## Flags
### `--anonymous`
**Alias**: `-a`
Ignore names, this includes identifiers, types, etc.
## Examples
### Find all no-arg function calls:
```shellsession
$ echo -e 'foo();\nbar();' | ast-grep 'fn()' -a
foo();
bar();
```
## FAQ
### Q. But @azz, `grep` stands for Global Regular Expression Print, this tool doesn't use Regular Expressions!
**A.** I know, but `gastp` doesn't sound great.
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