espurify

WebJar for espurify

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

espurify
Last Version

Last Version

1.7.0
Release Date

Release Date

Type

Type

jar
Description

Description

espurify
WebJar for espurify
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/estools/espurify

Download espurify

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/espurify/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>espurify</artifactId>
    <version>1.7.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/espurify/
implementation 'org.webjars.npm:espurify:1.7.0'
// https://jarcasting.com/artifacts/org.webjars.npm/espurify/
implementation ("org.webjars.npm:espurify:1.7.0")
'org.webjars.npm:espurify:jar:1.7.0'
<dependency org="org.webjars.npm" name="espurify" rev="1.7.0">
  <artifact name="espurify" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='espurify', version='1.7.0')
)
libraryDependencies += "org.webjars.npm" % "espurify" % "1.7.0"
[org.webjars.npm/espurify "1.7.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : core-js jar [2.0.0,3)

Project Modules

There are no modules declared in this project.

espurify

Clone new AST without extra properties

Build Status NPM version License Code Style

API

var purifiedAstClone = espurify(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.

Supported ECMAScript versions

var customizedCloneFunctionWithWhiteList = espurify.cloneWithWhitelist(whiteList)

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

var purifiedAstClone = customizedCloneFunctionWithWhiteList(originalAst)

Returns new clone of originalAst by customized function.

whiteList

type default value
object N/A

whiteList 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'],
    ...

var customizedCloneFunction = espurify.customize(options)

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

var 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.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

var espurify = require('espurify'),
    estraverse = require('estraverse'),
    esprima = require('esprima'),
    syntax = estraverse.Syntax,
    assert = require('assert');

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

// Adding extra informations to AST
var originalAst = esprima.parse(jsCode, {tolerant: true, loc: true, raw: true});
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
var purifiedClone = espurify(originalAst);


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


// 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'
                    }
                ]
            }
        }
    ]
});

INSTALL

via npm

Install

$ npm install --save espurify

Use

var espurify = require('espurify');

AUTHOR

CONTRIBUTORS

LICENSE

Licensed under the MIT license.

org.webjars.npm

ECMAScript Tooling

Versions

Version
1.7.0
1.3.0