Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

is-expression

pugjs7mMIT4.0.0

Check if a string is a valid JavaScript expression

javascript, expression

readme

is-expression

Validates a string as a JavaScript expression

Build Status Dependency Status Rolling Versions npm version

Installation

npm install is-expression

Usage

isExpression(src[, options])

Validates a string as a JavaScript expression.

src contains the source.

options can contain any Acorn options (since we use Acorn under-the-hood), or any of the following:

  • throw: Throw an error if the string is not an expression. The error can be an Acorn error, with location information in err.loc and err.pos. Defaults to false.
  • strict: Use strict mode when trying to parse the string. Defaults to false. Even if this option is false, if you have provided options.sourceType === 'module' which imples strict mode under ES2015, strict mode will be used.
  • lineComment: When true, allows line comments in the expression. Defaults to false for safety.

See the examples below for usage.

Examples

var isExpression = require('is-expression')

isExpression('myVar')
//=> true
isExpression('var')
//=> false
isExpression('["an", "array", "\'s"].indexOf("index")')
//=> true

isExpression('var', {throw: true})
// SyntaxError: Unexpected token (1:0)
//     at Parser.pp.raise (acorn/dist/acorn.js:940:13)
//     at ...

isExpression('public')
//=> true
isExpression('public', {strict: true})
//=> false

isExpression('abc // my comment')
//=> false
isExpression('abc // my comment', {lineComment: true})
//=> true

License

MIT

changelog

Change Log

Version 4.0.0 onwards are documented in Releases.

This project adheres to Semantic Versioning.

3.0.0 - 2016-09-11

Added

  • Updated to acorn ~4.0.0
    • ES2016 has been made the default ecmaVersion.
    • Async functions are now implemented for {ecmaVersion: 8}.
    • See acorn's CHANGELOG for a full list of changes.

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 for a full list of changes.

2.0.1 - 2016-06-04

Added

  • Updated to acorn ~3.1.0
    • See acorn's CHANGELOG for a list of changes.
    • Even though it is a minor version bump for acorn, the new features are not in parts of acorn we are using, and thus a patch level bump is warranted.

2.0.0 - 2016-02-12

Added

1.0.2 - 2016-01-06

Added

  • Updated to acorn ~2.7.0

1.0.1 - 2015-11-12

Fixed

  • Use a stricter version range for Acorn since we depend on Acorn internals.

1.0.0 - 2015-11-11

Added

  • Initial release