{"id":16338990,"url":"https://github.com/phadej/jsstana","last_synced_at":"2025-06-25T04:43:56.394Z","repository":{"id":11368884,"uuid":"13805043","full_name":"phadej/jsstana","owner":"phadej","description":"s-expression match patterns for Mozilla Parser AST","archived":false,"fork":false,"pushed_at":"2019-03-04T10:08:30.000Z","size":808,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-13T20:34:37.208Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phadej.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-23T14:30:32.000Z","updated_at":"2019-03-04T10:07:34.000Z","dependencies_parsed_at":"2022-09-05T21:01:32.063Z","dependency_job_id":null,"html_url":"https://github.com/phadej/jsstana","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/phadej/jsstana","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phadej%2Fjsstana","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phadej%2Fjsstana/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phadej%2Fjsstana/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phadej%2Fjsstana/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phadej","download_url":"https://codeload.github.com/phadej/jsstana/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phadej%2Fjsstana/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259811890,"owners_count":22915162,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T23:53:14.949Z","updated_at":"2025-06-25T04:43:56.366Z","avatar_url":"https://github.com/phadej.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsstana\n\n\u003e s-expression match patterns for [Mozilla Parser AST](https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API)\n\n[jsstana](oleg.fi/jsstana) will help you to find exactly the code snippet you are searching for.\nIt's much more precise than using grep.\n\n[![Build Status](https://secure.travis-ci.org/phadej/jsstana.svg?branch=master)](http://travis-ci.org/phadej/jsstana)\n[![NPM version](https://badge.fury.io/js/jsstana.svg)](http://badge.fury.io/js/jsstana)\n\n## Synopsis\n\n```javascript\nvar jsstana = require(\"jsstana\");\nvar esprima = require(\"esprima\");\n\nvar contents = // ...\nvar syntax = esprima.parse(contents);\n\njsstana.traverse(syntax, function (node) {\n  var m = jsstana.match(\"(call alert ?argument)\", node);\n  if (m) {\n    console.log(\"alert called with argument\", m.argument);\n  }\n});\n```\n\n## jsgrep\n\nThe jsgrep example utility is provided\n\n```bash\n# find assertArguments calls with 4 arguments\n% jsgrep '(call ?.assertArguments ? ? ? ?)' lib\nmatchers/literal.js:25:   this.assertArguments(\"true/false/null/infinity/nan/undefined\", 0, arguments, 1);\nmatchers/literal.js:111:   this.assertArguments(\"literal\", 1, arguments, 1);\nmatchers/member.js:18:   that.assertArguments(\"member/property/subscript\", 2, arguments, 1);\nmatchers/operator.js:63:   that.assertArguments(ratorName, 3, arguments, 3);\nmatchers/operator.js:98:   that.assertArguments(\"unary\", 2, arguments, 1);\nmatchers/operator.js:128:   that.assertArguments(\"update/postfix/prefix\", 2, arguments, 1);\nmatchers/simple.js:7:   this.assertArguments(rator, 1, arguments, 3);\n```\n\n## Pattern syntax\n\n#### (?name pattern)\n\nGives pattern a name, so matching node is also captured.\n\n```js\njsstana.match(\"(binary ?op ?lhs (?rhs (or (literal) (ident))))\");\n```\n\n#### (not pattern)\n\nMatches when `pattern` doesn't match.\n\n#### (or pattern1 pattern2...)\n\nMatches if any pattern matches, returns first match.\n\n#### (and pattern1 pattern2...)\n\nMatches if all pattern matches, returns combined match.\n\n### (nor pattern) and (nand pattern)\n\nAre the same as `(not (or pattern))` and `(not (and pattern))` respectively.\n\n#### (call callee arg0...argn)\n\nMatches `CallExpression`.\n\n`(call fun arg1 arg2)` matches exact amount of arguments,\nfor arbitrary arguments use\n`(call fun ??params)`\n\n#### (new class arg0...argn)\n\nMatches `NewExpression`.\n\n#### (ident name)\n\nMatches `Identifier`.\n\n#### (var name init)\n\nMatches `VariableDeclarator`.\n\n#### (null-node)\n\nMatches `undefined` node.\n\n#### (literal value)\n\nMatches `Literal`.\n\nThere are some additional version::\n\n- `(string value)` - string values\n- `(number value)` - number values\n- `(bool value)` - boolean values\n- `(regexp value)` - regular expressions\n- `(true)` - matches `true`\n- `(false)` - matches `false`\n- `(null)` - matches `null`\n- `(infinity)` - matches `Infinity`\n- `(nan)` - matches `NaN`, also `(NaN)` is supported\n- `(undefined)` - matches `undefined`, also `(Infinity)` is supported\n\n#### (this)\n\nMatches `ThisExpression`.\n\n#### (return value)\n\nMatches `ReturnStatement`.\n\n#### (expression expr)\n\nMatches expression statement, `ExpressionStatement`.\n\n#### (throw ex)\n\nMatches `ThrowStatement`.\n\n#### (break)\n\nMatches `BreakStatement`.\n\n#### (continue)\n\nMatches `ContinueStatement`.\n\n#### (member object property)\n\nMatches `MemberExpression`.\n\n- (property object property) matches non computed expressions, i.e. `foo.bar`.\n- (subscript object property) matches computed expressions i.e. `foo[bar]`.\n\n#### (lookup var.sub.name)\n\nHelper macro for nested variable access.\n`(lookup foo.bar.baz)` is equivalent to `(property (property foo bar) baz)`.\n\nThe atom `foo.bar.baz` works as `(lookup foo.bar.baz)`.\n\n#### (binary op lhs rhs)\n\nMatches `BinaryExpression`.\n\nAlso shorthand syntax is supported, `(+ a b)` is the same as `(binary + a b)`.\n\n#### (logical op lhs rhs)\n\nMatches `LogicalExpression`. ie. `\u0026\u0026` and `||` operators.\n\n#### (unary op value)\n\nMatches `UnaryExpression`.\n\nAlso shorthand version works for `!` and `~`: `(~ ?foo)` is the same as `(unary ~ ?foo)`.\n\n#### (update op value)\n\nMatches `UpdateExpression`.\n\nYou might want to use `postfix` and `prefix` though.\n\n#### (assign op var value)\n\nMatches `AssignmentExpression`.\n\n#### (ternary test con alt)\n\nMatches `ConditionalExpression`.\n\n#### (fn-expr)\n\nMatches `FunctionExpression`.\n\n#### (object)\n\nMatches `ObjectExpression`.\n\n## API\n\n### match(pattern, node)\n\nMatch `node` against `pattern`.\nIf pattern matches returns an object with match captures.\nOtherwise returns `undefined`.\n\nThis function is autocurried ie. when one argument is passed, returns function `node -\u003e matchresult`.\n\nThis function is also memoized on the pattern, ie each pattern is compiled only once.\n\n### createMatcher(pattern, [posMatcher])\n\nCreate matcher. With one argument, `matcher(pattern) === match(pattern)`.\nWith additional arguments, you can add `$0`, `$1`... additional anonymous matchers.\n\n```js\nvar matcher = jsstana.createMatcher(\"(expr (= a $0))\", function (node) {\n  return node.type === \"ObjectExpression\" \u0026\u0026 node.properties.length === 0 ? {} : undefined;\n});\n```\n\n### eslintRule(pattern, f)\n\n### new jsstana()\n\nCreate new jsstana context. You can add new operations to this one.\n\n```js\nvar ctx = new jsstana();\nctx.addMatchers(\"empty-object\", function () {\n  this.assertArguments(\"empty-object\", 0, arguments);\n  return function (node) {\n    return node.type === \"ObjectExpression\" \u0026\u0026 node.properties.length === 0 ? {} : undefined;\n  };\n});\nctx.match(\"(empty-object\", node);\n```\n\nYou may compile submatchers with `this.matcher(sexpr)` and combine their results with `this.combineMatches`.\n`this.assertArguments` checks argument (rator) count, to help validate pattern grammar.\n\n## Contributing\n\nIn lieu of a formal styleguide, take care to maintain the existing coding style.\n\n- Add unit tests for any new or changed functionality.\n- Lint and test your code using [Grunt](http://gruntjs.com/).\n- Use `istanbul cover grunt simplemocha` to run tests with coverage with [istanbul](http://gotwarlost.github.io/istanbul/).\n- Create a pull request\n\n## Release History\n\n- 0.1.6 \u0026mdash; *2015-08-28* \u0026mdash; Depdenency update\n- 0.1.5 \u0026mdash; *2014-02-25* \u0026mdash; Dependency bump\n- 0.1.4 \u0026mdash; *2014-11-09* \u0026ndash; jsstana.eslintRule\n    - `(object)` matcher\n- 0.1.3 Multiple multi-param matching groups in `(call)`\n\n    Example: `(call ? ?? 0 ??)` checks whether function has zero as any argument.\n\n- 0.1.2 Fix bug, identifier could start with underscore: `_`\n- 0.1.1 New (call) syntax\n    - `(call ?fun ?param ?params ?last-one)`\n- 0.0.22 dependency updates\n- 0.0.21 Use commander\n- 0.0.20 dependency update\n- 0.0.19 dependency updates\n- 0.0.18 null checks\n    - Also updated dependencies\n- 0.0.17 this, break \u0026amp; continue\n    - Added forementioned matchers\n- 0.0.16 Updates\n    - Dependencies updated\n    - `fn-expr` matches function expressions\n- 0.0.15 Updates\n    - Dependencies updated\n        - Introduce eslint\n        - Fix logical expressions: `\u0026\u0026` and `||`\n- 0.0.14 Better cli experience\n    - Strip shebang by default\n    - Truncate long output lines\n    - Fancier colorize of jsgrep output\n    - Catch parse errors and unexisting files\n- 0.0.13 nand, nor and ?\n    - node capturing\n    - nand and nor\n    - instanceof, typeof, delete and void operators\n- 0.0.12 Code reogranization\n- 0.0.11 User-provided patterns\n    - fixed installing on Windows\n    - assignment pattern\n    - anonymous matchers\n- 0.0.10 ident pattern\n- 0.0.9 Boolean patterns\n- 0.0.8 Even more rands\n    - unary and update expressions\n    - drop `literal-` prefix (eg plain `string` now)\n    - shorthand binary op syntax `(+ a b)`\n- shorthand lookup syntax\n- 0.0.7 jsgrep, third try\n- 0.0.6 jsgrep, second try\n- 0.0.5 jsgrep\n    - also new expression\n- 0.0.4 Binary and throw\n- 0.0.3 More rands\n    - call dotted syntax\n    - literals\n    - expr - expression statement\n    - use grunt-literate to generate README.md\n- 0.0.2 Dev setup\n- 0.0.1 Preview release\n\nCopyright Oleg Grenrus 2013\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n    * Redistributions of source code must retain the above copyright\n      notice, this list of conditions and the following disclaimer.\n\n    * Redistributions in binary form must reproduce the above\n      copyright notice, this list of conditions and the following\n      disclaimer in the documentation and/or other materials provided\n      with the distribution.\n\n    * Neither the name of Oleg Grenrus nor the names of other\n      contributors may be used to endorse or promote products derived\n      from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphadej%2Fjsstana","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphadej%2Fjsstana","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphadej%2Fjsstana/lists"}