Comparing version 0.0.7 to 0.0.8
@@ -75,3 +75,4 @@ /** | ||
return function (node) { | ||
return node.type === "Identifier" ? {} : undefined; | ||
assert(node.type === "Identifier", "identifier matcher expects identifier nodes"); | ||
return {}; | ||
}; | ||
@@ -81,11 +82,11 @@ } else if (sexpr[0] === "?") { | ||
return function (node) { | ||
if (node.type === "Identifier") { | ||
var res = {}; | ||
res[sexpr] = node.name; | ||
return res; | ||
} | ||
assert(node.type === "Identifier", "identifier matcher expects identifier nodes"); | ||
var res = {}; | ||
res[sexpr] = node.name; | ||
return res; | ||
}; | ||
} else { | ||
return function (node) { | ||
return node.type === "Identifier" && node.name === sexpr ? {} : undefined; | ||
assert(node.type === "Identifier", "identifier matcher expects identifier nodes"); | ||
return node.name === sexpr ? {} : undefined; | ||
}; | ||
@@ -95,5 +96,45 @@ } | ||
function operatorMatcher(operator, validOperators) { | ||
if (operator === "?") { | ||
return function () { return {}; }; | ||
} else if (operator[0] === "?") { | ||
operator = operator.substr(1); | ||
return function(op) { | ||
var res = {}; | ||
res[operator] = op; | ||
return res; | ||
}; | ||
} else { | ||
assert(_.contains(validOperators, operator), operator + " is not valid operator"); | ||
return function (op) { | ||
return op === operator ? {} : undefined; | ||
}; | ||
} | ||
} | ||
var validBinaryOperators = [ | ||
"+", "-", "*", "/", "%", | ||
"<<", ">>", ">>>", | ||
"<", ">", "<=", ">=", | ||
"==", "!=", "===", "!==", | ||
"&&", "||", | ||
"&", "|", "^", | ||
]; | ||
var validUnaryOperators = [ | ||
"!", "~", "+", "-", | ||
]; | ||
var validUpdateOperators = [ | ||
"++", "--", | ||
]; | ||
function matcher(sexpr) { | ||
if (_.isString(sexpr)) { | ||
if (sexpr === "?") { | ||
if (sexpr.indexOf(".") !== -1) { | ||
sexpr = sexpr.split(".").reduce(function (prev, next) { | ||
return ["property", prev, next]; | ||
}); | ||
return matcher(sexpr); | ||
} else if (sexpr === "?") { | ||
return function () { | ||
@@ -134,3 +175,3 @@ return {}; | ||
assert(_.isArray(sexpr), "expression should be an array -- " + sexpr); | ||
assert(_.isArray(sexpr), "expression should be a number, a string or an array -- " + sexpr); | ||
@@ -147,2 +188,6 @@ var rator = _.first(sexpr); | ||
"binary": binaryMatcher, | ||
"unary": unaryMatcher, | ||
"update": updateMatcher.bind(undefined, undefined), | ||
"prefix": updateMatcher.bind(undefined, true), | ||
"postfix": updateMatcher.bind(undefined, false), | ||
"member": memberMatcher.bind(undefined, undefined), | ||
@@ -155,6 +200,6 @@ "property": memberMatcher.bind(undefined, false), | ||
"literal": literalMatcher.bind(undefined, "any"), | ||
"literal-string": literalMatcher.bind(undefined, "string"), | ||
"literal-number": literalMatcher.bind(undefined, "number"), | ||
"literal-bool": literalMatcher.bind(undefined, "bool"), | ||
"literal-regexp": literalMatcher.bind(undefined, "regexp"), | ||
"string": literalMatcher.bind(undefined, "string"), | ||
"number": literalMatcher.bind(undefined, "number"), | ||
"bool": literalMatcher.bind(undefined, "bool"), | ||
"regexp": literalMatcher.bind(undefined, "regexp"), | ||
"null": literalBuiltinMatcher.bind(undefined, "null"), | ||
@@ -169,2 +214,15 @@ "true": literalBuiltinMatcher.bind(undefined, "true"), | ||
var binaryOperators = validBinaryOperators; | ||
var unaryOperators = _.difference(validUnaryOperators, validBinaryOperators); | ||
// "TODO: , == SequenceExpression" | ||
_.each(binaryOperators, function (binop) { | ||
matchers[binop] = binaryMatcher.bind(undefined, binop); | ||
}); | ||
_.each(unaryOperators, function (unop) { | ||
matchers[unop] = unaryMatcher.bind(undefined, unop); | ||
}); | ||
if (_.has(matchers, rator)) { | ||
@@ -182,2 +240,8 @@ return matchers[rator].apply(undefined, rands); | ||
function combineMatches() { | ||
var args = _.toArray(arguments); | ||
if (args.some(_.isUndefined)) { return undefined; } | ||
return _.extend.apply(undefined, args); | ||
} | ||
/** | ||
@@ -213,5 +277,3 @@ ### Pattern syntax | ||
var valueM = valueMatcher(node.argument); | ||
return valueM; | ||
return valueMatcher(node.argument); | ||
}; | ||
@@ -226,6 +288,6 @@ } | ||
There are some additional version: | ||
- `(literal-string value)` - string values | ||
- `(literal-number value)` - number values | ||
- `(literal-bool value)` - boolean values | ||
- `(literal-regexp value)` - regular expressions | ||
- `(string value)` - string values | ||
- `(number value)` - number values | ||
- `(bool value)` - boolean values | ||
- `(regexp value)` - regular expressions | ||
- `(true)` - matches `true` | ||
@@ -263,3 +325,3 @@ - `(false)` - matches `false` | ||
}; | ||
} else if (type === "undefined") { | ||
} else /* if (type === "undefined") */ { | ||
return function (node) { | ||
@@ -362,5 +424,3 @@ return node.type === "Identifier" && node.name === "undefined" ? {} : undefined; | ||
if (idM !== undefined && initM !== undefined) { | ||
return _.extend(idM, initM); | ||
} | ||
return combineMatches(idM, initM); | ||
}; | ||
@@ -452,2 +512,4 @@ } | ||
Matches `BinaryExpression`. | ||
Also shorthand syntax is supported, `(+ a b)` is the same as `(binary + a b)`. | ||
*/ | ||
@@ -463,18 +525,3 @@ function binaryMatcher(operator, lhs, rhs) { | ||
var operatorMatcher; | ||
if (operator === "?") { | ||
operatorMatcher = function () { return {}; }; | ||
} else if (operator[0] === "?") { | ||
operator = operator.substr(1); | ||
operatorMatcher = function(op) { | ||
var res = {}; | ||
res[operator] = op; | ||
return res; | ||
}; | ||
} else { | ||
operatorMatcher = function (op) { | ||
return op === operator ? {} : undefined; | ||
}; | ||
} | ||
var opMatcher = operatorMatcher(operator, validBinaryOperators); | ||
var lhsMatcher = matcher(lhs); | ||
@@ -486,9 +533,7 @@ var rhsMatcher = matcher(rhs); | ||
var operatorM = operatorMatcher(node.operator); | ||
var opM = opMatcher(node.operator); | ||
var lhsM = lhsMatcher(node.left); | ||
var rhsM = rhsMatcher(node.right); | ||
if (operatorM !== undefined && lhsM !== undefined && rhsM !== undefined) { | ||
return _.extend(operatorM, lhsM, rhsM); | ||
} | ||
return combineMatches(opM, lhsM, rhsM); | ||
}; | ||
@@ -498,2 +543,59 @@ } | ||
/** | ||
#### (unary op value) | ||
Matches `UnaryExpression`. | ||
Also shorthand version works for `!` and `~`: `(~ ?foo)` is the same as `(unary ~ ?foo)`. | ||
*/ | ||
function unaryMatcher(operator, value) { | ||
assertArguments("unary", 2, arguments, 1); | ||
operator = operator || "?"; | ||
value = value || "?"; | ||
assert(_.isString(operator), "binary operator should be string expr"); | ||
var opMatcher = operatorMatcher(operator, validUnaryOperators); | ||
var valueMatcher = matcher(value); | ||
return function (node) { | ||
if (node.type !== "UnaryExpression") { return undefined; } | ||
var opM = opMatcher(node.operator); | ||
var valueM = valueMatcher(node.argument); | ||
return combineMatches(opM, valueM); | ||
}; | ||
} | ||
/** | ||
#### (update op value) | ||
Matches `UpdateExpression`. | ||
You might want to use `postfix` and `prefix` though. | ||
*/ | ||
function updateMatcher(prefix, operator, value) { | ||
assertArguments("update/postfix/prefix", 2, arguments, 1); | ||
operator = operator || "?"; | ||
value = value || "?"; | ||
assert(_.isString(operator), "binary operator should be string expr"); | ||
var opMatcher = operatorMatcher(operator, validUpdateOperators); | ||
var valueMatcher = matcher(value); | ||
return function (node) { | ||
if (node.type !== "UpdateExpression") { return undefined; } | ||
if (prefix !== undefined && node.prefix !== prefix) { return undefined; } | ||
var opM = opMatcher(node.operator); | ||
var valueM = valueMatcher(node.argument); | ||
return combineMatches(opM, valueM); | ||
}; | ||
} | ||
/** | ||
#### (member object property) | ||
@@ -521,5 +623,3 @@ | ||
if (objectM !== undefined && propertyM !== undefined) { | ||
return _.extend(objectM, propertyM); | ||
} | ||
return combineMatches(objectM, propertyM); | ||
}; | ||
@@ -533,2 +633,4 @@ } | ||
`(lookup foo.bar.baz)` is equivalent to `(property (property foo bar) baz)`. | ||
The `foo.bar.baz` will work as `(lookup foo.bar.baz)` as well. | ||
*/ | ||
@@ -587,5 +689,3 @@ function lookupMatcher(varname) { | ||
if (testM !== undefined && consequentM !== undefined && alternateM !== undefined) { | ||
return _.extend(testM, consequentM, alternateM); | ||
} | ||
return combineMatches(testM, consequentM, alternateM); | ||
}; | ||
@@ -637,4 +737,12 @@ } | ||
Use `grunt mochacov` to generate coverage report with blanket, | ||
or `istanbul cover grunt simplemocha` to do coverage with istanbul. | ||
## Release History | ||
- 0.0.8 Even more rands | ||
- unary and update expressions | ||
- drop `literal-` prefix (eg plain `string` now) | ||
- shorthand binary op syntax `(+ a b)` | ||
- shorthand lookup syntax | ||
- 0.0.7 jsgrep, third try | ||
@@ -641,0 +749,0 @@ - 0.0.6 jsgrep, second try |
@@ -14,3 +14,3 @@ "use string"; | ||
p.seq(lexemeP("("), p.repeat(function () { return sexprP; }), lexemeP(")")).onMatch(function (arr) { return arr[1]; }), | ||
lexemeP(p.regex(/[a-zA-Z\?\.\-\/*+<>=!][a-zA-Z0-9_\?\.\-\/*+<>+!]*/)).onMatch(_.first), | ||
lexemeP(p.regex(/[a-zA-Z\?\.\-\/*+<>=!%,~][a-zA-Z0-9_\?\.\-\/*+<>+!%,~]*/)).onMatch(_.first), | ||
lexemeP(p.regex(/[0-9]+/)).onMatch(function (m) { return parseInt(_.first(m), 10); } ) | ||
@@ -17,0 +17,0 @@ ); |
{ | ||
"name": "jsstana", | ||
"description": "s-expression match patterns for Mozilla Parser AST", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"homepage": "https://github.com/phadej/jsstana", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -52,6 +52,6 @@ # jsstana [![Build Status](https://secure.travis-ci.org/phadej/jsstana.png?branch=master)](http://travis-ci.org/phadej/jsstana) | ||
There are some additional version: | ||
- `(literal-string value)` - string values | ||
- `(literal-number value)` - number values | ||
- `(literal-bool value)` - boolean values | ||
- `(literal-regexp value)` - regular expressions | ||
- `(string value)` - string values | ||
- `(number value)` - number values | ||
- `(bool value)` - boolean values | ||
- `(regexp value)` - regular expressions | ||
- `(true)` - matches `true` | ||
@@ -84,2 +84,16 @@ - `(false)` - matches `false` | ||
Also shorthand syntax is supported, `(+ a b)` is the same as `(binary + a b)`. | ||
#### (unary op value) | ||
Matches `UnaryExpression`. | ||
Also shorthand version works for `!` and `~`: `(~ ?foo)` is the same as `(unary ~ ?foo)`. | ||
#### (update op value) | ||
Matches `UpdateExpression`. | ||
You might want to use `postfix` and `prefix` though. | ||
#### (member object property) | ||
@@ -97,2 +111,4 @@ | ||
The `foo.bar.baz` will work as `(lookup foo.bar.baz)` as well. | ||
#### (throw ex) | ||
@@ -122,4 +138,12 @@ | ||
Use `grunt mochacov` to generate coverage report with blanket, | ||
or `istanbul cover grunt simplemocha` to do coverage with istanbul. | ||
## Release History | ||
- 0.0.8 Even more rands | ||
- unary and update expressions | ||
- drop `literal-` prefix (eg plain `string` now) | ||
- shorthand binary op syntax `(+ a b)` | ||
- shorthand lookup syntax | ||
- 0.0.7 jsgrep, third try | ||
@@ -126,0 +150,0 @@ - 0.0.6 jsgrep, second try |
@@ -12,2 +12,11 @@ /* global describe:true, it:true */ | ||
var syntax = esprima.parse("1 + 2;"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(binary)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), {}); | ||
}); | ||
it("matches binary operation expression nodes, inside expr", function () { | ||
var syntax = esprima.parse("1 + 2;"); | ||
var node = syntax.body[0]; | ||
@@ -29,2 +38,8 @@ var matcher = jsstana.match("(expr (binary))"); | ||
it("throws if invalid operator given", function () { | ||
assert.throws(function () { | ||
jsstana.match("(expr (binary +++))"); | ||
}); | ||
}); | ||
it("takes operator as first parameter, capture", function () { | ||
@@ -51,3 +66,3 @@ var syntax = esprima.parse("1 + 2;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (binary + (literal-number ?number) 2))"); | ||
var matcher = jsstana.match("(expr (binary + (number ?number) 2))"); | ||
@@ -57,2 +72,12 @@ assert.deepEqual(matcher(syntax), undefined); | ||
}); | ||
}); | ||
describe("+, -, * etc", function () { | ||
it("is the same as (binary op)", function () { | ||
var syntax = esprima.parse("1 + 2;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (+ (number ?number) 2))"); | ||
assert.deepEqual(matcher(node), { number: 1 }); | ||
}); | ||
}); |
@@ -67,2 +67,12 @@ /* global describe:true, it:true */ | ||
it("matches iff arguments matches", function () { | ||
var syntax = esprima.parse("module.fun(foo, bar)"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(call (lookup module.fun) foo baz)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
it("can use (call fun . ?) to match rest arguments", function () { | ||
@@ -69,0 +79,0 @@ var syntax = esprima.parse("module.fun(foo, bar, baz, quux)"); |
@@ -52,2 +52,10 @@ /* global describe:true, it:true */ | ||
it("numbers", function () { | ||
var syntax = esprima.parse("1;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr 2)"); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
it("null != false", function () { | ||
@@ -60,2 +68,18 @@ var syntax = esprima.parse("null;"); | ||
}); | ||
it("null != true", function () { | ||
var syntax = esprima.parse("null;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr true)"); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
it("false != null", function () { | ||
var syntax = esprima.parse("true;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr null)"); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
}); | ||
@@ -62,0 +86,0 @@ |
@@ -13,2 +13,11 @@ /* global describe:true, it:true */ | ||
var syntax = esprima.parse("'foo';"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(literal)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), {}); | ||
}); | ||
it("matches literal strings, inside expr", function () { | ||
var syntax = esprima.parse("'foo';"); | ||
var node = syntax.body[0]; | ||
@@ -50,6 +59,6 @@ var matcher = jsstana.match("(expr (literal))"); | ||
describe("one argument", function () { | ||
it("is the same as literal-string", function () { | ||
it("is the same as string", function () { | ||
var syntax = esprima.parse("'foo'"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string foo))"); | ||
var matcher = jsstana.match("(expr (string foo))"); | ||
@@ -62,3 +71,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
describe("literal-string", function () { | ||
describe("string", function () { | ||
describe("zero arguments", function () { | ||
@@ -68,3 +77,3 @@ it("matches literal strings", function () { | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string))"); | ||
var matcher = jsstana.match("(expr (string))"); | ||
@@ -78,3 +87,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string))"); | ||
var matcher = jsstana.match("(expr (string))"); | ||
@@ -88,3 +97,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string))"); | ||
var matcher = jsstana.match("(expr (string))"); | ||
@@ -98,3 +107,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string))"); | ||
var matcher = jsstana.match("(expr (string))"); | ||
@@ -108,3 +117,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string))"); | ||
var matcher = jsstana.match("(expr (string))"); | ||
@@ -120,3 +129,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string ?str))"); | ||
var matcher = jsstana.match("(expr (string ?str))"); | ||
@@ -129,4 +138,13 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var syntax = esprima.parse("'foo'"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(string foo)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), {}); | ||
}); | ||
it("doesn't match if different string, inside expr", function () { | ||
var syntax = esprima.parse("'foo'"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-string bar))"); | ||
var matcher = jsstana.match("(expr (string bar))"); | ||
@@ -139,3 +157,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
describe("literal-number", function () { | ||
describe("number", function () { | ||
describe("zero arguments", function () { | ||
@@ -145,3 +163,3 @@ it("doesn't match literal strings", function () { | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-number))"); | ||
var matcher = jsstana.match("(expr (number))"); | ||
@@ -155,3 +173,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-number))"); | ||
var matcher = jsstana.match("(expr (number))"); | ||
@@ -165,3 +183,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-number))"); | ||
var matcher = jsstana.match("(expr (number))"); | ||
@@ -175,3 +193,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-number))"); | ||
var matcher = jsstana.match("(expr (number))"); | ||
@@ -185,3 +203,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-number))"); | ||
var matcher = jsstana.match("(expr (number))"); | ||
@@ -197,3 +215,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-number ?number))"); | ||
var matcher = jsstana.match("(expr (number ?number))"); | ||
@@ -207,3 +225,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-number 2))"); | ||
var matcher = jsstana.match("(expr (number 2))"); | ||
@@ -216,3 +234,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
assert.throws(function () { | ||
jsstana.match("(expr (literal-number foo))"); | ||
jsstana.match("(expr (number foo))"); | ||
}); | ||
@@ -223,3 +241,3 @@ }); | ||
describe("literal-bool", function () { | ||
describe("bool", function () { | ||
describe("zero arguments", function () { | ||
@@ -229,3 +247,3 @@ it("doesn't match literal strings", function () { | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool))"); | ||
var matcher = jsstana.match("(expr (bool))"); | ||
@@ -239,3 +257,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool))"); | ||
var matcher = jsstana.match("(expr (bool))"); | ||
@@ -249,3 +267,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool))"); | ||
var matcher = jsstana.match("(expr (bool))"); | ||
@@ -259,3 +277,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool))"); | ||
var matcher = jsstana.match("(expr (bool))"); | ||
@@ -269,3 +287,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool))"); | ||
var matcher = jsstana.match("(expr (bool))"); | ||
@@ -281,3 +299,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool ?bool))"); | ||
var matcher = jsstana.match("(expr (bool ?bool))"); | ||
@@ -291,3 +309,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool true))"); | ||
var matcher = jsstana.match("(expr (bool true))"); | ||
@@ -301,3 +319,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool false))"); | ||
var matcher = jsstana.match("(expr (bool false))"); | ||
@@ -311,3 +329,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool false))"); | ||
var matcher = jsstana.match("(expr (bool false))"); | ||
@@ -321,3 +339,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-bool true))"); | ||
var matcher = jsstana.match("(expr (bool true))"); | ||
@@ -330,3 +348,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
assert.throws(function () { | ||
jsstana.match("(expr (literal-bool null))"); | ||
jsstana.match("(expr (bool null))"); | ||
}); | ||
@@ -337,8 +355,17 @@ }); | ||
describe("literal-regexp", function () { | ||
describe("regexp", function () { | ||
describe("zero arguments", function () { | ||
it("matches literal regexp", function () { | ||
var syntax = esprima.parse("/foo/;"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(regexp /foo/)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), {}); | ||
}); | ||
it("doesn't match literal strings", function () { | ||
var syntax = esprima.parse("'foo';"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp))"); | ||
var matcher = jsstana.match("(expr (regexp))"); | ||
@@ -352,3 +379,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp))"); | ||
var matcher = jsstana.match("(expr (regexp))"); | ||
@@ -362,3 +389,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp))"); | ||
var matcher = jsstana.match("(expr (regexp))"); | ||
@@ -372,3 +399,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp))"); | ||
var matcher = jsstana.match("(expr (regexp))"); | ||
@@ -379,6 +406,6 @@ assert.deepEqual(matcher(syntax), undefined); | ||
it("matches literal regexp", function () { | ||
it("matches literal regexp, inside expr", function () { | ||
var syntax = esprima.parse("/foo/;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp))"); | ||
var matcher = jsstana.match("(expr (regexp))"); | ||
@@ -388,2 +415,20 @@ assert.deepEqual(matcher(syntax), undefined); | ||
}); | ||
it("doesn't match different literal regexp, inside expr", function () { | ||
var syntax = esprima.parse("/foo/;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (regexp /bar/))"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
it("doesn't match not regexp, inside expr", function () { | ||
var syntax = esprima.parse("1;"); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (regexp /bar/))"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
}); | ||
@@ -395,3 +440,3 @@ | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp ?regexp))"); | ||
var matcher = jsstana.match("(expr (regexp ?regexp))"); | ||
@@ -405,3 +450,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp /foo/))"); | ||
var matcher = jsstana.match("(expr (regexp /foo/))"); | ||
@@ -415,3 +460,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp /foo/))"); | ||
var matcher = jsstana.match("(expr (regexp /foo/))"); | ||
@@ -425,3 +470,3 @@ assert.deepEqual(matcher(syntax), undefined); | ||
var node = syntax.body[0]; | ||
var matcher = jsstana.match("(expr (literal-regexp /a+/))"); | ||
var matcher = jsstana.match("(expr (regexp /a+/))"); | ||
@@ -428,0 +473,0 @@ assert.deepEqual(matcher(syntax), undefined); |
@@ -39,2 +39,34 @@ /* global describe:true, it:true */ | ||
}); | ||
}); | ||
describe("shorthand lookup", function () { | ||
it("makes it easier to work with properties", function () { | ||
var syntax = esprima.parse("foo.bar.baz"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("foo.bar.baz"); | ||
assert.deepEqual(matcher(node), {}); | ||
}); | ||
it("makes it easier to work with properties 2", function () { | ||
var syntax = esprima.parse("foo.quux.baz"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("foo.bar.baz"); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
it("makes it easier to work with properties 2", function () { | ||
var syntax = esprima.parse("foo.bar"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("?object.?property"); | ||
var m = matcher(node); | ||
assert.equal(m.object.type, "Identifier"); | ||
assert.equal(m.property.type, "Identifier"); | ||
assert.equal(m.object.name, "foo"); | ||
assert.equal(m.property.name, "bar"); | ||
}); | ||
}); |
@@ -27,2 +27,29 @@ /* global describe:true, it:true */ | ||
}); | ||
it("doesn't match if any argument doesn't match 1", function () { | ||
var syntax = esprima.parse("quux ? bar : baz"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(ternary foo bar baz)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
it("doesn't match if any argument doesn't match 2", function () { | ||
var syntax = esprima.parse("foo ? quux : baz"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(ternary foo bar baz)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
it("doesn't match if any argument doesn't match 3", function () { | ||
var syntax = esprima.parse("foo ? bar : quux"); | ||
var node = syntax.body[0].expression; | ||
var matcher = jsstana.match("(ternary foo bar baz)"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
}); |
@@ -74,3 +74,3 @@ /* global describe:true, it:true */ | ||
it("can use (null-node) to match uninitialized declrations", function () { | ||
it("can use (null-node) to match uninitialized declarations", function () { | ||
var syntax = esprima.parse("var foo;"); | ||
@@ -83,3 +83,12 @@ var node = syntax.body[0].declarations[0]; | ||
}); | ||
it("can use (null-node) to match uninitialized declarations", function () { | ||
var syntax = esprima.parse("var foo = 1;"); | ||
var node = syntax.body[0].declarations[0]; | ||
var matcher = jsstana.match("(var foo (null-node))"); | ||
assert.deepEqual(matcher(syntax), undefined); | ||
assert.deepEqual(matcher(node), undefined); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80993
30
2056
163