acorn-has-side-effect

A simple module to check whether an expression has any side effects.

MIT License

Downloads
5
Stars
6
Committers
1

acorn-has-side-effect

A simple module to check whether an expression has any side effects. This is aimed at being useful for writing optimising JavaScript compilers.

Installation

npm install acorn-has-side-effect

Usage

'use strict';

var assert = require('assert');
var hasSideEffect = require('./');

assert(hasSideEffect('x = 10') === true);
assert(hasSideEffect('x++') === true);
assert(hasSideEffect('x') === false);
assert(hasSideEffect('x', {strict: true}) === true);
assert(hasSideEffect('x.foo') === false);
assert(hasSideEffect('x.foo', {strict: true}) === true);
assert(hasSideEffect('this') === false);
assert(hasSideEffect('this', {strict: true}) === false);
assert(hasSideEffect('this.foo') === false);
assert(hasSideEffect('this.foo', {strict: true}) === true);

console.log('tests passed');

It assumes that property-access and variable-access should not count as a side effect, even though they technically could be because of property accessors and reference errors etc. For most purposes this should be fine, but it could go wrong in some rare circumstances. You can force it to treat even these nodes as having side effects by passing {strict: true} for the options.

If you already have an AST, you can pass an acorn AST instead of a string for the source code.

License

MIT

Package Rankings
Top 25.11% on Npmjs.org
Badges
Extracted from project README
Build Status Dependency Status NPM version