Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Clone AST without extra properties

License

NotificationsYou must be signed in to change notification settings

estools/espurify

Repository files navigation

Clone AST without extra properties

Build StatusNPM versionCode StyleLicense

API

const purifiedAstClone = espurify.purifyAst(originalAst)

Returns new clone oforiginalAst but without extra properties.

Leaves properties defined inThe ESTree Spec (formerly known asMozilla SpiderMonkey Parser API) only. Also note that extra informations (such asloc,range andraw) are eliminated too.

(note: usingespurify 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-providedallowList.

(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 oforiginalAst by customized function.

allowList

typedefault value
objectN/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 customoptions.

const purifiedAstClone = customizedCloneFunction(originalAst)

Returns new clone oforiginalAst by customized function.

options

typedefault value
object{}

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

options.ecmaVersion

typedefault value
string ornumber2025

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

options.extra

typedefault value
array ofstringnull

List of extra properties to be left in result AST. For example, functions returned byespurify.customize({extra: ['raw']}) will preserveraw properties ofLiteral. Functions return byespurify.customize({extra: ['loc', 'range']}) will preserveloc andrange properties of each Node.

EXAMPLE

constespurify=require('espurify');constestraverse=require('estraverse');constacorn=require('acorn');constsyntax=estraverse.Syntax;constassert=require('assert');constjsCode='assert("foo")';// Adding extra informations to ASTconstoriginalAst=acorn.parse(jsCode,{locations:true,ranges:true,ecmaVersion:2022});estraverse.replace(originalAst,{leave:function(currentNode,parentNode){if(currentNode.type===syntax.Literal&&typeofcurrentNode.raw!=='undefined'){currentNode['x-verbatim-bar']={content :currentNode.raw,precedence :18// escodegen.Precedence.Primary};returncurrentNode;}else{returnundefined;}}});// purify ASTconstpurifiedClone=espurify.purifyAst(originalAst);// Extra properties are eliminated from cloned ASTassert.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 modifiedassert.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

constespurify=require('espurify');

AUTHOR

CONTRIBUTORS

LICENSE

Licensed under theMIT license.


[8]ページ先頭

©2009-2025 Movatter.jp