source: trip-planner-front/node_modules/acorn-import-assertions/lib/index.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 8.6 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.importAssertions = importAssertions;
7
8var _acorn = _interopRequireWildcard(require("acorn"));
9
10function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
12function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
14const leftCurlyBrace = "{".charCodeAt(0);
15const space = " ".charCodeAt(0);
16const keyword = "assert";
17const FUNC_STATEMENT = 1,
18 FUNC_HANGING_STATEMENT = 2,
19 FUNC_NULLABLE_ID = 4;
20
21function importAssertions(Parser) {
22 // Use supplied version acorn version if present, to avoid
23 // reference mismatches due to different acorn versions. This
24 // allows this plugin to be used with Rollup which supplies
25 // its own internal version of acorn and thereby sidesteps
26 // the package manager.
27 const acorn = Parser.acorn || _acorn;
28 const {
29 tokTypes: tt,
30 TokenType
31 } = acorn;
32 return class extends Parser {
33 constructor(...args) {
34 super(...args);
35 this.assertToken = new TokenType(keyword);
36 }
37
38 _codeAt(i) {
39 return this.input.charCodeAt(i);
40 }
41
42 _eat(t) {
43 if (this.type !== t) {
44 this.unexpected();
45 }
46
47 this.next();
48 }
49
50 readToken(code) {
51 let i = 0;
52
53 for (; i < keyword.length; i++) {
54 if (this._codeAt(this.pos + i) !== keyword.charCodeAt(i)) {
55 return super.readToken(code);
56 }
57 } // ensure that the keyword is at the correct location
58 // ie `assert{...` or `assert {...`
59
60
61 for (;; i++) {
62 if (this._codeAt(this.pos + i) === leftCurlyBrace) {
63 // Found '{'
64 break;
65 } else if (this._codeAt(this.pos + i) === space) {
66 // white space is allowed between `assert` and `{`, so continue.
67 continue;
68 } else {
69 return super.readToken(code);
70 }
71 } // If we're inside a dynamic import expression we'll parse
72 // the `assert` keyword as a standard object property name
73 // ie `import(""./foo.json", { assert: { type: "json" } })`
74
75
76 if (this.type.label === "{") {
77 return super.readToken(code);
78 }
79
80 this.pos += keyword.length;
81 return this.finishToken(this.assertToken);
82 }
83
84 parseDynamicImport(node) {
85 this.next(); // skip `(`
86 // Parse node.source.
87
88 node.source = this.parseMaybeAssign();
89
90 if (this.eat(tt.comma)) {
91 const obj = this.parseObj(false);
92 node.arguments = [obj];
93 }
94
95 this._eat(tt.parenR);
96
97 return this.finishNode(node, "ImportExpression");
98 } // ported from acorn/src/statement.js pp.parseExport
99
100
101 parseExport(node, exports) {
102 this.next(); // export * from '...'
103
104 if (this.eat(tt.star)) {
105 if (this.options.ecmaVersion >= 11) {
106 if (this.eatContextual("as")) {
107 node.exported = this.parseIdent(true);
108 this.checkExport(exports, node.exported.name, this.lastTokStart);
109 } else {
110 node.exported = null;
111 }
112 }
113
114 this.expectContextual("from");
115
116 if (this.type !== tt.string) {
117 this.unexpected();
118 }
119
120 node.source = this.parseExprAtom();
121
122 if (this.type === this.assertToken) {
123 this.next();
124 const assertions = this.parseImportAssertions();
125
126 if (assertions) {
127 node.assertions = assertions;
128 }
129 }
130
131 this.semicolon();
132 return this.finishNode(node, "ExportAllDeclaration");
133 }
134
135 if (this.eat(tt._default)) {
136 // export default ...
137 this.checkExport(exports, "default", this.lastTokStart);
138 var isAsync;
139
140 if (this.type === tt._function || (isAsync = this.isAsyncFunction())) {
141 var fNode = this.startNode();
142 this.next();
143
144 if (isAsync) {
145 this.next();
146 }
147
148 node.declaration = this.parseFunction(fNode, FUNC_STATEMENT | FUNC_NULLABLE_ID, false, isAsync);
149 } else if (this.type === tt._class) {
150 var cNode = this.startNode();
151 node.declaration = this.parseClass(cNode, "nullableID");
152 } else {
153 node.declaration = this.parseMaybeAssign();
154 this.semicolon();
155 }
156
157 return this.finishNode(node, "ExportDefaultDeclaration");
158 } // export var|const|let|function|class ...
159
160
161 if (this.shouldParseExportStatement()) {
162 node.declaration = this.parseStatement(null);
163
164 if (node.declaration.type === "VariableDeclaration") {
165 this.checkVariableExport(exports, node.declaration.declarations);
166 } else {
167 this.checkExport(exports, node.declaration.id.name, node.declaration.id.start);
168 }
169
170 node.specifiers = [];
171 node.source = null;
172 } else {
173 // export { x, y as z } [from '...']
174 node.declaration = null;
175 node.specifiers = this.parseExportSpecifiers(exports);
176
177 if (this.eatContextual("from")) {
178 if (this.type !== tt.string) {
179 this.unexpected();
180 }
181
182 node.source = this.parseExprAtom();
183
184 if (this.type === this.assertToken) {
185 this.next();
186 const assertions = this.parseImportAssertions();
187
188 if (assertions) {
189 node.assertions = assertions;
190 }
191 }
192 } else {
193 for (var i = 0, list = node.specifiers; i < list.length; i += 1) {
194 // check for keywords used as local names
195 var spec = list[i];
196 this.checkUnreserved(spec.local); // check if export is defined
197
198 this.checkLocalExport(spec.local);
199 }
200
201 node.source = null;
202 }
203
204 this.semicolon();
205 }
206
207 return this.finishNode(node, "ExportNamedDeclaration");
208 }
209
210 parseImport(node) {
211 this.next(); // import '...'
212
213 if (this.type === tt.string) {
214 node.specifiers = [];
215 node.source = this.parseExprAtom();
216 } else {
217 node.specifiers = this.parseImportSpecifiers();
218 this.expectContextual("from");
219 node.source = this.type === tt.string ? this.parseExprAtom() : this.unexpected();
220 }
221
222 if (this.type === this.assertToken) {
223 this.next();
224 const assertions = this.parseImportAssertions();
225
226 if (assertions) {
227 node.assertions = assertions;
228 }
229 }
230
231 this.semicolon();
232 return this.finishNode(node, "ImportDeclaration");
233 }
234
235 parseImportAssertions() {
236 this._eat(tt.braceL);
237
238 const attrs = this.parseAssertEntries();
239
240 this._eat(tt.braceR);
241
242 return attrs;
243 }
244
245 parseAssertEntries() {
246 const attrs = [];
247 const attrNames = new Set();
248
249 do {
250 if (this.type === tt.braceR) {
251 break;
252 }
253
254 const node = this.startNode(); // parse AssertionKey : IdentifierName, StringLiteral
255
256 let assertionKeyNode;
257
258 if (this.type === tt.string) {
259 assertionKeyNode = this.parseLiteral(this.value);
260 } else {
261 assertionKeyNode = this.parseIdent(true);
262 }
263
264 this.next();
265 node.key = assertionKeyNode; // check if we already have an entry for an attribute
266 // if a duplicate entry is found, throw an error
267 // for now this logic will come into play only when someone declares `type` twice
268
269 if (attrNames.has(node.key.name)) {
270 this.raise(this.pos, "Duplicated key in assertions");
271 }
272
273 attrNames.add(node.key.name);
274
275 if (this.type !== tt.string) {
276 this.raise(this.pos, "Only string is supported as an assertion value");
277 }
278
279 node.value = this.parseLiteral(this.value);
280 attrs.push(this.finishNode(node, "ImportAttribute"));
281 } while (this.eat(tt.comma));
282
283 return attrs;
284 }
285
286 };
287}
Note: See TracBrowser for help on using the repository browser.