109 lines
11 KiB
JavaScript
109 lines
11 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _ExportMap = require('../ExportMap');
|
||
|
|
||
|
var _ExportMap2 = _interopRequireDefault(_ExportMap);
|
||
|
|
||
|
var _importDeclaration = require('../importDeclaration');
|
||
|
|
||
|
var _importDeclaration2 = _interopRequireDefault(_importDeclaration);
|
||
|
|
||
|
var _docsUrl = require('../docsUrl');
|
||
|
|
||
|
var _docsUrl2 = _interopRequireDefault(_docsUrl);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Rule Definition
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
url: (0, _docsUrl2.default)('no-named-as-default-member')
|
||
|
}
|
||
|
},
|
||
|
|
||
|
create: function (context) {
|
||
|
|
||
|
const fileImports = new Map();
|
||
|
const allPropertyLookups = new Map();
|
||
|
|
||
|
function handleImportDefault(node) {
|
||
|
const declaration = (0, _importDeclaration2.default)(context);
|
||
|
const exportMap = _ExportMap2.default.get(declaration.source.value, context);
|
||
|
if (exportMap == null) return;
|
||
|
|
||
|
if (exportMap.errors.length) {
|
||
|
exportMap.reportErrors(context, declaration);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
fileImports.set(node.local.name, {
|
||
|
exportMap,
|
||
|
sourcePath: declaration.source.value
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function storePropertyLookup(objectName, propName, node) {
|
||
|
const lookups = allPropertyLookups.get(objectName) || [];
|
||
|
lookups.push({ node, propName });
|
||
|
allPropertyLookups.set(objectName, lookups);
|
||
|
}
|
||
|
|
||
|
function handlePropLookup(node) {
|
||
|
const objectName = node.object.name;
|
||
|
const propName = node.property.name;
|
||
|
storePropertyLookup(objectName, propName, node);
|
||
|
}
|
||
|
|
||
|
function handleDestructuringAssignment(node) {
|
||
|
const isDestructure = node.id.type === 'ObjectPattern' && node.init != null && node.init.type === 'Identifier';
|
||
|
if (!isDestructure) return;
|
||
|
|
||
|
const objectName = node.init.name;
|
||
|
for (const _ref of node.id.properties) {
|
||
|
const key = _ref.key;
|
||
|
|
||
|
if (key == null) continue; // true for rest properties
|
||
|
storePropertyLookup(objectName, key.name, key);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function handleProgramExit() {
|
||
|
allPropertyLookups.forEach((lookups, objectName) => {
|
||
|
const fileImport = fileImports.get(objectName);
|
||
|
if (fileImport == null) return;
|
||
|
|
||
|
for (const _ref2 of lookups) {
|
||
|
const propName = _ref2.propName;
|
||
|
const node = _ref2.node;
|
||
|
|
||
|
// the default import can have a "default" property
|
||
|
if (propName === 'default') continue;
|
||
|
if (!fileImport.exportMap.namespace.has(propName)) continue;
|
||
|
|
||
|
context.report({
|
||
|
node,
|
||
|
message: `Caution: \`${objectName}\` also has a named export ` + `\`${propName}\`. Check if you meant to write ` + `\`import {${propName}} from '${fileImport.sourcePath}'\` ` + 'instead.'
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
'ImportDefaultSpecifier': handleImportDefault,
|
||
|
'MemberExpression': handlePropLookup,
|
||
|
'VariableDeclarator': handleDestructuringAssignment,
|
||
|
'Program:exit': handleProgramExit
|
||
|
};
|
||
|
}
|
||
|
}; /**
|
||
|
* @fileoverview Rule to warn about potentially confused use of name exports
|
||
|
* @author Desmond Brand
|
||
|
* @copyright 2016 Desmond Brand. All rights reserved.
|
||
|
* See LICENSE in root directory for full license.
|
||
|
*/
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lZC1hcy1kZWZhdWx0LW1lbWJlci5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJk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