webpack/lib/util/propertyAccess.js

31 lines
727 B
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
2024-07-31 06:15:03 +08:00
const { SAFE_IDENTIFIER, RESERVED_IDENTIFIER } = require("./propertyName");
2023-05-01 04:51:59 +08:00
/**
2023-05-01 06:17:37 +08:00
* @param {ArrayLike<string>} properties properties
2023-05-01 04:51:59 +08:00
* @param {number} start start index
* @returns {string} chain of property accesses
*/
const propertyAccess = (properties, start = 0) => {
let str = "";
for (let i = start; i < properties.length; i++) {
const p = properties[i];
2024-07-31 11:11:11 +08:00
if (`${Number(p)}` === p) {
str += `[${p}]`;
2021-10-13 11:01:46 +08:00
} else if (SAFE_IDENTIFIER.test(p) && !RESERVED_IDENTIFIER.has(p)) {
str += `.${p}`;
} else {
str += `[${JSON.stringify(p)}]`;
}
}
return str;
};
module.exports = propertyAccess;