1# is-expression
2
3Validates a string as a JavaScript expression
4
5[![Build Status](https://img.shields.io/travis/pugjs/is-expression/master.svg)](https://travis-ci.org/pugjs/is-expression)
6[![Dependency Status](https://img.shields.io/david/pugjs/is-expression.svg)](https://david-dm.org/pugjs/is-expression)
7[![npm version](https://img.shields.io/npm/v/is-expression.svg)](https://www.npmjs.org/package/is-expression)
8
9## Installation
10
11    npm install is-expression
12
13## Usage
14
15### `isExpression(src[, options])`
16
17Validates a string as a JavaScript expression.
18
19`src` contains the source.
20
21`options` can contain any Acorn options (since we use Acorn under-the-hood),
22or any of the following:
23
24- `throw`: Throw an error if the string is not an expression. The error can
25  be an Acorn error, with location information in `err.loc` and `err.pos`.
26  Defaults to `false`.
27- `strict`: Use strict mode when trying to parse the string. Defaults to
28  `false`. Even if this option is `false`, if you have provided
29  `options.sourceType === 'module'` which imples strict mode under ES2015,
30  strict mode will be used.
31- `lineComment`: When `true`, allows line comments in the expression.
32  Defaults to `false` for safety.
33
34See the examples below for usage.
35
36## Examples
37
38```js
39var isExpression = require('is-expression')
40
41isExpression('myVar')
42//=> true
43isExpression('var')
44//=> false
45isExpression('["an", "array", "\'s"].indexOf("index")')
46//=> true
47
48isExpression('var', {throw: true})
49// SyntaxError: Unexpected token (1:0)
50//     at Parser.pp.raise (acorn/dist/acorn.js:940:13)
51//     at ...
52
53isExpression('public')
54//=> true
55isExpression('public', {strict: true})
56//=> false
57
58isExpression('abc // my comment')
59//=> false
60isExpression('abc // my comment', {lineComment: true})
61//=> true
62```
63
64## License
65
66MIT
67