is-expression
The `is-expression` package provides a utility function for synchronously validating if a given string is a syntactically correct JavaScript expression. It is currently at version 4.0.0 and appears to have an infrequent release cadence, with the latest major update focusing on parser upgrades. The library uses Acorn under the hood to perform its parsing, allowing for detailed error reporting when the `throw` option is enabled. Key differentiators include its simplicity, direct integration with Acorn options, and the ability to toggle strict mode or allow line comments, making it suitable for environments like templating engines (e.g., Pug) or dynamic code evaluation where robust expression validation is crucial. It focuses purely on syntax, not semantic validity or runtime behavior.
Common errors
-
SyntaxError: Unexpected token (1:0) at Parser.pp.raise (acorn/dist/acorn.js:XXX:YY) ...
cause Attempting to validate a string that is not a valid JavaScript expression or contains syntax not allowed by the default Acorn parsing options.fixEnsure the input string is a true JavaScript expression. If you require specific parsing allowances (e.g., comments, non-standard syntax), pass appropriate `options` like `{ lineComment: true }` or Acorn-specific parser options. If the error is about a keyword like 'var' or 'const', remember these are statements/declarations, not expressions. -
ReferenceError: isExpression is not defined
cause The `isExpression` function was not correctly imported or required.fixFor CommonJS, use `const isExpression = require('is-expression');`. For ES Modules, use `import { isExpression } from 'is-expression';`. Ensure your environment supports the chosen module system.
Warnings
- breaking Version 4.0.0 upgraded the underlying Acorn parser from 4.0.2 to 7.1.1. This change significantly updates the default parsing mode to ES2019, meaning many constructs previously considered invalid expressions will now parse successfully as valid ES2019 expressions.
- gotcha By default, `isExpression` does not allow line comments within the string being validated. An expression like `a + b // comment` will be considered invalid.
- gotcha The `isExpression` function defaults to non-strict mode parsing. This can lead to certain keywords or syntax (e.g., `public`) being accepted as valid expressions, which might be invalid in strict-mode contexts.
Install
-
npm install is-expression -
yarn add is-expression -
pnpm add is-expression
Imports
- isExpression
import isExpression from 'is-expression'
import { isExpression } from 'is-expression' - isExpression
const isExpression = require('is-expression') - isExpression
import * as isExpressionModule from 'is-expression'; isExpressionModule.isExpression('foo')
Quickstart
import { isExpression } from 'is-expression';
// Basic validation
console.log(isExpression('myVar')); // => true
console.log(isExpression('var')); // => false (keyword, not expression)
console.log(isExpression('["an", "array"].indexOf("index")')); // => true
// Using options: throw on error
try {
isExpression('const foo', { throw: true }); // 'const' is a declaration, not an expression
} catch (error) {
console.error('Caught expected error:', error.message); // SyntaxError: Unexpected token 'const' (1:0)
}
// Using options: strict mode
console.log(isExpression('public')); // => true (in non-strict mode)
console.log(isExpression('public', { strict: true })); // => false (in strict mode, 'public' is a reserved keyword)
// Using options: allowing comments
console.log(isExpression('abc // my comment')); // => false (comments not allowed by default)
console.log(isExpression('abc // my comment', { lineComment: true })); // => true