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

Package detail

espurify

estools1.1mMIT3.2.0

Clone AST without extra properties

ast, estree, ecmascript, es6

readme

espurify

Clone AST without extra properties

Build Status NPM version Code Style License

API

const purifiedAstClone = espurify.purifyAst(originalAst)

Returns new clone of originalAst but without extra properties.

Leaves properties defined in The ESTree Spec (formerly known as Mozilla SpiderMonkey Parser API) only. Also note that extra informations (such as loc, range and raw) are eliminated too.

(note: using espurify as a default exported function is deprecated in favor of named exports aiming ESM era, and will be removed in future major releases)

Supported ECMAScript versions

const customizedCloneFunctionWithAllowList = espurify.cloneWithAllowlist(allowList)

Returns customized function for cloning AST, with user-provided allowList.

(note: espurify.cloneWithWhitelist is still exported but deprecated in favor of more inclusive language and will be removed in future major releases)

const purifiedAstClone = customizedCloneFunctionWithAllowList(originalAst)

Returns new clone of originalAst by customized function.

allowList

type default value
object N/A

allowList is an object containing NodeType as keys and properties as values.

{
    ArrayExpression: ['type', 'elements'],
    ArrayPattern: ['type', 'elements'],
    ArrowFunctionExpression: ['type', 'id', 'params', 'body', 'generator', 'expression'],
    AssignmentExpression: ['type', 'operator', 'left', 'right'],
    ...

const customizedCloneFunction = espurify.customize(options)

Returns customized function for cloning AST, configured by custom options.

const purifiedAstClone = customizedCloneFunction(originalAst)

Returns new clone of originalAst by customized function.

options

type default value
object {}

Configuration options. If not passed, default options will be used.

options.ecmaVersion

type default value
string or number 2025

Indicates the ECMAScript version to clone. Must be either 5, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025.

options.extra

type default value
array of string null

List of extra properties to be left in result AST. For example, functions returned by espurify.customize({extra: ['raw']}) will preserve raw properties of Literal. Functions return by espurify.customize({extra: ['loc', 'range']}) will preserve loc and range properties of each Node.

EXAMPLE

const espurify = require('espurify');
const estraverse = require('estraverse');
const acorn = require('acorn');
const syntax = estraverse.Syntax;
const assert = require('assert');

const jsCode = 'assert("foo")';

// Adding extra informations to AST
const originalAst = acorn.parse(jsCode, { locations: true, ranges: true, ecmaVersion: 2022 });
estraverse.replace(originalAst, {
  leave: function (currentNode, parentNode) {
    if (currentNode.type === syntax.Literal && typeof currentNode.raw !== 'undefined') {
      currentNode['x-verbatim-bar'] = {
        content : currentNode.raw,
        precedence : 18  // escodegen.Precedence.Primary
      };
      return currentNode;
    } else {
      return undefined;
    }
  }
});


// purify AST
const purifiedClone = espurify.purifyAst(originalAst);


// Extra properties are eliminated from cloned AST
assert.deepEqual(purifiedClone, {
  type: 'Program',
  body: [
    {
      type: 'ExpressionStatement',
      expression: {
        type: 'CallExpression',
        callee: {
          type: 'Identifier',
          name: 'assert'
        },
        arguments: [
          {
            type: 'Literal',
            value: 'foo'
          }
        ],
        optional: false
      }
    }
  ],
  sourceType: 'script'
});


// original AST is not modified
assert.deepEqual(originalAst,{
  type: 'Program',
  start: 0,
  end: 13,
  loc: {
    start: {
      line: 1,
      column: 0
    },
    end: {
      line: 1,
      column: 13
    }
  },
  range: [
    0,
    13
  ],
  body: [
    {
      type: 'ExpressionStatement',
      start: 0,
      end: 13,
      loc: {
        start: {
          line: 1,
          column: 0
        },
        end: {
          line: 1,
          column: 13
        }
      },
      range: [
        0,
        13
      ],
      expression: {
        type: 'CallExpression',
        start: 0,
        end: 13,
        loc: {
          start: {
            line: 1,
            column: 0
          },
          end: {
            line: 1,
            column: 13
          }
        },
        range: [
          0,
          13
        ],
        callee: {
          type: 'Identifier',
          start: 0,
          end: 6,
          loc: {
            start: {
              line: 1,
              column: 0
            },
            end: {
              line: 1,
              column: 6
            }
          },
          range: [
            0,
            6
          ],
          name: 'assert'
        },
        arguments: [
          {
            type: 'Literal',
            start: 7,
            end: 12,
            loc: {
              start: {
                line: 1,
                column: 7
              },
              end: {
                line: 1,
                column: 12
              }
            },
            range: [
              7,
              12
            ],
            value: 'foo',
            raw: '"foo"',
            "x-verbatim-bar": {
              content: '"foo"',
              precedence: 18
            }
          }
        ],
        optional: false
      }
    }
  ],
  sourceType: 'script'
});

INSTALL

via npm

Install

$ npm install --save espurify

Use

const espurify = require('espurify');

AUTHOR

CONTRIBUTORS

LICENSE

Licensed under the MIT license.

changelog

3.2.0 (2024-12-23)

Features

3.1.0 (2024-07-04)

Features

3.0.0 (2022-07-17)

Features

Breaking Changes

This release will not affect most users immediately. There are three notable changes.

  1. espurify function is still exported as default but deprecated in favor of named exports aiming ESM era, and will be removed in future major releases. Please use espurify.purifyAst instead.

  2. espurify.cloneWithWhitelist is still exported but deprecated in favor of more inclusive language and will be removed in future major releases. Please use espurify.cloneWithAllowlist instead.

  3. Some new properties will appear in purified AST and may affect deep-equality of the tree, since default ecmaVersion is changed from 2018 to 2022 which add some properties to existing Nodes.

-  CallExpression: ['type', 'callee', 'arguments'],
+  CallExpression: ['type', 'callee', 'arguments', 'optional'],
-  ExportAllDeclaration: ['type', 'source'],
+  ExportAllDeclaration: ['type', 'source', 'exported'],
-  Literal: ['type', 'value', 'regex'],
+  Literal: ['type', 'value', 'regex', 'bigint'],

To make espurify's behavior same as v2, please use espurify.customize function with ecmaVersion: 2018 option.

const purify = espurify.customize({ ecmaVersion: 2018 });
const clonedAst = purify(originalAst);

2.1.1 (2021-03-29)

Bug Fixes

2.1.0 (2021-03-26)

Features

  • Support ES2020 grammar
    • support ChainExpression
    • support ImportExpression
    • support exported property of ExportAllDeclaration
    • support BigInt literals

2.0.1 (2019-02-15)

Bug Fixes

2.0.0 (2018-11-23)

Breaking Changes

We drop support of ancient (= before ES6) environments. Please use polyfills by your own.

We stopped providing prebuilt bundle for browsers. Please build your own by your bundler. We also dropped bower support. Please use npm instead.

1.8.1 (2018-07-10)

Bug Fixes

1.8.0 (2018-05-10)

Features

1.7.0 (2017-02-24)

Features

1.6.1 (2017-02-13)

Bug Fixes

  • fix property order of ConditionalExpression (01c13ada)

1.6.0 (2016-05-25)

Features

1.5.1 (2016-03-28)

Performance Improvements

1.5.0 (2015-12-21)

Features

  • prepend type to whitelist if it does not exist (af941315)

1.4.0 (2015-12-18)

Features

1.3.0 (2015-06-05)

Features

  • add delegate to YieldExpression (8ef733ed)

1.2.0 (2015-04-17)

Features

1.1.0 (2015-04-12)

Features

  • switch to The ESTree Spec to support ES6 (b9ca486e)

Bug Fixes

  • avoid cloning private (maybe recursive) props (501b12bf)

1.0.1 (2015-03-06)

Moved repository to estools (2014-11-24)

Moved repository from twada/espurify to estools/espurify.

1.0.0 (2014-11-01)

The first stable release.

0.1.3 (2014-08-01)

0.1.2 (2014-07-22)

0.1.1 (2014-07-22)

0.1.0 (2014-07-22)

The first release.