odin-javascript-exercises/node_modules/eslint-plugin-import/lib/rules/no-named-as-default-member.js

103 lines
11 KiB
JavaScript
Raw Normal View History

2017-12-15 10:56:14 -08:00
'use strict';
var _ExportMap = require('../ExportMap');
var _ExportMap2 = _interopRequireDefault(_ExportMap);
var _importDeclaration = require('../importDeclaration');
var _importDeclaration2 = _interopRequireDefault(_importDeclaration);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/**
* @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.
*/
module.exports = {
meta: {
docs: {}
},
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
};
}
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25vLW5hbWVkLWFzLWRlZmF1bHQtbWVtYmVyLmpzIl0sIm5hbWVzIjpbIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsImNyZWF0ZSIsImNvbnRleHQiLCJmaWxlSW1wb3J0cyIsIk1hcCIsImFsbFByb3BlcnR5TG9va3VwcyIsImhhbmRsZUltcG9ydERlZmF1bHQiLCJub2RlIiwiZGVjbGFyYXRpb24iLCJleHBvcnRNYXAiLCJnZXQiLCJzb3VyY2UiLCJ2YWx1ZSIsImVycm9ycyIsImxlbmd0aCIsInJlcG9ydEVycm9ycyIsInNldCIsImxvY2FsIiwibmFtZSIsInNvdXJjZVBhdGgiLCJzdG9yZVByb3BlcnR5TG9va3VwIiwib2JqZWN0TmFtZSIsInByb3BOYW1lIiwibG9va3VwcyIsInB1c2giLCJoYW5kbGVQcm9wTG9va3VwIiwib2JqZWN0IiwicHJvcGVydHkiLCJoYW5kbGVEZXN0cnVjdHVyaW5nQXNzaWdubWVudCIsImlzRGVzdHJ1Y3R1cmUiLCJpZCIsInR5cGUiLCJpbml0IiwicHJvcGVydGllcyIsImtleSIsImhhbmRsZVByb2dyYW1FeGl0IiwiZm9yRWFjaCIsImZpbGVJbXBvcnQiLCJuYW1lc3BhY2UiLCJoYXMiLCJyZXBvcnQiLCJtZXNzYWdlIl0sIm1hcHBpbmdzIjoiOztBQU1BOzs7O0FBQ0E7Ozs7OztBQUVBO0FBQ0E7QUFDQTs7QUFYQTs7Ozs7O0FBYUFBLE9BQU9DLE9BQVAsR0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