1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 | const Collection = require('./Collection');
|
11 |
|
12 | const collections = require('./collections');
|
13 | const getParser = require('./getParser');
|
14 | const matchNode = require('./matchNode');
|
15 | const recast = require('recast');
|
16 | const template = require('./template');
|
17 |
|
18 | const Node = recast.types.namedTypes.Node;
|
19 | const NodePath = recast.types.NodePath;
|
20 |
|
21 |
|
22 | for (var name in collections) {
|
23 | collections[name].register();
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function core(source, options) {
|
43 | return typeof source === 'string' ?
|
44 | fromSource(source, options) :
|
45 | fromAST(source);
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function fromAST(ast) {
|
57 | if (Array.isArray(ast)) {
|
58 | if (ast[0] instanceof NodePath || ast.length === 0) {
|
59 | return Collection.fromPaths(ast);
|
60 | } else if (Node.check(ast[0])) {
|
61 | return Collection.fromNodes(ast);
|
62 | }
|
63 | } else {
|
64 | if (ast instanceof NodePath) {
|
65 | return Collection.fromPaths([ast]);
|
66 | } else if (Node.check(ast)) {
|
67 | return Collection.fromNodes([ast]);
|
68 | }
|
69 | }
|
70 | throw new TypeError(
|
71 | 'Received an unexpected value ' + Object.prototype.toString.call(ast)
|
72 | );
|
73 | }
|
74 |
|
75 | function fromSource(source, options) {
|
76 | if (!options) {
|
77 | options = {};
|
78 | }
|
79 | if (!options.parser) {
|
80 | options.parser = getParser();
|
81 | }
|
82 | return fromAST(recast.parse(source, options));
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function match(path, filter) {
|
94 | if (!(path instanceof NodePath)) {
|
95 | if (typeof path.get === 'function') {
|
96 | path = path.get();
|
97 | } else {
|
98 | path = {value: path};
|
99 | }
|
100 | }
|
101 | return matchNode(path.value, filter);
|
102 | }
|
103 |
|
104 | const plugins = [];
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function use(plugin) {
|
118 | if (plugins.indexOf(plugin) === -1) {
|
119 | plugins.push(plugin);
|
120 | plugin(core);
|
121 | }
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | function withParser(parser) {
|
132 | if (typeof parser === 'string') {
|
133 | parser = getParser(parser);
|
134 | }
|
135 |
|
136 | const newCore = function(source, options) {
|
137 | if (options && !options.parser) {
|
138 | options.parser = parser;
|
139 | } else {
|
140 | options = {parser};
|
141 | }
|
142 | return core(source, options);
|
143 | };
|
144 |
|
145 | return enrichCore(newCore, parser);
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | function enrichCore(core, parser) {
|
155 |
|
156 | Object.assign(core, recast.types.namedTypes);
|
157 | Object.assign(core, recast.types.builders);
|
158 | core.registerMethods = Collection.registerMethods;
|
159 | |
160 |
|
161 |
|
162 |
|
163 | core.types = recast.types;
|
164 | core.match = match;
|
165 | core.template = template(parser);
|
166 |
|
167 |
|
168 | core.filters = {};
|
169 | core.mappings = {};
|
170 | for (const name in collections) {
|
171 | if (collections[name].filters) {
|
172 | core.filters[name] = collections[name].filters;
|
173 | }
|
174 | if (collections[name].mappings) {
|
175 | core.mappings[name] = collections[name].mappings;
|
176 | }
|
177 | }
|
178 | core.use = use;
|
179 | core.withParser = withParser;
|
180 | return core;
|
181 | }
|
182 |
|
183 | module.exports = enrichCore(core, getParser());
|