110 lines
11 KiB
JavaScript
110 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')
|
||
|
},
|
||
|
schema: []
|
||
|
},
|
||
|
|
||
|
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lZC1hcy1kZWZhdWx0LW1lbWJlci5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIs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