108 lines
11 KiB
JavaScript
108 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: {
|
||
|
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25vLW5hbWVkLWFzLWRlZmF1bHQtbWVtYmVyLmpzIl0sIm5hbWVzIjpbIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsInVybCIsImNyZWF0ZSIsImNvbnRleHQiL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