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.
npm install is-expressionVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic expression validation using `isExpression` and how to leverage `throw`, `strict`, and `lineComment` options to control parsing behavior and error handling.
Review existing uses of `is-expression` after upgrading to v4.0.0. If you relied on certain syntax being invalid for older JavaScript versions, you may need to introduce custom parsing options via `options` or implement additional validation logic.
If line comments are expected and should be treated as part of a valid expression, you must explicitly pass `{ lineComment: true }` in the options object to `isExpression`.For stricter validation consistent with modern JavaScript modules or strict-mode code, always provide `{ strict: true }` in the options object to `isExpression`.Ensure 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.For CommonJS, use `const isExpression = require('is-expression');`. For ES Modules, use `import { isExpression } from 'is-expression';`. Ensure your environment supports the chosen module system.No dependency data recorded yet.