@locker/ast-lib-maker
Advanced tools
Comparing version 0.14.4 to 0.14.5
@@ -6,230 +6,296 @@ /*! | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var shared = require('@locker/shared'); | ||
const ASTERISK_CHARACTER = '*'; | ||
const DOCUMENT_ALIASES = [`${ASTERISK_CHARACTER}.ownerDocument`, 'document']; | ||
const DOCUMENT_ALIASES = ["".concat(ASTERISK_CHARACTER, ".ownerDocument"), 'document']; | ||
const WINDOW_ALIASES = ['document.defaultView', 'frames', 'globalThis', 'self', 'window']; | ||
const expandPatternsCache = new Map(); | ||
function dehydratePattern(pattern) { | ||
return shared.ArrayIsArray(pattern) ? pattern.join('.') : String(pattern); | ||
return shared.ArrayIsArray(pattern) ? pattern.join('.') : String(pattern); | ||
} | ||
function ensureArray(value) { | ||
return shared.ArrayIsArray(value) ? value : [value]; | ||
return shared.ArrayIsArray(value) ? value : [value]; | ||
} | ||
function expandPattern(pattern) { | ||
const expandedPatterns = [rehydratePattern(normalizePattern(pattern))]; | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
function push(stringPattern) { | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(rehydratePattern(stringPattern)); | ||
} | ||
const expandedPatterns = [rehydratePattern(normalizePattern(pattern))]; | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
function push(stringPattern) { | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(rehydratePattern(stringPattern)); | ||
} | ||
function pushForEachAlias(aliases, pathTail) { | ||
for (let i = 0, { length } = aliases; i < length; i += 1) { | ||
push(`${aliases[i]}.${pathTail}`); | ||
} | ||
} | ||
function pushForEachAlias(aliases, pathTail) { | ||
for (let i = 0, { | ||
length | ||
} = aliases; i < length; i += 1) { | ||
push("".concat(aliases[i], ".").concat(pathTail)); | ||
} | ||
const { length } = dehydratedPatterns; | ||
for (let i = 0; i < length; i += 1) { | ||
const stringPattern = dehydratedPatterns[i]; | ||
// Skip empty patterns and those starting with '*.'. | ||
if (stringPattern === '' || stringPatternStartsWith(stringPattern, ASTERISK_CHARACTER)) { | ||
continue; | ||
} | ||
const topLevelPath = isWindowStringPattern(stringPattern) | ||
? stringPatternTail(stringPattern) | ||
: stringPattern; | ||
// Expand patterns with a top-level identifier. | ||
push(topLevelPath); | ||
// Expand patterns with other `window` aliases. | ||
pushForEachAlias(WINDOW_ALIASES, topLevelPath); | ||
// Expand patterns with other `document` aliases. | ||
if (isDocumentStringPattern(topLevelPath)) { | ||
pushForEachAlias(DOCUMENT_ALIASES, stringPatternTail(topLevelPath)); | ||
} | ||
} | ||
const { | ||
length | ||
} = dehydratedPatterns; | ||
for (let i = 0; i < length; i += 1) { | ||
const stringPattern = dehydratedPatterns[i]; // Skip empty patterns and those starting with '*.'. | ||
if (stringPattern === '' || stringPatternStartsWith(stringPattern, ASTERISK_CHARACTER)) { | ||
continue; | ||
} | ||
return expandedPatterns; | ||
const topLevelPath = isWindowStringPattern(stringPattern) ? stringPatternTail(stringPattern) : stringPattern; // Expand patterns with a top-level identifier. | ||
push(topLevelPath); // Expand patterns with other `window` aliases. | ||
pushForEachAlias(WINDOW_ALIASES, topLevelPath); // Expand patterns with other `document` aliases. | ||
if (isDocumentStringPattern(topLevelPath)) { | ||
pushForEachAlias(DOCUMENT_ALIASES, stringPatternTail(topLevelPath)); | ||
} | ||
} | ||
return expandedPatterns; | ||
} | ||
function isDocumentStringPattern(stringPattern) { | ||
return stringPatternStartsWithByAliases(stringPattern, DOCUMENT_ALIASES); | ||
return stringPatternStartsWithByAliases(stringPattern, DOCUMENT_ALIASES); | ||
} | ||
function isNodeLike(value) { | ||
return shared.isObject(value) && typeof value.type === 'string'; | ||
return shared.isObject(value) && typeof value.type === 'string'; | ||
} | ||
function isWindowStringPattern(stringPattern) { | ||
return stringPatternStartsWithByAliases(stringPattern, WINDOW_ALIASES); | ||
return stringPatternStartsWithByAliases(stringPattern, WINDOW_ALIASES); | ||
} | ||
function normalizePattern(pattern) { | ||
return shared.ArrayIsArray(pattern) ? pattern.map(removeWhitespace) : removeWhitespace(pattern); | ||
return shared.ArrayIsArray(pattern) ? pattern.map(removeWhitespace) : removeWhitespace(pattern); | ||
} | ||
function rehydratePattern(pattern) { | ||
return shared.ArrayIsArray(pattern) ? pattern : String(pattern).split('.'); | ||
return shared.ArrayIsArray(pattern) ? pattern : String(pattern).split('.'); | ||
} | ||
function removeWhitespace(string) { | ||
return String(string).replace(/\s/g, ''); | ||
return String(string).replace(/\s/g, ''); | ||
} | ||
function stringPatternStartsWith(stringPattern, searchString) { | ||
return stringPattern.startsWith(`${searchString}.`); | ||
return stringPattern.startsWith("".concat(searchString, ".")); | ||
} | ||
function stringPatternStartsWithByAliases(stringPattern, aliases) { | ||
return aliases.some((alias) => stringPatternStartsWith(stringPattern, alias)); | ||
return aliases.some(alias => stringPatternStartsWith(stringPattern, alias)); | ||
} | ||
function stringPatternTail(stringPattern) { | ||
return stringPattern.slice(stringPattern.indexOf('.') + 1); | ||
return stringPattern.slice(stringPattern.indexOf('.') + 1); | ||
} | ||
const baseLib = { | ||
/** | ||
* String patterns are converted to arrays and expanded for document and | ||
* window aliases. For example: | ||
* ```js | ||
* ['window.top'] | ||
* ``` | ||
* expands to: | ||
* ```js | ||
* [ | ||
* ['window', 'top'], | ||
* ['top'], | ||
* ['document', 'defaultView', 'top'], | ||
* ['frames', 'top'], | ||
* ['globalThis', 'top'], | ||
* ['self', 'top'] | ||
* ] | ||
* ``` | ||
*/ | ||
expandPatterns(patterns) { | ||
const cacheKey = shared.JSONStringify(patterns); | ||
const cachedValue = expandPatternsCache.get(cacheKey); | ||
if (cachedValue !== undefined) { | ||
return shared.JSONParse(cachedValue); | ||
/** | ||
* String patterns are converted to arrays and expanded for document and | ||
* window aliases. For example: | ||
* ```js | ||
* ['window.top'] | ||
* ``` | ||
* expands to: | ||
* ```js | ||
* [ | ||
* ['window', 'top'], | ||
* ['top'], | ||
* ['document', 'defaultView', 'top'], | ||
* ['frames', 'top'], | ||
* ['globalThis', 'top'], | ||
* ['self', 'top'] | ||
* ] | ||
* ``` | ||
*/ | ||
expandPatterns(patterns) { | ||
const cacheKey = shared.JSONStringify(patterns); | ||
const cachedValue = expandPatternsCache.get(cacheKey); | ||
if (cachedValue !== undefined) { | ||
return shared.JSONParse(cachedValue); | ||
} | ||
const expandedPatterns = patterns.map(pattern => rehydratePattern(normalizePattern(pattern)) // eslint-disable-next-line function-paren-newline | ||
); | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
for (let i = 0, { | ||
length | ||
} = dehydratedPatterns; i < length; i += 1) { | ||
const arrayPatterns = expandPattern(dehydratedPatterns[i]); // eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { | ||
length: length_j | ||
} = arrayPatterns; j < length_j; j += 1) { | ||
const arrayPattern = arrayPatterns[j]; | ||
const stringPattern = dehydratePattern(arrayPattern); | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(arrayPattern); | ||
} | ||
const expandedPatterns = patterns.map((pattern) => rehydratePattern(normalizePattern(pattern)) | ||
// eslint-disable-next-line function-paren-newline | ||
); | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
for (let i = 0, { length } = dehydratedPatterns; i < length; i += 1) { | ||
const arrayPatterns = expandPattern(dehydratedPatterns[i]); | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { length: length_j } = arrayPatterns; j < length_j; j += 1) { | ||
const arrayPattern = arrayPatterns[j]; | ||
const stringPattern = dehydratePattern(arrayPattern); | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(arrayPattern); | ||
} | ||
} | ||
} | ||
} | ||
expandPatternsCache.set(cacheKey, shared.JSONStringify(expandedPatterns)); | ||
return expandedPatterns; | ||
}, | ||
getIdentifierName(node) { | ||
return node.name; | ||
}, | ||
getParentNode(node) { | ||
return isNodeLike(node) ? node.parent : undefined; | ||
}, | ||
isMatchableNode(node) { | ||
return this.isNodeOfType(node, 'Identifier') || this.isNodeOfType(node, 'ThisExpression'); | ||
}, | ||
isNodeByAsteriskOrName(node, name) { | ||
if (this.isMatchableNode(node)) { | ||
return name === ASTERISK_CHARACTER || node.type === 'ThisExpression' || name === this.getIdentifierName(node); | ||
} | ||
return false; | ||
}, | ||
isNodeOfType(node, type) { | ||
return isNodeLike(node) && node.type === type; | ||
}, | ||
isNonComputedMemberExpressionNode(node) { | ||
return this.isNodeOfType(node, 'MemberExpression') && node.computed === false; | ||
}, | ||
match(identifier, patterns) { | ||
const expandedPatterns = this.expandPatterns(ensureArray(patterns)); | ||
expandedLoop: for (let i = 0, { | ||
length | ||
} = expandedPatterns; i < length; i += 1) { | ||
const arrayPattern = expandedPatterns[i]; // Skip fast for mismatched identifiers. | ||
if (!this.isNodeByAsteriskOrName(identifier, arrayPattern[0])) { | ||
continue; | ||
} // Match and exit early for non-member identifier matches. | ||
if (arrayPattern.length === 1) { | ||
return { | ||
identifier, | ||
node: identifier, | ||
pattern: arrayPattern.join('.') | ||
}; | ||
} | ||
let currentNode = identifier; // eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { | ||
length: length_j | ||
} = arrayPattern; j < length_j; j += 1) { | ||
currentNode = this.getParentNode(currentNode); // Skip for unexpected nodes. | ||
if (!this.isNonComputedMemberExpressionNode(currentNode)) { | ||
continue expandedLoop; | ||
} | ||
expandPatternsCache.set(cacheKey, shared.JSONStringify(expandedPatterns)); | ||
return expandedPatterns; | ||
}, | ||
getIdentifierName(node) { | ||
return node.name; | ||
}, | ||
getParentNode(node) { | ||
return isNodeLike(node) ? node.parent : undefined; | ||
}, | ||
isMatchableNode(node) { | ||
return this.isNodeOfType(node, 'Identifier') || this.isNodeOfType(node, 'ThisExpression'); | ||
}, | ||
isNodeByAsteriskOrName(node, name) { | ||
if (this.isMatchableNode(node)) { | ||
return (name === ASTERISK_CHARACTER || | ||
node.type === 'ThisExpression' || | ||
name === this.getIdentifierName(node)); | ||
} | ||
return false; | ||
}, | ||
isNodeOfType(node, type) { | ||
return isNodeLike(node) && node.type === type; | ||
}, | ||
isNonComputedMemberExpressionNode(node) { | ||
return (this.isNodeOfType(node, 'MemberExpression') && | ||
node.computed === false); | ||
}, | ||
match(identifier, patterns) { | ||
const expandedPatterns = this.expandPatterns(ensureArray(patterns)); | ||
expandedLoop: for (let i = 0, { length } = expandedPatterns; i < length; i += 1) { | ||
const arrayPattern = expandedPatterns[i]; | ||
// Skip fast for mismatched identifiers. | ||
if (!this.isNodeByAsteriskOrName(identifier, arrayPattern[0])) { | ||
continue; | ||
const { | ||
object, | ||
property | ||
} = currentNode; | ||
let matchedByPreviousAsterisk = false; | ||
if (this.isMatchableNode(object)) { | ||
// Skip for mismatched object identifiers. | ||
if (!this.isNodeByAsteriskOrName(object, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
// Match and exit early for non-member identifier matches. | ||
if (arrayPattern.length === 1) { | ||
return { | ||
identifier, | ||
node: identifier, | ||
pattern: arrayPattern.join('.'), | ||
}; | ||
} | ||
let currentNode = identifier; | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { length: length_j } = arrayPattern; j < length_j; j += 1) { | ||
currentNode = this.getParentNode(currentNode); | ||
// Skip for unexpected nodes. | ||
if (!this.isNonComputedMemberExpressionNode(currentNode)) { | ||
continue expandedLoop; | ||
} | ||
const { object, property } = currentNode; | ||
let matchedByPreviousAsterisk = false; | ||
if (this.isMatchableNode(object)) { | ||
// Skip for mismatched object identifiers. | ||
if (!this.isNodeByAsteriskOrName(object, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
} | ||
if (!matchedByPreviousAsterisk) { | ||
j += 1; | ||
} | ||
} | ||
// Skip nodes that aren't part of a property chain. | ||
else if (!this.isNonComputedMemberExpressionNode(object)) { | ||
continue expandedLoop; | ||
} | ||
// Skip for mismatched property identifiers. | ||
if (!this.isNodeByAsteriskOrName(property, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
} | ||
if (matchedByPreviousAsterisk) { | ||
j -= 1; | ||
} | ||
} | ||
// If we've made it this far it's a match! | ||
return { | ||
identifier, | ||
node: currentNode, | ||
pattern: arrayPattern.join('.'), | ||
}; | ||
} | ||
if (!matchedByPreviousAsterisk) { | ||
j += 1; | ||
} | ||
} // Skip nodes that aren't part of a property chain. | ||
else if (!this.isNonComputedMemberExpressionNode(object)) { | ||
continue expandedLoop; | ||
} // Skip for mismatched property identifiers. | ||
if (!this.isNodeByAsteriskOrName(property, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
} | ||
return undefined; | ||
}, | ||
matchAll(identifiers, patterns) { | ||
const matches = []; | ||
for (let i = 0, { length } = identifiers; i < length; i += 1) { | ||
const identifier = identifiers[i]; | ||
const matchData = this.match(identifier, patterns); | ||
if (matchData !== undefined) { | ||
matches.push(matchData); | ||
} | ||
if (matchedByPreviousAsterisk) { | ||
j -= 1; | ||
} | ||
return matches; | ||
}, | ||
} // If we've made it this far it's a match! | ||
return { | ||
identifier, | ||
node: currentNode, | ||
pattern: arrayPattern.join('.') | ||
}; | ||
} | ||
return undefined; | ||
}, | ||
matchAll(identifiers, patterns) { | ||
const matches = []; | ||
for (let i = 0, { | ||
length | ||
} = identifiers; i < length; i += 1) { | ||
const identifier = identifiers[i]; | ||
const matchData = this.match(identifier, patterns); | ||
if (matchData !== undefined) { | ||
matches.push(matchData); | ||
} | ||
} | ||
return matches; | ||
} | ||
}; | ||
function createLib(overrides) { | ||
return { | ||
// @ts-ignore | ||
__proto__: null, | ||
...baseLib, | ||
...overrides, | ||
}; | ||
return _objectSpread(_objectSpread({ | ||
// @ts-ignore | ||
__proto__: null | ||
}, baseLib), overrides); | ||
} | ||
exports.createLib = createLib; | ||
/*! version: 0.14.4 */ | ||
/*! version: 0.14.5 */ |
/*! | ||
* Copyright (C) 2020 salesforce.com, inc. | ||
*/ | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
import { JSONStringify, JSONParse, ArrayIsArray, isObject } from '@locker/shared'; | ||
const ASTERISK_CHARACTER = '*'; | ||
const DOCUMENT_ALIASES = [`${ASTERISK_CHARACTER}.ownerDocument`, 'document']; | ||
const DOCUMENT_ALIASES = ["".concat(ASTERISK_CHARACTER, ".ownerDocument"), 'document']; | ||
const WINDOW_ALIASES = ['document.defaultView', 'frames', 'globalThis', 'self', 'window']; | ||
const expandPatternsCache = new Map(); | ||
function dehydratePattern(pattern) { | ||
return ArrayIsArray(pattern) ? pattern.join('.') : String(pattern); | ||
return ArrayIsArray(pattern) ? pattern.join('.') : String(pattern); | ||
} | ||
function ensureArray(value) { | ||
return ArrayIsArray(value) ? value : [value]; | ||
return ArrayIsArray(value) ? value : [value]; | ||
} | ||
function expandPattern(pattern) { | ||
const expandedPatterns = [rehydratePattern(normalizePattern(pattern))]; | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
function push(stringPattern) { | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(rehydratePattern(stringPattern)); | ||
} | ||
const expandedPatterns = [rehydratePattern(normalizePattern(pattern))]; | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
function push(stringPattern) { | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(rehydratePattern(stringPattern)); | ||
} | ||
function pushForEachAlias(aliases, pathTail) { | ||
for (let i = 0, { length } = aliases; i < length; i += 1) { | ||
push(`${aliases[i]}.${pathTail}`); | ||
} | ||
} | ||
function pushForEachAlias(aliases, pathTail) { | ||
for (let i = 0, { | ||
length | ||
} = aliases; i < length; i += 1) { | ||
push("".concat(aliases[i], ".").concat(pathTail)); | ||
} | ||
const { length } = dehydratedPatterns; | ||
for (let i = 0; i < length; i += 1) { | ||
const stringPattern = dehydratedPatterns[i]; | ||
// Skip empty patterns and those starting with '*.'. | ||
if (stringPattern === '' || stringPatternStartsWith(stringPattern, ASTERISK_CHARACTER)) { | ||
continue; | ||
} | ||
const topLevelPath = isWindowStringPattern(stringPattern) | ||
? stringPatternTail(stringPattern) | ||
: stringPattern; | ||
// Expand patterns with a top-level identifier. | ||
push(topLevelPath); | ||
// Expand patterns with other `window` aliases. | ||
pushForEachAlias(WINDOW_ALIASES, topLevelPath); | ||
// Expand patterns with other `document` aliases. | ||
if (isDocumentStringPattern(topLevelPath)) { | ||
pushForEachAlias(DOCUMENT_ALIASES, stringPatternTail(topLevelPath)); | ||
} | ||
} | ||
const { | ||
length | ||
} = dehydratedPatterns; | ||
for (let i = 0; i < length; i += 1) { | ||
const stringPattern = dehydratedPatterns[i]; // Skip empty patterns and those starting with '*.'. | ||
if (stringPattern === '' || stringPatternStartsWith(stringPattern, ASTERISK_CHARACTER)) { | ||
continue; | ||
} | ||
return expandedPatterns; | ||
const topLevelPath = isWindowStringPattern(stringPattern) ? stringPatternTail(stringPattern) : stringPattern; // Expand patterns with a top-level identifier. | ||
push(topLevelPath); // Expand patterns with other `window` aliases. | ||
pushForEachAlias(WINDOW_ALIASES, topLevelPath); // Expand patterns with other `document` aliases. | ||
if (isDocumentStringPattern(topLevelPath)) { | ||
pushForEachAlias(DOCUMENT_ALIASES, stringPatternTail(topLevelPath)); | ||
} | ||
} | ||
return expandedPatterns; | ||
} | ||
function isDocumentStringPattern(stringPattern) { | ||
return stringPatternStartsWithByAliases(stringPattern, DOCUMENT_ALIASES); | ||
return stringPatternStartsWithByAliases(stringPattern, DOCUMENT_ALIASES); | ||
} | ||
function isNodeLike(value) { | ||
return isObject(value) && typeof value.type === 'string'; | ||
return isObject(value) && typeof value.type === 'string'; | ||
} | ||
function isWindowStringPattern(stringPattern) { | ||
return stringPatternStartsWithByAliases(stringPattern, WINDOW_ALIASES); | ||
return stringPatternStartsWithByAliases(stringPattern, WINDOW_ALIASES); | ||
} | ||
function normalizePattern(pattern) { | ||
return ArrayIsArray(pattern) ? pattern.map(removeWhitespace) : removeWhitespace(pattern); | ||
return ArrayIsArray(pattern) ? pattern.map(removeWhitespace) : removeWhitespace(pattern); | ||
} | ||
function rehydratePattern(pattern) { | ||
return ArrayIsArray(pattern) ? pattern : String(pattern).split('.'); | ||
return ArrayIsArray(pattern) ? pattern : String(pattern).split('.'); | ||
} | ||
function removeWhitespace(string) { | ||
return String(string).replace(/\s/g, ''); | ||
return String(string).replace(/\s/g, ''); | ||
} | ||
function stringPatternStartsWith(stringPattern, searchString) { | ||
return stringPattern.startsWith(`${searchString}.`); | ||
return stringPattern.startsWith("".concat(searchString, ".")); | ||
} | ||
function stringPatternStartsWithByAliases(stringPattern, aliases) { | ||
return aliases.some((alias) => stringPatternStartsWith(stringPattern, alias)); | ||
return aliases.some(alias => stringPatternStartsWith(stringPattern, alias)); | ||
} | ||
function stringPatternTail(stringPattern) { | ||
return stringPattern.slice(stringPattern.indexOf('.') + 1); | ||
return stringPattern.slice(stringPattern.indexOf('.') + 1); | ||
} | ||
const baseLib = { | ||
/** | ||
* String patterns are converted to arrays and expanded for document and | ||
* window aliases. For example: | ||
* ```js | ||
* ['window.top'] | ||
* ``` | ||
* expands to: | ||
* ```js | ||
* [ | ||
* ['window', 'top'], | ||
* ['top'], | ||
* ['document', 'defaultView', 'top'], | ||
* ['frames', 'top'], | ||
* ['globalThis', 'top'], | ||
* ['self', 'top'] | ||
* ] | ||
* ``` | ||
*/ | ||
expandPatterns(patterns) { | ||
const cacheKey = JSONStringify(patterns); | ||
const cachedValue = expandPatternsCache.get(cacheKey); | ||
if (cachedValue !== undefined) { | ||
return JSONParse(cachedValue); | ||
/** | ||
* String patterns are converted to arrays and expanded for document and | ||
* window aliases. For example: | ||
* ```js | ||
* ['window.top'] | ||
* ``` | ||
* expands to: | ||
* ```js | ||
* [ | ||
* ['window', 'top'], | ||
* ['top'], | ||
* ['document', 'defaultView', 'top'], | ||
* ['frames', 'top'], | ||
* ['globalThis', 'top'], | ||
* ['self', 'top'] | ||
* ] | ||
* ``` | ||
*/ | ||
expandPatterns(patterns) { | ||
const cacheKey = JSONStringify(patterns); | ||
const cachedValue = expandPatternsCache.get(cacheKey); | ||
if (cachedValue !== undefined) { | ||
return JSONParse(cachedValue); | ||
} | ||
const expandedPatterns = patterns.map(pattern => rehydratePattern(normalizePattern(pattern)) // eslint-disable-next-line function-paren-newline | ||
); | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
for (let i = 0, { | ||
length | ||
} = dehydratedPatterns; i < length; i += 1) { | ||
const arrayPatterns = expandPattern(dehydratedPatterns[i]); // eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { | ||
length: length_j | ||
} = arrayPatterns; j < length_j; j += 1) { | ||
const arrayPattern = arrayPatterns[j]; | ||
const stringPattern = dehydratePattern(arrayPattern); | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(arrayPattern); | ||
} | ||
const expandedPatterns = patterns.map((pattern) => rehydratePattern(normalizePattern(pattern)) | ||
// eslint-disable-next-line function-paren-newline | ||
); | ||
const dehydratedPatterns = expandedPatterns.map(dehydratePattern); | ||
for (let i = 0, { length } = dehydratedPatterns; i < length; i += 1) { | ||
const arrayPatterns = expandPattern(dehydratedPatterns[i]); | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { length: length_j } = arrayPatterns; j < length_j; j += 1) { | ||
const arrayPattern = arrayPatterns[j]; | ||
const stringPattern = dehydratePattern(arrayPattern); | ||
if (!dehydratedPatterns.includes(stringPattern)) { | ||
dehydratedPatterns.push(stringPattern); | ||
expandedPatterns.push(arrayPattern); | ||
} | ||
} | ||
} | ||
} | ||
expandPatternsCache.set(cacheKey, JSONStringify(expandedPatterns)); | ||
return expandedPatterns; | ||
}, | ||
getIdentifierName(node) { | ||
return node.name; | ||
}, | ||
getParentNode(node) { | ||
return isNodeLike(node) ? node.parent : undefined; | ||
}, | ||
isMatchableNode(node) { | ||
return this.isNodeOfType(node, 'Identifier') || this.isNodeOfType(node, 'ThisExpression'); | ||
}, | ||
isNodeByAsteriskOrName(node, name) { | ||
if (this.isMatchableNode(node)) { | ||
return name === ASTERISK_CHARACTER || node.type === 'ThisExpression' || name === this.getIdentifierName(node); | ||
} | ||
return false; | ||
}, | ||
isNodeOfType(node, type) { | ||
return isNodeLike(node) && node.type === type; | ||
}, | ||
isNonComputedMemberExpressionNode(node) { | ||
return this.isNodeOfType(node, 'MemberExpression') && node.computed === false; | ||
}, | ||
match(identifier, patterns) { | ||
const expandedPatterns = this.expandPatterns(ensureArray(patterns)); | ||
expandedLoop: for (let i = 0, { | ||
length | ||
} = expandedPatterns; i < length; i += 1) { | ||
const arrayPattern = expandedPatterns[i]; // Skip fast for mismatched identifiers. | ||
if (!this.isNodeByAsteriskOrName(identifier, arrayPattern[0])) { | ||
continue; | ||
} // Match and exit early for non-member identifier matches. | ||
if (arrayPattern.length === 1) { | ||
return { | ||
identifier, | ||
node: identifier, | ||
pattern: arrayPattern.join('.') | ||
}; | ||
} | ||
let currentNode = identifier; // eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { | ||
length: length_j | ||
} = arrayPattern; j < length_j; j += 1) { | ||
currentNode = this.getParentNode(currentNode); // Skip for unexpected nodes. | ||
if (!this.isNonComputedMemberExpressionNode(currentNode)) { | ||
continue expandedLoop; | ||
} | ||
expandPatternsCache.set(cacheKey, JSONStringify(expandedPatterns)); | ||
return expandedPatterns; | ||
}, | ||
getIdentifierName(node) { | ||
return node.name; | ||
}, | ||
getParentNode(node) { | ||
return isNodeLike(node) ? node.parent : undefined; | ||
}, | ||
isMatchableNode(node) { | ||
return this.isNodeOfType(node, 'Identifier') || this.isNodeOfType(node, 'ThisExpression'); | ||
}, | ||
isNodeByAsteriskOrName(node, name) { | ||
if (this.isMatchableNode(node)) { | ||
return (name === ASTERISK_CHARACTER || | ||
node.type === 'ThisExpression' || | ||
name === this.getIdentifierName(node)); | ||
} | ||
return false; | ||
}, | ||
isNodeOfType(node, type) { | ||
return isNodeLike(node) && node.type === type; | ||
}, | ||
isNonComputedMemberExpressionNode(node) { | ||
return (this.isNodeOfType(node, 'MemberExpression') && | ||
node.computed === false); | ||
}, | ||
match(identifier, patterns) { | ||
const expandedPatterns = this.expandPatterns(ensureArray(patterns)); | ||
expandedLoop: for (let i = 0, { length } = expandedPatterns; i < length; i += 1) { | ||
const arrayPattern = expandedPatterns[i]; | ||
// Skip fast for mismatched identifiers. | ||
if (!this.isNodeByAsteriskOrName(identifier, arrayPattern[0])) { | ||
continue; | ||
const { | ||
object, | ||
property | ||
} = currentNode; | ||
let matchedByPreviousAsterisk = false; | ||
if (this.isMatchableNode(object)) { | ||
// Skip for mismatched object identifiers. | ||
if (!this.isNodeByAsteriskOrName(object, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
// Match and exit early for non-member identifier matches. | ||
if (arrayPattern.length === 1) { | ||
return { | ||
identifier, | ||
node: identifier, | ||
pattern: arrayPattern.join('.'), | ||
}; | ||
} | ||
let currentNode = identifier; | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
for (let j = 0, { length: length_j } = arrayPattern; j < length_j; j += 1) { | ||
currentNode = this.getParentNode(currentNode); | ||
// Skip for unexpected nodes. | ||
if (!this.isNonComputedMemberExpressionNode(currentNode)) { | ||
continue expandedLoop; | ||
} | ||
const { object, property } = currentNode; | ||
let matchedByPreviousAsterisk = false; | ||
if (this.isMatchableNode(object)) { | ||
// Skip for mismatched object identifiers. | ||
if (!this.isNodeByAsteriskOrName(object, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
} | ||
if (!matchedByPreviousAsterisk) { | ||
j += 1; | ||
} | ||
} | ||
// Skip nodes that aren't part of a property chain. | ||
else if (!this.isNonComputedMemberExpressionNode(object)) { | ||
continue expandedLoop; | ||
} | ||
// Skip for mismatched property identifiers. | ||
if (!this.isNodeByAsteriskOrName(property, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
} | ||
if (matchedByPreviousAsterisk) { | ||
j -= 1; | ||
} | ||
} | ||
// If we've made it this far it's a match! | ||
return { | ||
identifier, | ||
node: currentNode, | ||
pattern: arrayPattern.join('.'), | ||
}; | ||
} | ||
if (!matchedByPreviousAsterisk) { | ||
j += 1; | ||
} | ||
} // Skip nodes that aren't part of a property chain. | ||
else if (!this.isNonComputedMemberExpressionNode(object)) { | ||
continue expandedLoop; | ||
} // Skip for mismatched property identifiers. | ||
if (!this.isNodeByAsteriskOrName(property, arrayPattern[j])) { | ||
matchedByPreviousAsterisk = arrayPattern[j - 1] === ASTERISK_CHARACTER; | ||
if (!matchedByPreviousAsterisk) { | ||
continue expandedLoop; | ||
} | ||
} | ||
return undefined; | ||
}, | ||
matchAll(identifiers, patterns) { | ||
const matches = []; | ||
for (let i = 0, { length } = identifiers; i < length; i += 1) { | ||
const identifier = identifiers[i]; | ||
const matchData = this.match(identifier, patterns); | ||
if (matchData !== undefined) { | ||
matches.push(matchData); | ||
} | ||
if (matchedByPreviousAsterisk) { | ||
j -= 1; | ||
} | ||
return matches; | ||
}, | ||
} // If we've made it this far it's a match! | ||
return { | ||
identifier, | ||
node: currentNode, | ||
pattern: arrayPattern.join('.') | ||
}; | ||
} | ||
return undefined; | ||
}, | ||
matchAll(identifiers, patterns) { | ||
const matches = []; | ||
for (let i = 0, { | ||
length | ||
} = identifiers; i < length; i += 1) { | ||
const identifier = identifiers[i]; | ||
const matchData = this.match(identifier, patterns); | ||
if (matchData !== undefined) { | ||
matches.push(matchData); | ||
} | ||
} | ||
return matches; | ||
} | ||
}; | ||
function createLib(overrides) { | ||
return { | ||
// @ts-ignore | ||
__proto__: null, | ||
...baseLib, | ||
...overrides, | ||
}; | ||
return _objectSpread(_objectSpread({ | ||
// @ts-ignore | ||
__proto__: null | ||
}, baseLib), overrides); | ||
} | ||
export { createLib }; | ||
/*! version: 0.14.4 */ | ||
/*! version: 0.14.5 */ |
{ | ||
"name": "@locker/ast-lib-maker", | ||
"version": "0.14.4", | ||
"version": "0.14.5", | ||
"license": "Salesforce Developer Agreement", | ||
@@ -24,3 +24,3 @@ "author": "Salesforce UI Security Team", | ||
"dependencies": { | ||
"@locker/shared": "0.14.4" | ||
"@locker/shared": "0.14.5" | ||
}, | ||
@@ -34,3 +34,3 @@ "devDependencies": { | ||
], | ||
"gitHead": "775de69b0fe1bb6cc739abc304e56b552c1f85d2" | ||
"gitHead": "257009273b0c98354e0fafd9d4e81d16664bad2e" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27390
529
+ Added@locker/shared@0.14.5(transitive)
- Removed@locker/shared@0.14.4(transitive)
Updated@locker/shared@0.14.5