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