mirror of
https://github.com/joelwmale/webhook-action.git
synced 2024-12-01 20:05:21 +01:00
160 lines
No EOL
20 KiB
JavaScript
160 lines
No EOL
20 KiB
JavaScript
'use strict';
|
|
|
|
|
|
|
|
|
|
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _toConsumableArray(arr) {if (Array.isArray(arr)) {for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];return arr2;} else {return Array.from(arr);}} /**
|
|
* @fileoverview Rule to disallow namespace import
|
|
* @author Radek Benkel
|
|
*/ //------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = {
|
|
meta: {
|
|
type: 'suggestion',
|
|
docs: {
|
|
url: (0, _docsUrl2.default)('no-namespace') },
|
|
|
|
fixable: 'code',
|
|
schema: [] },
|
|
|
|
|
|
create: function (context) {
|
|
return {
|
|
'ImportNamespaceSpecifier': function (node) {
|
|
const scopeVariables = context.getScope().variables;
|
|
const namespaceVariable = scopeVariables.find(variable =>
|
|
variable.defs[0].node === node);
|
|
|
|
const namespaceReferences = namespaceVariable.references;
|
|
const namespaceIdentifiers = namespaceReferences.map(reference => reference.identifier);
|
|
const canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers);
|
|
|
|
context.report({
|
|
node,
|
|
message: `Unexpected namespace import.`,
|
|
fix: canFix && (fixer => {
|
|
const scopeManager = context.getSourceCode().scopeManager;
|
|
const fixes = [];
|
|
|
|
// Pass 1: Collect variable names that are already in scope for each reference we want
|
|
// to transform, so that we can be sure that we choose non-conflicting import names
|
|
const importNameConflicts = {};
|
|
namespaceIdentifiers.forEach(identifier => {
|
|
const parent = identifier.parent;
|
|
if (parent && parent.type === 'MemberExpression') {
|
|
const importName = getMemberPropertyName(parent);
|
|
const localConflicts = getVariableNamesInScope(scopeManager, parent);
|
|
if (!importNameConflicts[importName]) {
|
|
importNameConflicts[importName] = localConflicts;
|
|
} else {
|
|
localConflicts.forEach(c => importNameConflicts[importName].add(c));
|
|
}
|
|
}
|
|
});
|
|
|
|
// Choose new names for each import
|
|
const importNames = Object.keys(importNameConflicts);
|
|
const importLocalNames = generateLocalNames(
|
|
importNames,
|
|
importNameConflicts,
|
|
namespaceVariable.name);
|
|
|
|
|
|
// Replace the ImportNamespaceSpecifier with a list of ImportSpecifiers
|
|
const namedImportSpecifiers = importNames.map(importName =>
|
|
importName === importLocalNames[importName] ?
|
|
importName :
|
|
`${importName} as ${importLocalNames[importName]}`);
|
|
|
|
fixes.push(fixer.replaceText(node, `{ ${namedImportSpecifiers.join(', ')} }`));
|
|
|
|
// Pass 2: Replace references to the namespace with references to the named imports
|
|
namespaceIdentifiers.forEach(identifier => {
|
|
const parent = identifier.parent;
|
|
if (parent && parent.type === 'MemberExpression') {
|
|
const importName = getMemberPropertyName(parent);
|
|
fixes.push(fixer.replaceText(parent, importLocalNames[importName]));
|
|
}
|
|
});
|
|
|
|
return fixes;
|
|
}) });
|
|
|
|
} };
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @param {Identifier[]} namespaceIdentifiers
|
|
* @returns {boolean} `true` if the namespace variable is more than just a glorified constant
|
|
*/ };
|
|
function usesNamespaceAsObject(namespaceIdentifiers) {
|
|
return !namespaceIdentifiers.every(identifier => {
|
|
const parent = identifier.parent;
|
|
|
|
// `namespace.x` or `namespace['x']`
|
|
return (
|
|
parent && parent.type === 'MemberExpression' && (
|
|
parent.property.type === 'Identifier' || parent.property.type === 'Literal'));
|
|
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param {MemberExpression} memberExpression
|
|
* @returns {string} the name of the member in the object expression, e.g. the `x` in `namespace.x`
|
|
*/
|
|
function getMemberPropertyName(memberExpression) {
|
|
return memberExpression.property.type === 'Identifier' ?
|
|
memberExpression.property.name :
|
|
memberExpression.property.value;
|
|
}
|
|
|
|
/**
|
|
* @param {ScopeManager} scopeManager
|
|
* @param {ASTNode} node
|
|
* @return {Set<string>}
|
|
*/
|
|
function getVariableNamesInScope(scopeManager, node) {
|
|
let currentNode = node;
|
|
let scope = scopeManager.acquire(currentNode);
|
|
while (scope == null) {
|
|
currentNode = currentNode.parent;
|
|
scope = scopeManager.acquire(currentNode, true);
|
|
}
|
|
return new Set([].concat(_toConsumableArray(
|
|
scope.variables.map(variable => variable.name)), _toConsumableArray(
|
|
scope.upper.variables.map(variable => variable.name))));
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {*} names
|
|
* @param {*} nameConflicts
|
|
* @param {*} namespaceName
|
|
*/
|
|
function generateLocalNames(names, nameConflicts, namespaceName) {
|
|
const localNames = {};
|
|
names.forEach(name => {
|
|
let localName;
|
|
if (!nameConflicts[name].has(name)) {
|
|
localName = name;
|
|
} else if (!nameConflicts[name].has(`${namespaceName}_${name}`)) {
|
|
localName = `${namespaceName}_${name}`;
|
|
} else {
|
|
for (let i = 1; i < Infinity; i++) {
|
|
if (!nameConflicts[name].has(`${namespaceName}_${name}_${i}`)) {
|
|
localName = `${namespaceName}_${name}_${i}`;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
localNames[name] = localName;
|
|
});
|
|
return localNames;
|
|
}
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJjcmVhdGUiLCJjb250ZXh0Iiwibm9kZSIsInNjb3BlVmFyaWFibGVzIiwiZ2V0U2NvcGUiLCJ2YXJpYWJsZXMiLCJuYW1lc3BhY2VWYXJpYWJsZSIsImZpbmQiLCJ2YXJpYWJsZSIsImRlZnMiLCJuYW1lc3BhY2VSZWZlcmVuY2VzI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ljdGluZyBpbXBvcnQgbmFtZXNcbiAgICAgICAgICAgIGNvbnN0IGltcG9ydE5hbWVDb25mbGljdHMgPSB7fVxuICAgICAgICAgICAgbmFtZXNwYWNlSWRlbnRpZmllcnMuZm9yRWFjaCgoaWRlbnRpZmllcikgPT4ge1xuICAgICAgICAgICAgICBjb25zdCBwYXJlbnQgPSBpZGVudGlmaWVyLnBhcmVudFxuICAgICAgICAgICAgICBpZiAocGFyZW50ICYmIHBhcmVudC50eXBlID09PSAnTWVtYmVyRXhwcmVzc2lvbicpIHtcbiAgICAgICAgICAgICAgICBjb25zdCBpbXBvcnROYW1lID0gZ2V0TWVtYmVyUHJvcGVydHlOYW1lKHBhcmVudClcbiAgICAgICAgICAgICAgICBjb25zdCBsb2NhbENvbmZsaWN0cyA9IGdldFZhcmlhYmxlTmFtZXNJblNjb3BlKHNjb3BlTWFuYWdlciwgcGFyZW50KVxuICAgICAgICAgICAgICAgIGlmICghaW1wb3J0TmFtZUNvbmZsaWN0c1tpbXBvcnROYW1lXSkge1xuICAgICAgICAgICAgICAgICAgaW1wb3J0TmFtZUNvbmZsaWN0c1tpbXBvcnROYW1lXSA9IGxvY2FsQ29uZmxpY3RzXG4gICAgICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICAgIGxvY2FsQ29uZmxpY3RzLmZvckVhY2goKGMpID0+IGltcG9ydE5hbWVDb25mbGljdHNbaW1wb3J0TmFtZV0uYWRkKGMpKVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSlcblxuICAgICAgICAgICAgLy8gQ2hvb3NlIG5ldyBuYW1lcyBmb3IgZWFjaCBpbXBvcnRcbiAgICAgICAgICAgIGNvbnN0IGltcG9ydE5hbWVzID0gT2JqZWN0LmtleXMoaW1wb3J0TmFtZUNvbmZsaWN0cylcbiAgICAgICAgICAgIGNvbnN0IGltcG9ydExvY2FsTmFtZXMgPSBnZW5lcmF0ZUxvY2FsTmFtZXMoXG4gICAgICAgICAgICAgIGltcG9ydE5hbWVzLFxuICAgICAgICAgICAgICBpbXBvcnROYW1lQ29uZmxpY3RzLFxuICAgICAgICAgICAgICBuYW1lc3BhY2VWYXJpYWJsZS5uYW1lXG4gICAgICAgICAgICApXG5cbiAgICAgICAgICAgIC8vIFJlcGxhY2UgdGhlIEltcG9ydE5hbWVzcGFjZVNwZWNpZmllciB3aXRoIGEgbGlzdCBvZiBJbXBvcnRTcGVjaWZpZXJzXG4gICAgICAgICAgICBjb25zdCBuYW1lZEltcG9ydFNwZWNpZmllcnMgPSBpbXBvcnROYW1lcy5tYXAoKGltcG9ydE5hbWUpID0+XG4gICAgICAgICAgICAgIGltcG9ydE5hbWUgPT09IGltcG9ydExvY2FsTmFtZXNbaW1wb3J0TmFtZV1cbiAgICAgICAgICAgICAgICA/IGltcG9ydE5hbWVcbiAgICAgICAgICAgICAgICA6IGAke2ltcG9ydE5hbWV9IGFzICR7aW1wb3J0TG9jYWxOYW1lc1tpbXBvcnROYW1lXX1gXG4gICAgICAgICAgICApXG4gICAgICAgICAgICBmaXhlcy5wdXNoKGZpeGVyLnJlcGxhY2VUZXh0KG5vZGUsIGB7ICR7bmFtZWRJbXBvcnRTcGVjaWZpZXJzLmpvaW4oJywgJyl9IH1gKSlcblxuICAgICAgICAgICAgLy8gUGFzcyAyOiBSZXBsYWNlIHJlZmVyZW5jZXMgdG8gdGhlIG5hbWVzcGFjZSB3aXRoIHJlZmVyZW5jZXMgdG8gdGhlIG5hbWVkIGltcG9ydHNcbiAgICAgICAgICAgIG5hbWVzcGFjZUlkZW50aWZpZXJzLmZvckVhY2goKGlkZW50aWZpZXIpID0+IHtcbiAgICAgICAgICAgICAgY29uc3QgcGFyZW50ID0gaWRlbnRpZmllci5wYXJlbnRcbiAgICAgICAgICAgICAgaWYgKHBhcmVudCAmJiBwYXJlbnQudHlwZSA9PT0gJ01lbWJlckV4cHJlc3Npb24nKSB7XG4gICAgICAgICAgICAgICAgY29uc3QgaW1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