source: trip-planner-front/node_modules/acorn-import-assertions/src/index.js@ 188ee53

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

initial commit

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