Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
s-expression match patterns for Mozilla Parser AST
var jsstana = require("jsstana");
var esprima = require("esprima");
var contents = // ...
var syntax = esprima.parse(contents);
jsstana.traverse(syntax, function (node) {
var m = jsstana.match("(call alert ?argument)", node);
if (m) {
console.log("alert called with argument", m.argument);
}
});
The jsgrep example utility is provided
# find assertArguments calls with 4 arguments
% jsgrep '(call ?.assertArguments ? ? ? ?)' lib
matchers/literal.js:25: this.assertArguments("true/false/null/infinity/nan/undefined", 0, arguments, 1);
matchers/literal.js:111: this.assertArguments("literal", 1, arguments, 1);
matchers/member.js:18: that.assertArguments("member/property/subscript", 2, arguments, 1);
matchers/operator.js:63: that.assertArguments(ratorName, 3, arguments, 3);
matchers/operator.js:98: that.assertArguments("unary", 2, arguments, 1);
matchers/operator.js:128: that.assertArguments("update/postfix/prefix", 2, arguments, 1);
matchers/simple.js:7: this.assertArguments(rator, 1, arguments, 3);
Matches when pattern
doesn't match.
Matches if any pattern matches, returns first match.
Matches if all pattern matches, returns combinedMatch
Matches CallExpression
.
(call fun arg1 arg2)
matches exact amount of arguments,
for arbitrary arguments use
(call fun . ?)
or similar dotted list syntax.
Matches Identifier
.
Matches VariableDeclarator
.
Matches undefined
node.
Matches Literal
.
There are some additional version::
(string value)
- string values(number value)
- number values(bool value)
- boolean values(regexp value)
- regular expressions(true)
- matches true
(false)
- matches false
(null)
- matches null
(infinity)
- matches Infinity
(nan)
- matches NaN
(undefined)
- matches undefined
Matches ReturnStatement
.
Matches expression statement, ExpressionStatement
.
Matches ThrowStatement
.
Matches MemberExpression
.
foo.bar
.foo[bar]
.Helper macro for nested variable access.
(lookup foo.bar.baz)
is equivalent to (property (property foo bar) baz)
.
The atom foo.bar.baz
works as (lookup foo.bar.baz)
.
Matches BinaryExpression
.
Also shorthand syntax is supported, (+ a b)
is the same as (binary + a b)
.
Matches UnaryExpression
.
Also shorthand version works for !
and ~
: (~ ?foo)
is the same as (unary ~ ?foo)
.
Matches UpdateExpression
.
You might want to use postfix
and prefix
though.
Matches AssignmentExpression
.
Matches ConditionalExpression
.
Match node
against pattern
.
If pattern matches returns an object with match captures.
Otherwise returns undefined
.
This function is autocurried ie. when one argument is passed, returns function node -> matchresult
.
This function is also memoized on the pattern, ie each pattern is compiled only once.
Create matcher. With one argument, matcher(pattern) === match(pattern)
.
With additional arguments, you can add $0
, $1
... additional anonymous matchers.
var matcher = jsstana.createMatcher("(expr (= a $0))", function (node) {
return node.type === "ObjectExpression" && node.properties.length === 0 ? {} : undefined;
});
Create new jsstana context. You can add new operations to this one.
var ctx = new jsstana();
ctx.addMatchers("empty-object", function () {
this.assertArguments("empty-object", 0, arguments);
return function (node) {
return node.type === "ObjectExpression" && node.properties.length === 0 ? {} : undefined;
};
});
ctx.match("(empty-object", node);
You may compile submatchers with this.matcher(sexpr)
and combine their results with this.combineMatches
.
this.assertArguments
checks argument (rator) count, to help validate pattern grammar.
In lieu of a formal styleguide, take care to maintain the existing coding style.
istanbul cover grunt simplemocha
to run tests with coverage with istanbul.literal-
prefix (eg plain string
now)(+ a b)
Copyright Oleg Grenrus 2013
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Oleg Grenrus nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
FAQs
s-expression match patterns for Mozilla Parser AST
The npm package jsstana receives a total of 1,690 weekly downloads. As such, jsstana popularity was classified as popular.
We found that jsstana demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.