source: frontend/node_modules/@rollup/pluginutils/dist/cjs/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 14.4 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var path = require('path');
8var pm = _interopDefault(require('picomatch'));
9
10const addExtension = function addExtension(filename, ext = '.js') {
11 let result = `${filename}`;
12 if (!path.extname(filename))
13 result += ext;
14 return result;
15};
16
17function walk(ast, { enter, leave }) {
18 return visit(ast, null, enter, leave);
19}
20
21let should_skip = false;
22let should_remove = false;
23let replacement = null;
24const context = {
25 skip: () => should_skip = true,
26 remove: () => should_remove = true,
27 replace: (node) => replacement = node
28};
29
30function replace(parent, prop, index, node) {
31 if (parent) {
32 if (index !== null) {
33 parent[prop][index] = node;
34 } else {
35 parent[prop] = node;
36 }
37 }
38}
39
40function remove(parent, prop, index) {
41 if (parent) {
42 if (index !== null) {
43 parent[prop].splice(index, 1);
44 } else {
45 delete parent[prop];
46 }
47 }
48}
49
50function visit(
51 node,
52 parent,
53 enter,
54 leave,
55 prop,
56 index
57) {
58 if (node) {
59 if (enter) {
60 const _should_skip = should_skip;
61 const _should_remove = should_remove;
62 const _replacement = replacement;
63 should_skip = false;
64 should_remove = false;
65 replacement = null;
66
67 enter.call(context, node, parent, prop, index);
68
69 if (replacement) {
70 node = replacement;
71 replace(parent, prop, index, node);
72 }
73
74 if (should_remove) {
75 remove(parent, prop, index);
76 }
77
78 const skipped = should_skip;
79 const removed = should_remove;
80
81 should_skip = _should_skip;
82 should_remove = _should_remove;
83 replacement = _replacement;
84
85 if (skipped) return node;
86 if (removed) return null;
87 }
88
89 for (const key in node) {
90 const value = (node )[key];
91
92 if (typeof value !== 'object') {
93 continue;
94 }
95
96 else if (Array.isArray(value)) {
97 for (let j = 0, k = 0; j < value.length; j += 1, k += 1) {
98 if (value[j] !== null && typeof value[j].type === 'string') {
99 if (!visit(value[j], node, enter, leave, key, k)) {
100 // removed
101 j--;
102 }
103 }
104 }
105 }
106
107 else if (value !== null && typeof value.type === 'string') {
108 visit(value, node, enter, leave, key, null);
109 }
110 }
111
112 if (leave) {
113 const _replacement = replacement;
114 const _should_remove = should_remove;
115 replacement = null;
116 should_remove = false;
117
118 leave.call(context, node, parent, prop, index);
119
120 if (replacement) {
121 node = replacement;
122 replace(parent, prop, index, node);
123 }
124
125 if (should_remove) {
126 remove(parent, prop, index);
127 }
128
129 const removed = should_remove;
130
131 replacement = _replacement;
132 should_remove = _should_remove;
133
134 if (removed) return null;
135 }
136 }
137
138 return node;
139}
140
141const extractors = {
142 ArrayPattern(names, param) {
143 for (const element of param.elements) {
144 if (element)
145 extractors[element.type](names, element);
146 }
147 },
148 AssignmentPattern(names, param) {
149 extractors[param.left.type](names, param.left);
150 },
151 Identifier(names, param) {
152 names.push(param.name);
153 },
154 MemberExpression() { },
155 ObjectPattern(names, param) {
156 for (const prop of param.properties) {
157 // @ts-ignore Typescript reports that this is not a valid type
158 if (prop.type === 'RestElement') {
159 extractors.RestElement(names, prop);
160 }
161 else {
162 extractors[prop.value.type](names, prop.value);
163 }
164 }
165 },
166 RestElement(names, param) {
167 extractors[param.argument.type](names, param.argument);
168 }
169};
170const extractAssignedNames = function extractAssignedNames(param) {
171 const names = [];
172 extractors[param.type](names, param);
173 return names;
174};
175
176const blockDeclarations = {
177 const: true,
178 let: true
179};
180class Scope {
181 constructor(options = {}) {
182 this.parent = options.parent;
183 this.isBlockScope = !!options.block;
184 this.declarations = Object.create(null);
185 if (options.params) {
186 options.params.forEach((param) => {
187 extractAssignedNames(param).forEach((name) => {
188 this.declarations[name] = true;
189 });
190 });
191 }
192 }
193 addDeclaration(node, isBlockDeclaration, isVar) {
194 if (!isBlockDeclaration && this.isBlockScope) {
195 // it's a `var` or function node, and this
196 // is a block scope, so we need to go up
197 this.parent.addDeclaration(node, isBlockDeclaration, isVar);
198 }
199 else if (node.id) {
200 extractAssignedNames(node.id).forEach((name) => {
201 this.declarations[name] = true;
202 });
203 }
204 }
205 contains(name) {
206 return this.declarations[name] || (this.parent ? this.parent.contains(name) : false);
207 }
208}
209const attachScopes = function attachScopes(ast, propertyName = 'scope') {
210 let scope = new Scope();
211 walk(ast, {
212 enter(n, parent) {
213 const node = n;
214 // function foo () {...}
215 // class Foo {...}
216 if (/(Function|Class)Declaration/.test(node.type)) {
217 scope.addDeclaration(node, false, false);
218 }
219 // var foo = 1
220 if (node.type === 'VariableDeclaration') {
221 const { kind } = node;
222 const isBlockDeclaration = blockDeclarations[kind];
223 // don't add const/let declarations in the body of a for loop #113
224 const parentType = parent ? parent.type : '';
225 if (!(isBlockDeclaration && /ForOfStatement/.test(parentType))) {
226 node.declarations.forEach((declaration) => {
227 scope.addDeclaration(declaration, isBlockDeclaration, true);
228 });
229 }
230 }
231 let newScope;
232 // create new function scope
233 if (/Function/.test(node.type)) {
234 const func = node;
235 newScope = new Scope({
236 parent: scope,
237 block: false,
238 params: func.params
239 });
240 // named function expressions - the name is considered
241 // part of the function's scope
242 if (func.type === 'FunctionExpression' && func.id) {
243 newScope.addDeclaration(func, false, false);
244 }
245 }
246 // create new block scope
247 if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) {
248 newScope = new Scope({
249 parent: scope,
250 block: true
251 });
252 }
253 // catch clause has its own block scope
254 if (node.type === 'CatchClause') {
255 newScope = new Scope({
256 parent: scope,
257 params: node.param ? [node.param] : [],
258 block: true
259 });
260 }
261 if (newScope) {
262 Object.defineProperty(node, propertyName, {
263 value: newScope,
264 configurable: true
265 });
266 scope = newScope;
267 }
268 },
269 leave(n) {
270 const node = n;
271 if (node[propertyName])
272 scope = scope.parent;
273 }
274 });
275 return scope;
276};
277
278// Helper since Typescript can't detect readonly arrays with Array.isArray
279function isArray(arg) {
280 return Array.isArray(arg);
281}
282function ensureArray(thing) {
283 if (isArray(thing))
284 return thing;
285 if (thing == null)
286 return [];
287 return [thing];
288}
289
290function getMatcherString(id, resolutionBase) {
291 if (resolutionBase === false) {
292 return id;
293 }
294 // resolve('') is valid and will default to process.cwd()
295 const basePath = path.resolve(resolutionBase || '')
296 .split(path.sep)
297 .join('/')
298 // escape all possible (posix + win) path characters that might interfere with regex
299 .replace(/[-^$*+?.()|[\]{}]/g, '\\$&');
300 // Note that we use posix.join because:
301 // 1. the basePath has been normalized to use /
302 // 2. the incoming glob (id) matcher, also uses /
303 // otherwise Node will force backslash (\) on windows
304 return path.posix.join(basePath, id);
305}
306const createFilter = function createFilter(include, exclude, options) {
307 const resolutionBase = options && options.resolve;
308 const getMatcher = (id) => id instanceof RegExp
309 ? id
310 : {
311 test: (what) => {
312 // this refactor is a tad overly verbose but makes for easy debugging
313 const pattern = getMatcherString(id, resolutionBase);
314 const fn = pm(pattern, { dot: true });
315 const result = fn(what);
316 return result;
317 }
318 };
319 const includeMatchers = ensureArray(include).map(getMatcher);
320 const excludeMatchers = ensureArray(exclude).map(getMatcher);
321 return function result(id) {
322 if (typeof id !== 'string')
323 return false;
324 if (/\0/.test(id))
325 return false;
326 const pathId = id.split(path.sep).join('/');
327 for (let i = 0; i < excludeMatchers.length; ++i) {
328 const matcher = excludeMatchers[i];
329 if (matcher.test(pathId))
330 return false;
331 }
332 for (let i = 0; i < includeMatchers.length; ++i) {
333 const matcher = includeMatchers[i];
334 if (matcher.test(pathId))
335 return true;
336 }
337 return !includeMatchers.length;
338 };
339};
340
341const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
342const builtins = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl';
343const forbiddenIdentifiers = new Set(`${reservedWords} ${builtins}`.split(' '));
344forbiddenIdentifiers.add('');
345const makeLegalIdentifier = function makeLegalIdentifier(str) {
346 let identifier = str
347 .replace(/-(\w)/g, (_, letter) => letter.toUpperCase())
348 .replace(/[^$_a-zA-Z0-9]/g, '_');
349 if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
350 identifier = `_${identifier}`;
351 }
352 return identifier || '_';
353};
354
355function stringify(obj) {
356 return (JSON.stringify(obj) || 'undefined').replace(/[\u2028\u2029]/g, (char) => `\\u${`000${char.charCodeAt(0).toString(16)}`.slice(-4)}`);
357}
358function serializeArray(arr, indent, baseIndent) {
359 let output = '[';
360 const separator = indent ? `\n${baseIndent}${indent}` : '';
361 for (let i = 0; i < arr.length; i++) {
362 const key = arr[i];
363 output += `${i > 0 ? ',' : ''}${separator}${serialize(key, indent, baseIndent + indent)}`;
364 }
365 return `${output}${indent ? `\n${baseIndent}` : ''}]`;
366}
367function serializeObject(obj, indent, baseIndent) {
368 let output = '{';
369 const separator = indent ? `\n${baseIndent}${indent}` : '';
370 const entries = Object.entries(obj);
371 for (let i = 0; i < entries.length; i++) {
372 const [key, value] = entries[i];
373 const stringKey = makeLegalIdentifier(key) === key ? key : stringify(key);
374 output += `${i > 0 ? ',' : ''}${separator}${stringKey}:${indent ? ' ' : ''}${serialize(value, indent, baseIndent + indent)}`;
375 }
376 return `${output}${indent ? `\n${baseIndent}` : ''}}`;
377}
378function serialize(obj, indent, baseIndent) {
379 if (obj === Infinity)
380 return 'Infinity';
381 if (obj === -Infinity)
382 return '-Infinity';
383 if (obj === 0 && 1 / obj === -Infinity)
384 return '-0';
385 if (obj instanceof Date)
386 return `new Date(${obj.getTime()})`;
387 if (obj instanceof RegExp)
388 return obj.toString();
389 if (obj !== obj)
390 return 'NaN'; // eslint-disable-line no-self-compare
391 if (Array.isArray(obj))
392 return serializeArray(obj, indent, baseIndent);
393 if (obj === null)
394 return 'null';
395 if (typeof obj === 'object')
396 return serializeObject(obj, indent, baseIndent);
397 return stringify(obj);
398}
399const dataToEsm = function dataToEsm(data, options = {}) {
400 const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
401 const _ = options.compact ? '' : ' ';
402 const n = options.compact ? '' : '\n';
403 const declarationType = options.preferConst ? 'const' : 'var';
404 if (options.namedExports === false ||
405 typeof data !== 'object' ||
406 Array.isArray(data) ||
407 data instanceof Date ||
408 data instanceof RegExp ||
409 data === null) {
410 const code = serialize(data, options.compact ? null : t, '');
411 const magic = _ || (/^[{[\-\/]/.test(code) ? '' : ' '); // eslint-disable-line no-useless-escape
412 return `export default${magic}${code};`;
413 }
414 let namedExportCode = '';
415 const defaultExportRows = [];
416 for (const [key, value] of Object.entries(data)) {
417 if (key === makeLegalIdentifier(key)) {
418 if (options.objectShorthand)
419 defaultExportRows.push(key);
420 else
421 defaultExportRows.push(`${key}:${_}${key}`);
422 namedExportCode += `export ${declarationType} ${key}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`;
423 }
424 else {
425 defaultExportRows.push(`${stringify(key)}:${_}${serialize(value, options.compact ? null : t, '')}`);
426 }
427 }
428 return `${namedExportCode}export default${_}{${n}${t}${defaultExportRows.join(`,${n}${t}`)}${n}};${n}`;
429};
430
431// TODO: remove this in next major
432var index = {
433 addExtension,
434 attachScopes,
435 createFilter,
436 dataToEsm,
437 extractAssignedNames,
438 makeLegalIdentifier
439};
440
441exports.addExtension = addExtension;
442exports.attachScopes = attachScopes;
443exports.createFilter = createFilter;
444exports.dataToEsm = dataToEsm;
445exports.default = index;
446exports.extractAssignedNames = extractAssignedNames;
447exports.makeLegalIdentifier = makeLegalIdentifier;
Note: See TracBrowser for help on using the repository browser.