is-expression
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -5,2 +5,9 @@ # Change Log | ||
## 2.1.0 - 2016-07-27 | ||
### Added | ||
- Updated to acorn ~3.3.0 | ||
- The ES2016 check for strict mode in function parameters is now implemented | ||
for `{ecmaVersion: 7}`. | ||
- See [acorn's CHANGELOG][acorn-3.3.0] for a full list of changes. | ||
## 2.0.1 - 2016-06-04 | ||
@@ -30,3 +37,4 @@ ### Added | ||
[acorn-3.3.0]: https://github.com/ternjs/acorn/blob/master/CHANGELOG.md#330-2016-07-25 | ||
[acorn-3.1.0]: https://github.com/ternjs/acorn/blob/master/CHANGELOG.md#310-2016-04-18 | ||
[acorn-3.0.0]: https://github.com/ternjs/acorn/blob/master/CHANGELOG.md#300-2016-02-10 |
44
index.js
'use strict'; | ||
var acorn = require('acorn') | ||
var objectAssign = require('object-assign') | ||
var acorn = require('acorn'); | ||
var objectAssign = require('object-assign'); | ||
module.exports = isExpression | ||
module.exports = isExpression; | ||
@@ -12,23 +12,35 @@ var DEFAULT_OPTIONS = { | ||
lineComment: false | ||
} | ||
}; | ||
function isExpression (src, options) { | ||
options = objectAssign({}, DEFAULT_OPTIONS, options) | ||
function isExpression(src, options) { | ||
options = objectAssign({}, DEFAULT_OPTIONS, options); | ||
try { | ||
var parser = new acorn.Parser(options, src, 0) | ||
if (options.strict) parser.strict = true | ||
var parser = new acorn.Parser(options, src, 0); | ||
if (options.strict) { | ||
parser.strict = true; | ||
} | ||
if (!options.lineComment) { | ||
parser.skipLineComment = function (startSkip) { | ||
this.raise(this.pos, 'Line comments not allowed in an expression') | ||
} | ||
this.raise(this.pos, 'Line comments not allowed in an expression'); | ||
}; | ||
} | ||
parser.nextToken() | ||
parser.parseExpression() | ||
if (parser.type !== acorn.tokTypes.eof) parser.unexpected() | ||
parser.nextToken(); | ||
parser.parseExpression(); | ||
if (parser.type !== acorn.tokTypes.eof) { | ||
parser.unexpected(); | ||
} | ||
} catch (ex) { | ||
if (!options.throw) return false | ||
throw ex | ||
if (!options.throw) { | ||
return false; | ||
} | ||
throw ex; | ||
} | ||
return true | ||
return true; | ||
} |
{ | ||
"name": "is-expression", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Check if a string is a valid JavaScript expression", | ||
@@ -10,3 +10,3 @@ "keywords": [ | ||
"dependencies": { | ||
"acorn": "~3.1.0", | ||
"acorn": "~3.3.0", | ||
"object-assign": "^4.0.1" | ||
@@ -24,3 +24,3 @@ }, | ||
"type": "git", | ||
"url": "https://github.com/TimothyGu/is-expression.git" | ||
"url": "https://github.com/pugjs/is-expression.git" | ||
}, | ||
@@ -27,0 +27,0 @@ "author": "Timothy Gu <timothygu99@gmail.com>", |
@@ -5,4 +5,4 @@ # is-expression | ||
[![Build Status](https://img.shields.io/travis/TimothyGu/is-expression/master.svg)](https://travis-ci.org/TimothyGu/is-expression) | ||
[![Dependency Status](https://img.shields.io/david/TimothyGu/is-expression.svg)](https://david-dm.org/TimothyGu/is-expression) | ||
[![Build Status](https://img.shields.io/travis/pugjs/is-expression/master.svg)](https://travis-ci.org/pugjs/is-expression) | ||
[![Dependency Status](https://img.shields.io/david/pugjs/is-expression.svg)](https://david-dm.org/pugjs/is-expression) | ||
[![npm version](https://img.shields.io/npm/v/is-expression.svg)](https://www.npmjs.org/package/is-expression) | ||
@@ -9,0 +9,0 @@ |
68
test.js
@@ -1,46 +0,48 @@ | ||
'use strict' | ||
'use strict'; | ||
var assert = require('assert') | ||
var testit = require('testit') | ||
var isExpression = require('./') | ||
var assert = require('assert'); | ||
var testit = require('testit'); | ||
var isExpression = require('./'); | ||
function passes (src, options) { | ||
function passes(src, options) { | ||
testit(JSON.stringify(src, options), function () { | ||
options = options || {} | ||
assert(isExpression(src, options)) | ||
}) | ||
options = options || {}; | ||
assert(isExpression(src, options)); | ||
}); | ||
} | ||
testit('passes', function () { | ||
passes('myVar') | ||
passes('["an", "array", "\'s"].indexOf("index")') | ||
passes('\npublic') | ||
passes('abc // my comment', {lineComment: true}) | ||
passes('() => a', {ecmaVersion: 6}) | ||
}) | ||
passes('myVar'); | ||
passes('["an", "array", "\'s"].indexOf("index")'); | ||
passes('\npublic'); | ||
passes('abc // my comment', {lineComment: true}); | ||
passes('() => a'); | ||
passes('function (a = "default") {"use strict";}'); | ||
}); | ||
function error (src, line, col, options) { | ||
function error(src, line, col, options) { | ||
testit(JSON.stringify(src), function () { | ||
options = options || {} | ||
assert(!isExpression(src, options)) | ||
options.throw = true | ||
options = options || {}; | ||
assert(!isExpression(src, options)); | ||
options.throw = true; | ||
assert.throws(function () { | ||
isExpression(src, options) | ||
isExpression(src, options); | ||
}, function (err) { | ||
assert.equal(err.loc.line, line) | ||
assert.equal(err.loc.column, col) | ||
assert(err.message) | ||
return true | ||
}) | ||
}) | ||
assert.equal(err.loc.line, line); | ||
assert.equal(err.loc.column, col); | ||
assert(err.message); | ||
return true; | ||
}); | ||
}); | ||
} | ||
testit('fails', function () { | ||
error('', 1, 0) | ||
error('var', 1, 0) | ||
error('weird error', 1, 6) | ||
error('asdf}', 1, 4) | ||
error('\npublic', 2, 0, {strict: true}) | ||
error('abc // my comment', 1, 4) | ||
error('() => a', 1, 1, {ecmaVersion: 5}) | ||
}) | ||
error('', 1, 0); | ||
error('var', 1, 0); | ||
error('weird error', 1, 6); | ||
error('asdf}', 1, 4); | ||
error('\npublic', 2, 0, {strict: true}); | ||
error('abc // my comment', 1, 4); | ||
error('() => a', 1, 1, {ecmaVersion: 5}); | ||
error('function (a = "default") {"use strict";}', 1, 26, {ecmaVersion: 7}); | ||
}); |
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
7195
77
+ Addedacorn@3.3.0(transitive)
- Removedacorn@3.1.0(transitive)
Updatedacorn@~3.3.0