@hailin-zheng/editor-core 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index-cjs.js +4 -807
- package/index-cjs.js.map +1 -1
- package/index.js +3 -807
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index-cjs.js
CHANGED
@@ -6,6 +6,7 @@ var nanoid = require('nanoid');
|
|
6
6
|
var moment = require('moment');
|
7
7
|
var acor = require('acorn');
|
8
8
|
var astring = require('astring');
|
9
|
+
var estraverse = require('estraverse');
|
9
10
|
var bwipjs = require('bwip-js');
|
10
11
|
|
11
12
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
@@ -30,6 +31,7 @@ function _interopNamespace(e) {
|
|
30
31
|
|
31
32
|
var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);
|
32
33
|
var acor__namespace = /*#__PURE__*/_interopNamespace(acor);
|
34
|
+
var estraverse__namespace = /*#__PURE__*/_interopNamespace(estraverse);
|
33
35
|
var bwipjs__default = /*#__PURE__*/_interopDefaultLegacy(bwipjs);
|
34
36
|
|
35
37
|
/**
|
@@ -2708,814 +2710,9 @@ class DataDecorateRenderObject extends LeafRenderObject {
|
|
2708
2710
|
}
|
2709
2711
|
}
|
2710
2712
|
|
2711
|
-
/*
|
2712
|
-
Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
|
2713
|
-
Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
|
2714
|
-
|
2715
|
-
Redistribution and use in source and binary forms, with or without
|
2716
|
-
modification, are permitted provided that the following conditions are met:
|
2717
|
-
|
2718
|
-
* Redistributions of source code must retain the above copyright
|
2719
|
-
notice, this list of conditions and the following disclaimer.
|
2720
|
-
* Redistributions in binary form must reproduce the above copyright
|
2721
|
-
notice, this list of conditions and the following disclaimer in the
|
2722
|
-
documentation and/or other materials provided with the distribution.
|
2723
|
-
|
2724
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
2725
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
2726
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2727
|
-
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
2728
|
-
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
2729
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
2730
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
2731
|
-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2732
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
2733
|
-
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2734
|
-
*/
|
2735
|
-
/*jslint vars:false, bitwise:true*/
|
2736
|
-
/*jshint indent:4*/
|
2737
|
-
/*global exports:true*/
|
2738
|
-
(function clone(exports) {
|
2739
|
-
|
2740
|
-
var Syntax,
|
2741
|
-
VisitorOption,
|
2742
|
-
VisitorKeys,
|
2743
|
-
BREAK,
|
2744
|
-
SKIP,
|
2745
|
-
REMOVE;
|
2746
|
-
|
2747
|
-
function deepCopy(obj) {
|
2748
|
-
var ret = {}, key, val;
|
2749
|
-
for (key in obj) {
|
2750
|
-
if (obj.hasOwnProperty(key)) {
|
2751
|
-
val = obj[key];
|
2752
|
-
if (typeof val === 'object' && val !== null) {
|
2753
|
-
ret[key] = deepCopy(val);
|
2754
|
-
} else {
|
2755
|
-
ret[key] = val;
|
2756
|
-
}
|
2757
|
-
}
|
2758
|
-
}
|
2759
|
-
return ret;
|
2760
|
-
}
|
2761
|
-
|
2762
|
-
// based on LLVM libc++ upper_bound / lower_bound
|
2763
|
-
// MIT License
|
2764
|
-
|
2765
|
-
function upperBound(array, func) {
|
2766
|
-
var diff, len, i, current;
|
2767
|
-
|
2768
|
-
len = array.length;
|
2769
|
-
i = 0;
|
2770
|
-
|
2771
|
-
while (len) {
|
2772
|
-
diff = len >>> 1;
|
2773
|
-
current = i + diff;
|
2774
|
-
if (func(array[current])) {
|
2775
|
-
len = diff;
|
2776
|
-
} else {
|
2777
|
-
i = current + 1;
|
2778
|
-
len -= diff + 1;
|
2779
|
-
}
|
2780
|
-
}
|
2781
|
-
return i;
|
2782
|
-
}
|
2783
|
-
|
2784
|
-
Syntax = {
|
2785
|
-
AssignmentExpression: 'AssignmentExpression',
|
2786
|
-
AssignmentPattern: 'AssignmentPattern',
|
2787
|
-
ArrayExpression: 'ArrayExpression',
|
2788
|
-
ArrayPattern: 'ArrayPattern',
|
2789
|
-
ArrowFunctionExpression: 'ArrowFunctionExpression',
|
2790
|
-
AwaitExpression: 'AwaitExpression', // CAUTION: It's deferred to ES7.
|
2791
|
-
BlockStatement: 'BlockStatement',
|
2792
|
-
BinaryExpression: 'BinaryExpression',
|
2793
|
-
BreakStatement: 'BreakStatement',
|
2794
|
-
CallExpression: 'CallExpression',
|
2795
|
-
CatchClause: 'CatchClause',
|
2796
|
-
ChainExpression: 'ChainExpression',
|
2797
|
-
ClassBody: 'ClassBody',
|
2798
|
-
ClassDeclaration: 'ClassDeclaration',
|
2799
|
-
ClassExpression: 'ClassExpression',
|
2800
|
-
ComprehensionBlock: 'ComprehensionBlock', // CAUTION: It's deferred to ES7.
|
2801
|
-
ComprehensionExpression: 'ComprehensionExpression', // CAUTION: It's deferred to ES7.
|
2802
|
-
ConditionalExpression: 'ConditionalExpression',
|
2803
|
-
ContinueStatement: 'ContinueStatement',
|
2804
|
-
DebuggerStatement: 'DebuggerStatement',
|
2805
|
-
DirectiveStatement: 'DirectiveStatement',
|
2806
|
-
DoWhileStatement: 'DoWhileStatement',
|
2807
|
-
EmptyStatement: 'EmptyStatement',
|
2808
|
-
ExportAllDeclaration: 'ExportAllDeclaration',
|
2809
|
-
ExportDefaultDeclaration: 'ExportDefaultDeclaration',
|
2810
|
-
ExportNamedDeclaration: 'ExportNamedDeclaration',
|
2811
|
-
ExportSpecifier: 'ExportSpecifier',
|
2812
|
-
ExpressionStatement: 'ExpressionStatement',
|
2813
|
-
ForStatement: 'ForStatement',
|
2814
|
-
ForInStatement: 'ForInStatement',
|
2815
|
-
ForOfStatement: 'ForOfStatement',
|
2816
|
-
FunctionDeclaration: 'FunctionDeclaration',
|
2817
|
-
FunctionExpression: 'FunctionExpression',
|
2818
|
-
GeneratorExpression: 'GeneratorExpression', // CAUTION: It's deferred to ES7.
|
2819
|
-
Identifier: 'Identifier',
|
2820
|
-
IfStatement: 'IfStatement',
|
2821
|
-
ImportExpression: 'ImportExpression',
|
2822
|
-
ImportDeclaration: 'ImportDeclaration',
|
2823
|
-
ImportDefaultSpecifier: 'ImportDefaultSpecifier',
|
2824
|
-
ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
|
2825
|
-
ImportSpecifier: 'ImportSpecifier',
|
2826
|
-
Literal: 'Literal',
|
2827
|
-
LabeledStatement: 'LabeledStatement',
|
2828
|
-
LogicalExpression: 'LogicalExpression',
|
2829
|
-
MemberExpression: 'MemberExpression',
|
2830
|
-
MetaProperty: 'MetaProperty',
|
2831
|
-
MethodDefinition: 'MethodDefinition',
|
2832
|
-
ModuleSpecifier: 'ModuleSpecifier',
|
2833
|
-
NewExpression: 'NewExpression',
|
2834
|
-
ObjectExpression: 'ObjectExpression',
|
2835
|
-
ObjectPattern: 'ObjectPattern',
|
2836
|
-
PrivateIdentifier: 'PrivateIdentifier',
|
2837
|
-
Program: 'Program',
|
2838
|
-
Property: 'Property',
|
2839
|
-
PropertyDefinition: 'PropertyDefinition',
|
2840
|
-
RestElement: 'RestElement',
|
2841
|
-
ReturnStatement: 'ReturnStatement',
|
2842
|
-
SequenceExpression: 'SequenceExpression',
|
2843
|
-
SpreadElement: 'SpreadElement',
|
2844
|
-
Super: 'Super',
|
2845
|
-
SwitchStatement: 'SwitchStatement',
|
2846
|
-
SwitchCase: 'SwitchCase',
|
2847
|
-
TaggedTemplateExpression: 'TaggedTemplateExpression',
|
2848
|
-
TemplateElement: 'TemplateElement',
|
2849
|
-
TemplateLiteral: 'TemplateLiteral',
|
2850
|
-
ThisExpression: 'ThisExpression',
|
2851
|
-
ThrowStatement: 'ThrowStatement',
|
2852
|
-
TryStatement: 'TryStatement',
|
2853
|
-
UnaryExpression: 'UnaryExpression',
|
2854
|
-
UpdateExpression: 'UpdateExpression',
|
2855
|
-
VariableDeclaration: 'VariableDeclaration',
|
2856
|
-
VariableDeclarator: 'VariableDeclarator',
|
2857
|
-
WhileStatement: 'WhileStatement',
|
2858
|
-
WithStatement: 'WithStatement',
|
2859
|
-
YieldExpression: 'YieldExpression'
|
2860
|
-
};
|
2861
|
-
|
2862
|
-
VisitorKeys = {
|
2863
|
-
AssignmentExpression: ['left', 'right'],
|
2864
|
-
AssignmentPattern: ['left', 'right'],
|
2865
|
-
ArrayExpression: ['elements'],
|
2866
|
-
ArrayPattern: ['elements'],
|
2867
|
-
ArrowFunctionExpression: ['params', 'body'],
|
2868
|
-
AwaitExpression: ['argument'], // CAUTION: It's deferred to ES7.
|
2869
|
-
BlockStatement: ['body'],
|
2870
|
-
BinaryExpression: ['left', 'right'],
|
2871
|
-
BreakStatement: ['label'],
|
2872
|
-
CallExpression: ['callee', 'arguments'],
|
2873
|
-
CatchClause: ['param', 'body'],
|
2874
|
-
ChainExpression: ['expression'],
|
2875
|
-
ClassBody: ['body'],
|
2876
|
-
ClassDeclaration: ['id', 'superClass', 'body'],
|
2877
|
-
ClassExpression: ['id', 'superClass', 'body'],
|
2878
|
-
ComprehensionBlock: ['left', 'right'], // CAUTION: It's deferred to ES7.
|
2879
|
-
ComprehensionExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7.
|
2880
|
-
ConditionalExpression: ['test', 'consequent', 'alternate'],
|
2881
|
-
ContinueStatement: ['label'],
|
2882
|
-
DebuggerStatement: [],
|
2883
|
-
DirectiveStatement: [],
|
2884
|
-
DoWhileStatement: ['body', 'test'],
|
2885
|
-
EmptyStatement: [],
|
2886
|
-
ExportAllDeclaration: ['source'],
|
2887
|
-
ExportDefaultDeclaration: ['declaration'],
|
2888
|
-
ExportNamedDeclaration: ['declaration', 'specifiers', 'source'],
|
2889
|
-
ExportSpecifier: ['exported', 'local'],
|
2890
|
-
ExpressionStatement: ['expression'],
|
2891
|
-
ForStatement: ['init', 'test', 'update', 'body'],
|
2892
|
-
ForInStatement: ['left', 'right', 'body'],
|
2893
|
-
ForOfStatement: ['left', 'right', 'body'],
|
2894
|
-
FunctionDeclaration: ['id', 'params', 'body'],
|
2895
|
-
FunctionExpression: ['id', 'params', 'body'],
|
2896
|
-
GeneratorExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7.
|
2897
|
-
Identifier: [],
|
2898
|
-
IfStatement: ['test', 'consequent', 'alternate'],
|
2899
|
-
ImportExpression: ['source'],
|
2900
|
-
ImportDeclaration: ['specifiers', 'source'],
|
2901
|
-
ImportDefaultSpecifier: ['local'],
|
2902
|
-
ImportNamespaceSpecifier: ['local'],
|
2903
|
-
ImportSpecifier: ['imported', 'local'],
|
2904
|
-
Literal: [],
|
2905
|
-
LabeledStatement: ['label', 'body'],
|
2906
|
-
LogicalExpression: ['left', 'right'],
|
2907
|
-
MemberExpression: ['object', 'property'],
|
2908
|
-
MetaProperty: ['meta', 'property'],
|
2909
|
-
MethodDefinition: ['key', 'value'],
|
2910
|
-
ModuleSpecifier: [],
|
2911
|
-
NewExpression: ['callee', 'arguments'],
|
2912
|
-
ObjectExpression: ['properties'],
|
2913
|
-
ObjectPattern: ['properties'],
|
2914
|
-
PrivateIdentifier: [],
|
2915
|
-
Program: ['body'],
|
2916
|
-
Property: ['key', 'value'],
|
2917
|
-
PropertyDefinition: ['key', 'value'],
|
2918
|
-
RestElement: [ 'argument' ],
|
2919
|
-
ReturnStatement: ['argument'],
|
2920
|
-
SequenceExpression: ['expressions'],
|
2921
|
-
SpreadElement: ['argument'],
|
2922
|
-
Super: [],
|
2923
|
-
SwitchStatement: ['discriminant', 'cases'],
|
2924
|
-
SwitchCase: ['test', 'consequent'],
|
2925
|
-
TaggedTemplateExpression: ['tag', 'quasi'],
|
2926
|
-
TemplateElement: [],
|
2927
|
-
TemplateLiteral: ['quasis', 'expressions'],
|
2928
|
-
ThisExpression: [],
|
2929
|
-
ThrowStatement: ['argument'],
|
2930
|
-
TryStatement: ['block', 'handler', 'finalizer'],
|
2931
|
-
UnaryExpression: ['argument'],
|
2932
|
-
UpdateExpression: ['argument'],
|
2933
|
-
VariableDeclaration: ['declarations'],
|
2934
|
-
VariableDeclarator: ['id', 'init'],
|
2935
|
-
WhileStatement: ['test', 'body'],
|
2936
|
-
WithStatement: ['object', 'body'],
|
2937
|
-
YieldExpression: ['argument']
|
2938
|
-
};
|
2939
|
-
|
2940
|
-
// unique id
|
2941
|
-
BREAK = {};
|
2942
|
-
SKIP = {};
|
2943
|
-
REMOVE = {};
|
2944
|
-
|
2945
|
-
VisitorOption = {
|
2946
|
-
Break: BREAK,
|
2947
|
-
Skip: SKIP,
|
2948
|
-
Remove: REMOVE
|
2949
|
-
};
|
2950
|
-
|
2951
|
-
function Reference(parent, key) {
|
2952
|
-
this.parent = parent;
|
2953
|
-
this.key = key;
|
2954
|
-
}
|
2955
|
-
|
2956
|
-
Reference.prototype.replace = function replace(node) {
|
2957
|
-
this.parent[this.key] = node;
|
2958
|
-
};
|
2959
|
-
|
2960
|
-
Reference.prototype.remove = function remove() {
|
2961
|
-
if (Array.isArray(this.parent)) {
|
2962
|
-
this.parent.splice(this.key, 1);
|
2963
|
-
return true;
|
2964
|
-
} else {
|
2965
|
-
this.replace(null);
|
2966
|
-
return false;
|
2967
|
-
}
|
2968
|
-
};
|
2969
|
-
|
2970
|
-
function Element(node, path, wrap, ref) {
|
2971
|
-
this.node = node;
|
2972
|
-
this.path = path;
|
2973
|
-
this.wrap = wrap;
|
2974
|
-
this.ref = ref;
|
2975
|
-
}
|
2976
|
-
|
2977
|
-
function Controller() { }
|
2978
|
-
|
2979
|
-
// API:
|
2980
|
-
// return property path array from root to current node
|
2981
|
-
Controller.prototype.path = function path() {
|
2982
|
-
var i, iz, j, jz, result, element;
|
2983
|
-
|
2984
|
-
function addToPath(result, path) {
|
2985
|
-
if (Array.isArray(path)) {
|
2986
|
-
for (j = 0, jz = path.length; j < jz; ++j) {
|
2987
|
-
result.push(path[j]);
|
2988
|
-
}
|
2989
|
-
} else {
|
2990
|
-
result.push(path);
|
2991
|
-
}
|
2992
|
-
}
|
2993
|
-
|
2994
|
-
// root node
|
2995
|
-
if (!this.__current.path) {
|
2996
|
-
return null;
|
2997
|
-
}
|
2998
|
-
|
2999
|
-
// first node is sentinel, second node is root element
|
3000
|
-
result = [];
|
3001
|
-
for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {
|
3002
|
-
element = this.__leavelist[i];
|
3003
|
-
addToPath(result, element.path);
|
3004
|
-
}
|
3005
|
-
addToPath(result, this.__current.path);
|
3006
|
-
return result;
|
3007
|
-
};
|
3008
|
-
|
3009
|
-
// API:
|
3010
|
-
// return type of current node
|
3011
|
-
Controller.prototype.type = function () {
|
3012
|
-
var node = this.current();
|
3013
|
-
return node.type || this.__current.wrap;
|
3014
|
-
};
|
3015
|
-
|
3016
|
-
// API:
|
3017
|
-
// return array of parent elements
|
3018
|
-
Controller.prototype.parents = function parents() {
|
3019
|
-
var i, iz, result;
|
3020
|
-
|
3021
|
-
// first node is sentinel
|
3022
|
-
result = [];
|
3023
|
-
for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {
|
3024
|
-
result.push(this.__leavelist[i].node);
|
3025
|
-
}
|
3026
|
-
|
3027
|
-
return result;
|
3028
|
-
};
|
3029
|
-
|
3030
|
-
// API:
|
3031
|
-
// return current node
|
3032
|
-
Controller.prototype.current = function current() {
|
3033
|
-
return this.__current.node;
|
3034
|
-
};
|
3035
|
-
|
3036
|
-
Controller.prototype.__execute = function __execute(callback, element) {
|
3037
|
-
var previous, result;
|
3038
|
-
|
3039
|
-
result = undefined;
|
3040
|
-
|
3041
|
-
previous = this.__current;
|
3042
|
-
this.__current = element;
|
3043
|
-
this.__state = null;
|
3044
|
-
if (callback) {
|
3045
|
-
result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);
|
3046
|
-
}
|
3047
|
-
this.__current = previous;
|
3048
|
-
|
3049
|
-
return result;
|
3050
|
-
};
|
3051
|
-
|
3052
|
-
// API:
|
3053
|
-
// notify control skip / break
|
3054
|
-
Controller.prototype.notify = function notify(flag) {
|
3055
|
-
this.__state = flag;
|
3056
|
-
};
|
3057
|
-
|
3058
|
-
// API:
|
3059
|
-
// skip child nodes of current node
|
3060
|
-
Controller.prototype.skip = function () {
|
3061
|
-
this.notify(SKIP);
|
3062
|
-
};
|
3063
|
-
|
3064
|
-
// API:
|
3065
|
-
// break traversals
|
3066
|
-
Controller.prototype['break'] = function () {
|
3067
|
-
this.notify(BREAK);
|
3068
|
-
};
|
3069
|
-
|
3070
|
-
// API:
|
3071
|
-
// remove node
|
3072
|
-
Controller.prototype.remove = function () {
|
3073
|
-
this.notify(REMOVE);
|
3074
|
-
};
|
3075
|
-
|
3076
|
-
Controller.prototype.__initialize = function(root, visitor) {
|
3077
|
-
this.visitor = visitor;
|
3078
|
-
this.root = root;
|
3079
|
-
this.__worklist = [];
|
3080
|
-
this.__leavelist = [];
|
3081
|
-
this.__current = null;
|
3082
|
-
this.__state = null;
|
3083
|
-
this.__fallback = null;
|
3084
|
-
if (visitor.fallback === 'iteration') {
|
3085
|
-
this.__fallback = Object.keys;
|
3086
|
-
} else if (typeof visitor.fallback === 'function') {
|
3087
|
-
this.__fallback = visitor.fallback;
|
3088
|
-
}
|
3089
|
-
|
3090
|
-
this.__keys = VisitorKeys;
|
3091
|
-
if (visitor.keys) {
|
3092
|
-
this.__keys = Object.assign(Object.create(this.__keys), visitor.keys);
|
3093
|
-
}
|
3094
|
-
};
|
3095
|
-
|
3096
|
-
function isNode(node) {
|
3097
|
-
if (node == null) {
|
3098
|
-
return false;
|
3099
|
-
}
|
3100
|
-
return typeof node === 'object' && typeof node.type === 'string';
|
3101
|
-
}
|
3102
|
-
|
3103
|
-
function isProperty(nodeType, key) {
|
3104
|
-
return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key;
|
3105
|
-
}
|
3106
|
-
|
3107
|
-
function candidateExistsInLeaveList(leavelist, candidate) {
|
3108
|
-
for (var i = leavelist.length - 1; i >= 0; --i) {
|
3109
|
-
if (leavelist[i].node === candidate) {
|
3110
|
-
return true;
|
3111
|
-
}
|
3112
|
-
}
|
3113
|
-
return false;
|
3114
|
-
}
|
3115
|
-
|
3116
|
-
Controller.prototype.traverse = function traverse(root, visitor) {
|
3117
|
-
var worklist,
|
3118
|
-
leavelist,
|
3119
|
-
element,
|
3120
|
-
node,
|
3121
|
-
nodeType,
|
3122
|
-
ret,
|
3123
|
-
key,
|
3124
|
-
current,
|
3125
|
-
current2,
|
3126
|
-
candidates,
|
3127
|
-
candidate,
|
3128
|
-
sentinel;
|
3129
|
-
|
3130
|
-
this.__initialize(root, visitor);
|
3131
|
-
|
3132
|
-
sentinel = {};
|
3133
|
-
|
3134
|
-
// reference
|
3135
|
-
worklist = this.__worklist;
|
3136
|
-
leavelist = this.__leavelist;
|
3137
|
-
|
3138
|
-
// initialize
|
3139
|
-
worklist.push(new Element(root, null, null, null));
|
3140
|
-
leavelist.push(new Element(null, null, null, null));
|
3141
|
-
|
3142
|
-
while (worklist.length) {
|
3143
|
-
element = worklist.pop();
|
3144
|
-
|
3145
|
-
if (element === sentinel) {
|
3146
|
-
element = leavelist.pop();
|
3147
|
-
|
3148
|
-
ret = this.__execute(visitor.leave, element);
|
3149
|
-
|
3150
|
-
if (this.__state === BREAK || ret === BREAK) {
|
3151
|
-
return;
|
3152
|
-
}
|
3153
|
-
continue;
|
3154
|
-
}
|
3155
|
-
|
3156
|
-
if (element.node) {
|
3157
|
-
|
3158
|
-
ret = this.__execute(visitor.enter, element);
|
3159
|
-
|
3160
|
-
if (this.__state === BREAK || ret === BREAK) {
|
3161
|
-
return;
|
3162
|
-
}
|
3163
|
-
|
3164
|
-
worklist.push(sentinel);
|
3165
|
-
leavelist.push(element);
|
3166
|
-
|
3167
|
-
if (this.__state === SKIP || ret === SKIP) {
|
3168
|
-
continue;
|
3169
|
-
}
|
3170
|
-
|
3171
|
-
node = element.node;
|
3172
|
-
nodeType = node.type || element.wrap;
|
3173
|
-
candidates = this.__keys[nodeType];
|
3174
|
-
if (!candidates) {
|
3175
|
-
if (this.__fallback) {
|
3176
|
-
candidates = this.__fallback(node);
|
3177
|
-
} else {
|
3178
|
-
throw new Error('Unknown node type ' + nodeType + '.');
|
3179
|
-
}
|
3180
|
-
}
|
3181
|
-
|
3182
|
-
current = candidates.length;
|
3183
|
-
while ((current -= 1) >= 0) {
|
3184
|
-
key = candidates[current];
|
3185
|
-
candidate = node[key];
|
3186
|
-
if (!candidate) {
|
3187
|
-
continue;
|
3188
|
-
}
|
3189
|
-
|
3190
|
-
if (Array.isArray(candidate)) {
|
3191
|
-
current2 = candidate.length;
|
3192
|
-
while ((current2 -= 1) >= 0) {
|
3193
|
-
if (!candidate[current2]) {
|
3194
|
-
continue;
|
3195
|
-
}
|
3196
|
-
|
3197
|
-
if (candidateExistsInLeaveList(leavelist, candidate[current2])) {
|
3198
|
-
continue;
|
3199
|
-
}
|
3200
|
-
|
3201
|
-
if (isProperty(nodeType, candidates[current])) {
|
3202
|
-
element = new Element(candidate[current2], [key, current2], 'Property', null);
|
3203
|
-
} else if (isNode(candidate[current2])) {
|
3204
|
-
element = new Element(candidate[current2], [key, current2], null, null);
|
3205
|
-
} else {
|
3206
|
-
continue;
|
3207
|
-
}
|
3208
|
-
worklist.push(element);
|
3209
|
-
}
|
3210
|
-
} else if (isNode(candidate)) {
|
3211
|
-
if (candidateExistsInLeaveList(leavelist, candidate)) {
|
3212
|
-
continue;
|
3213
|
-
}
|
3214
|
-
|
3215
|
-
worklist.push(new Element(candidate, key, null, null));
|
3216
|
-
}
|
3217
|
-
}
|
3218
|
-
}
|
3219
|
-
}
|
3220
|
-
};
|
3221
|
-
|
3222
|
-
Controller.prototype.replace = function replace(root, visitor) {
|
3223
|
-
var worklist,
|
3224
|
-
leavelist,
|
3225
|
-
node,
|
3226
|
-
nodeType,
|
3227
|
-
target,
|
3228
|
-
element,
|
3229
|
-
current,
|
3230
|
-
current2,
|
3231
|
-
candidates,
|
3232
|
-
candidate,
|
3233
|
-
sentinel,
|
3234
|
-
outer,
|
3235
|
-
key;
|
3236
|
-
|
3237
|
-
function removeElem(element) {
|
3238
|
-
var i,
|
3239
|
-
key,
|
3240
|
-
nextElem,
|
3241
|
-
parent;
|
3242
|
-
|
3243
|
-
if (element.ref.remove()) {
|
3244
|
-
// When the reference is an element of an array.
|
3245
|
-
key = element.ref.key;
|
3246
|
-
parent = element.ref.parent;
|
3247
|
-
|
3248
|
-
// If removed from array, then decrease following items' keys.
|
3249
|
-
i = worklist.length;
|
3250
|
-
while (i--) {
|
3251
|
-
nextElem = worklist[i];
|
3252
|
-
if (nextElem.ref && nextElem.ref.parent === parent) {
|
3253
|
-
if (nextElem.ref.key < key) {
|
3254
|
-
break;
|
3255
|
-
}
|
3256
|
-
--nextElem.ref.key;
|
3257
|
-
}
|
3258
|
-
}
|
3259
|
-
}
|
3260
|
-
}
|
3261
|
-
|
3262
|
-
this.__initialize(root, visitor);
|
3263
|
-
|
3264
|
-
sentinel = {};
|
3265
|
-
|
3266
|
-
// reference
|
3267
|
-
worklist = this.__worklist;
|
3268
|
-
leavelist = this.__leavelist;
|
3269
|
-
|
3270
|
-
// initialize
|
3271
|
-
outer = {
|
3272
|
-
root: root
|
3273
|
-
};
|
3274
|
-
element = new Element(root, null, null, new Reference(outer, 'root'));
|
3275
|
-
worklist.push(element);
|
3276
|
-
leavelist.push(element);
|
3277
|
-
|
3278
|
-
while (worklist.length) {
|
3279
|
-
element = worklist.pop();
|
3280
|
-
|
3281
|
-
if (element === sentinel) {
|
3282
|
-
element = leavelist.pop();
|
3283
|
-
|
3284
|
-
target = this.__execute(visitor.leave, element);
|
3285
|
-
|
3286
|
-
// node may be replaced with null,
|
3287
|
-
// so distinguish between undefined and null in this place
|
3288
|
-
if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
|
3289
|
-
// replace
|
3290
|
-
element.ref.replace(target);
|
3291
|
-
}
|
3292
|
-
|
3293
|
-
if (this.__state === REMOVE || target === REMOVE) {
|
3294
|
-
removeElem(element);
|
3295
|
-
}
|
3296
|
-
|
3297
|
-
if (this.__state === BREAK || target === BREAK) {
|
3298
|
-
return outer.root;
|
3299
|
-
}
|
3300
|
-
continue;
|
3301
|
-
}
|
3302
|
-
|
3303
|
-
target = this.__execute(visitor.enter, element);
|
3304
|
-
|
3305
|
-
// node may be replaced with null,
|
3306
|
-
// so distinguish between undefined and null in this place
|
3307
|
-
if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
|
3308
|
-
// replace
|
3309
|
-
element.ref.replace(target);
|
3310
|
-
element.node = target;
|
3311
|
-
}
|
3312
|
-
|
3313
|
-
if (this.__state === REMOVE || target === REMOVE) {
|
3314
|
-
removeElem(element);
|
3315
|
-
element.node = null;
|
3316
|
-
}
|
3317
|
-
|
3318
|
-
if (this.__state === BREAK || target === BREAK) {
|
3319
|
-
return outer.root;
|
3320
|
-
}
|
3321
|
-
|
3322
|
-
// node may be null
|
3323
|
-
node = element.node;
|
3324
|
-
if (!node) {
|
3325
|
-
continue;
|
3326
|
-
}
|
3327
|
-
|
3328
|
-
worklist.push(sentinel);
|
3329
|
-
leavelist.push(element);
|
3330
|
-
|
3331
|
-
if (this.__state === SKIP || target === SKIP) {
|
3332
|
-
continue;
|
3333
|
-
}
|
3334
|
-
|
3335
|
-
nodeType = node.type || element.wrap;
|
3336
|
-
candidates = this.__keys[nodeType];
|
3337
|
-
if (!candidates) {
|
3338
|
-
if (this.__fallback) {
|
3339
|
-
candidates = this.__fallback(node);
|
3340
|
-
} else {
|
3341
|
-
throw new Error('Unknown node type ' + nodeType + '.');
|
3342
|
-
}
|
3343
|
-
}
|
3344
|
-
|
3345
|
-
current = candidates.length;
|
3346
|
-
while ((current -= 1) >= 0) {
|
3347
|
-
key = candidates[current];
|
3348
|
-
candidate = node[key];
|
3349
|
-
if (!candidate) {
|
3350
|
-
continue;
|
3351
|
-
}
|
3352
|
-
|
3353
|
-
if (Array.isArray(candidate)) {
|
3354
|
-
current2 = candidate.length;
|
3355
|
-
while ((current2 -= 1) >= 0) {
|
3356
|
-
if (!candidate[current2]) {
|
3357
|
-
continue;
|
3358
|
-
}
|
3359
|
-
if (isProperty(nodeType, candidates[current])) {
|
3360
|
-
element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));
|
3361
|
-
} else if (isNode(candidate[current2])) {
|
3362
|
-
element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));
|
3363
|
-
} else {
|
3364
|
-
continue;
|
3365
|
-
}
|
3366
|
-
worklist.push(element);
|
3367
|
-
}
|
3368
|
-
} else if (isNode(candidate)) {
|
3369
|
-
worklist.push(new Element(candidate, key, null, new Reference(node, key)));
|
3370
|
-
}
|
3371
|
-
}
|
3372
|
-
}
|
3373
|
-
|
3374
|
-
return outer.root;
|
3375
|
-
};
|
3376
|
-
|
3377
|
-
function traverse(root, visitor) {
|
3378
|
-
var controller = new Controller();
|
3379
|
-
return controller.traverse(root, visitor);
|
3380
|
-
}
|
3381
|
-
|
3382
|
-
function replace(root, visitor) {
|
3383
|
-
var controller = new Controller();
|
3384
|
-
return controller.replace(root, visitor);
|
3385
|
-
}
|
3386
|
-
|
3387
|
-
function extendCommentRange(comment, tokens) {
|
3388
|
-
var target;
|
3389
|
-
|
3390
|
-
target = upperBound(tokens, function search(token) {
|
3391
|
-
return token.range[0] > comment.range[0];
|
3392
|
-
});
|
3393
|
-
|
3394
|
-
comment.extendedRange = [comment.range[0], comment.range[1]];
|
3395
|
-
|
3396
|
-
if (target !== tokens.length) {
|
3397
|
-
comment.extendedRange[1] = tokens[target].range[0];
|
3398
|
-
}
|
3399
|
-
|
3400
|
-
target -= 1;
|
3401
|
-
if (target >= 0) {
|
3402
|
-
comment.extendedRange[0] = tokens[target].range[1];
|
3403
|
-
}
|
3404
|
-
|
3405
|
-
return comment;
|
3406
|
-
}
|
3407
|
-
|
3408
|
-
function attachComments(tree, providedComments, tokens) {
|
3409
|
-
// At first, we should calculate extended comment ranges.
|
3410
|
-
var comments = [], comment, len, i, cursor;
|
3411
|
-
|
3412
|
-
if (!tree.range) {
|
3413
|
-
throw new Error('attachComments needs range information');
|
3414
|
-
}
|
3415
|
-
|
3416
|
-
// tokens array is empty, we attach comments to tree as 'leadingComments'
|
3417
|
-
if (!tokens.length) {
|
3418
|
-
if (providedComments.length) {
|
3419
|
-
for (i = 0, len = providedComments.length; i < len; i += 1) {
|
3420
|
-
comment = deepCopy(providedComments[i]);
|
3421
|
-
comment.extendedRange = [0, tree.range[0]];
|
3422
|
-
comments.push(comment);
|
3423
|
-
}
|
3424
|
-
tree.leadingComments = comments;
|
3425
|
-
}
|
3426
|
-
return tree;
|
3427
|
-
}
|
3428
|
-
|
3429
|
-
for (i = 0, len = providedComments.length; i < len; i += 1) {
|
3430
|
-
comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
|
3431
|
-
}
|
3432
|
-
|
3433
|
-
// This is based on John Freeman's implementation.
|
3434
|
-
cursor = 0;
|
3435
|
-
traverse(tree, {
|
3436
|
-
enter: function (node) {
|
3437
|
-
var comment;
|
3438
|
-
|
3439
|
-
while (cursor < comments.length) {
|
3440
|
-
comment = comments[cursor];
|
3441
|
-
if (comment.extendedRange[1] > node.range[0]) {
|
3442
|
-
break;
|
3443
|
-
}
|
3444
|
-
|
3445
|
-
if (comment.extendedRange[1] === node.range[0]) {
|
3446
|
-
if (!node.leadingComments) {
|
3447
|
-
node.leadingComments = [];
|
3448
|
-
}
|
3449
|
-
node.leadingComments.push(comment);
|
3450
|
-
comments.splice(cursor, 1);
|
3451
|
-
} else {
|
3452
|
-
cursor += 1;
|
3453
|
-
}
|
3454
|
-
}
|
3455
|
-
|
3456
|
-
// already out of owned node
|
3457
|
-
if (cursor === comments.length) {
|
3458
|
-
return VisitorOption.Break;
|
3459
|
-
}
|
3460
|
-
|
3461
|
-
if (comments[cursor].extendedRange[0] > node.range[1]) {
|
3462
|
-
return VisitorOption.Skip;
|
3463
|
-
}
|
3464
|
-
}
|
3465
|
-
});
|
3466
|
-
|
3467
|
-
cursor = 0;
|
3468
|
-
traverse(tree, {
|
3469
|
-
leave: function (node) {
|
3470
|
-
var comment;
|
3471
|
-
|
3472
|
-
while (cursor < comments.length) {
|
3473
|
-
comment = comments[cursor];
|
3474
|
-
if (node.range[1] < comment.extendedRange[0]) {
|
3475
|
-
break;
|
3476
|
-
}
|
3477
|
-
|
3478
|
-
if (node.range[1] === comment.extendedRange[0]) {
|
3479
|
-
if (!node.trailingComments) {
|
3480
|
-
node.trailingComments = [];
|
3481
|
-
}
|
3482
|
-
node.trailingComments.push(comment);
|
3483
|
-
comments.splice(cursor, 1);
|
3484
|
-
} else {
|
3485
|
-
cursor += 1;
|
3486
|
-
}
|
3487
|
-
}
|
3488
|
-
|
3489
|
-
// already out of owned node
|
3490
|
-
if (cursor === comments.length) {
|
3491
|
-
return VisitorOption.Break;
|
3492
|
-
}
|
3493
|
-
|
3494
|
-
if (comments[cursor].extendedRange[0] > node.range[1]) {
|
3495
|
-
return VisitorOption.Skip;
|
3496
|
-
}
|
3497
|
-
}
|
3498
|
-
});
|
3499
|
-
|
3500
|
-
return tree;
|
3501
|
-
}
|
3502
|
-
|
3503
|
-
exports.Syntax = Syntax;
|
3504
|
-
exports.traverse = traverse;
|
3505
|
-
exports.replace = replace;
|
3506
|
-
exports.attachComments = attachComments;
|
3507
|
-
exports.VisitorKeys = VisitorKeys;
|
3508
|
-
exports.VisitorOption = VisitorOption;
|
3509
|
-
exports.Controller = Controller;
|
3510
|
-
exports.cloneEnvironment = function () { return clone({}); };
|
3511
|
-
|
3512
|
-
return exports;
|
3513
|
-
}(exports));
|
3514
|
-
/* vim: set sw=4 ts=4 et tw=80 : */
|
3515
|
-
|
3516
2713
|
function parser(code) {
|
3517
2714
|
const node = acor__namespace.parse(code, { ecmaVersion: 'latest' });
|
3518
|
-
|
2715
|
+
estraverse__namespace.traverse(node, {
|
3519
2716
|
enter: (child, parent) => {
|
3520
2717
|
if (child.type === 'Identifier') {
|
3521
2718
|
const identifierName = child['name'];
|
@@ -3525,7 +2722,7 @@ function parser(code) {
|
|
3525
2722
|
}
|
3526
2723
|
}
|
3527
2724
|
});
|
3528
|
-
|
2725
|
+
estraverse__namespace.replace(node, {
|
3529
2726
|
leave: (child) => {
|
3530
2727
|
//函数调用
|
3531
2728
|
if (child.type == 'CallExpression') {
|