source: frontend/node_modules/webpack/lib/util/property.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: 1.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const SAFE_IDENTIFIER = /^[_a-z$][_a-z$0-9]*$/i;
9const RESERVED_IDENTIFIER = new Set([
10 "break",
11 "case",
12 "catch",
13 "class",
14 "const",
15 "continue",
16 "debugger",
17 "default",
18 "delete",
19 "do",
20 "else",
21 "export",
22 "extends",
23 "finally",
24 "for",
25 "function",
26 "if",
27 "import",
28 "in",
29 "instanceof",
30 "new",
31 "return",
32 "super",
33 "switch",
34 "this",
35 "throw",
36 "try",
37 "typeof",
38 "var",
39 "void",
40 "while",
41 "with",
42 "enum",
43 // strict mode
44 "implements",
45 "interface",
46 "let",
47 "package",
48 "private",
49 "protected",
50 "public",
51 "static",
52 "yield",
53 // module code
54 "await",
55 // skip future reserved keywords defined under ES1 till ES3
56 // additional
57 "null",
58 "true",
59 "false"
60]);
61
62/**
63 * @summary Returns a valid JS property name for the given property.
64 * Certain strings like "default", "null", and names with whitespace are not
65 * valid JS property names, so they are returned as strings.
66 * @param {string} prop property name to analyze
67 * @returns {string} valid JS property name
68 */
69const propertyName = (prop) => {
70 if (SAFE_IDENTIFIER.test(prop) && !RESERVED_IDENTIFIER.has(prop)) {
71 return prop;
72 }
73 return JSON.stringify(prop);
74};
75
76/**
77 * Returns chain of property accesses.
78 * @param {ArrayLike<string>} properties properties
79 * @param {number} start start index
80 * @returns {string} chain of property accesses
81 */
82const propertyAccess = (properties, start = 0) => {
83 let str = "";
84 for (let i = start; i < properties.length; i++) {
85 const p = properties[i];
86 if (`${Number(p)}` === p) {
87 str += `[${p}]`;
88 } else if (SAFE_IDENTIFIER.test(p) && !RESERVED_IDENTIFIER.has(p)) {
89 str += `.${p}`;
90 } else {
91 str += `[${JSON.stringify(p)}]`;
92 }
93 }
94 return str;
95};
96
97module.exports = {
98 RESERVED_IDENTIFIER,
99 SAFE_IDENTIFIER,
100 propertyAccess,
101 propertyName
102};
Note: See TracBrowser for help on using the repository browser.