|
|
'use strict';var _declaredScope = require('eslint-module-utils/declaredScope');var _declaredScope2 = _interopRequireDefault(_declaredScope); 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 };}
module.exports = { meta: { type: 'problem', docs: { url: (0, _docsUrl2.default)('namespace') },
schema: [ { type: 'object', properties: { allowComputed: { description: 'If `false`, will report computed (and thus, un-lintable) references to namespace members.', type: 'boolean', default: false } },
additionalProperties: false }] },
create: function namespaceRule(context) {
// read options
var _ref =
context.options[0] || {},_ref$allowComputed = _ref.allowComputed;const allowComputed = _ref$allowComputed === undefined ? false : _ref$allowComputed;
const namespaces = new Map();
function makeMessage(last, namepath) { return `'${last.name}' not found in ${namepath.length > 1 ? 'deeply ' : ''}imported namespace '${namepath.join('.')}'.`; }
return { // pick up all imports at body entry time, to properly respect hoisting
Program(_ref2) {let body = _ref2.body; function processBodyStatement(declaration) { if (declaration.type !== 'ImportDeclaration') return;
if (declaration.specifiers.length === 0) return;
const imports = _ExportMap2.default.get(declaration.source.value, context); if (imports == null) return null;
if (imports.errors.length) { imports.reportErrors(context, declaration); return; }
for (const specifier of declaration.specifiers) { switch (specifier.type) { case 'ImportNamespaceSpecifier': if (!imports.size) { context.report( specifier, `No exported names found in module '${declaration.source.value}'.`);
} namespaces.set(specifier.local.name, imports); break; case 'ImportDefaultSpecifier': case 'ImportSpecifier':{ const meta = imports.get( // default to 'default' for default http://i.imgur.com/nj6qAWy.jpg
specifier.imported ? specifier.imported.name : 'default');
if (!meta || !meta.namespace) {break;} namespaces.set(specifier.local.name, meta.namespace); break; }}
} } body.forEach(processBodyStatement); },
// same as above, but does not add names to local map
ExportNamespaceSpecifier(namespace) { var declaration = (0, _importDeclaration2.default)(context);
var imports = _ExportMap2.default.get(declaration.source.value, context); if (imports == null) return null;
if (imports.errors.length) { imports.reportErrors(context, declaration); return; }
if (!imports.size) { context.report( namespace, `No exported names found in module '${declaration.source.value}'.`);
} },
// todo: check for possible redefinition
MemberExpression(dereference) { if (dereference.object.type !== 'Identifier') return; if (!namespaces.has(dereference.object.name)) return; if ((0, _declaredScope2.default)(context, dereference.object.name) !== 'module') return;
if (dereference.parent.type === 'AssignmentExpression' && dereference.parent.left === dereference) { context.report( dereference.parent, `Assignment to member of namespace '${dereference.object.name}'.`);
}
// go deep
var namespace = namespaces.get(dereference.object.name); var namepath = [dereference.object.name]; // while property is namespace and parent is member expression, keep validating
while (namespace instanceof _ExportMap2.default && dereference.type === 'MemberExpression') {
if (dereference.computed) { if (!allowComputed) { context.report( dereference.property, `Unable to validate computed reference to imported namespace '${dereference.object.name}'.`);
} return; }
if (!namespace.has(dereference.property.name)) { context.report( dereference.property, makeMessage(dereference.property, namepath));
break; }
const exported = namespace.get(dereference.property.name); if (exported == null) return;
// stash and pop
namepath.push(dereference.property.name); namespace = exported.namespace; dereference = dereference.parent; }
},
VariableDeclarator(_ref3) {let id = _ref3.id,init = _ref3.init; if (init == null) return; if (init.type !== 'Identifier') return; if (!namespaces.has(init.name)) return;
// check for redefinition in intermediate scopes
if ((0, _declaredScope2.default)(context, init.name) !== 'module') return;
// DFS traverse child namespaces
function testKey(pattern, namespace) {let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [init.name]; if (!(namespace instanceof _ExportMap2.default)) return;
if (pattern.type !== 'ObjectPattern') return;
for (const property of pattern.properties) { if ( property.type === 'ExperimentalRestProperty' || property.type === 'RestElement' || !property.key) { continue; }
if (property.key.type !== 'Identifier') { context.report({ node: property, message: 'Only destructure top-level names.' });
continue; }
if (!namespace.has(property.key.name)) { context.report({ node: property, message: makeMessage(property.key, path) });
continue; }
path.push(property.key.name); const dependencyExportMap = namespace.get(property.key.name); // could be null when ignored or ambiguous
if (dependencyExportMap !== null) { testKey(property.value, dependencyExportMap.namespace, path); } path.pop(); } }
testKey(id, namespaces.get(init.name)); },
JSXMemberExpression(_ref4) {let object = _ref4.object,property = _ref4.property; if (!namespaces.has(object.name)) return; var namespace = namespaces.get(object.name); if (!namespace.has(property.name)) { context.report({ node: property, message: makeMessage(property, [object.name]) });
} } };
} }; //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsInByb3BlcnRpZXMiLCJhbGxvd0NvbXB1dGVkIiwiZGVzY3JpcHRpb24iLCJkZWZhdWx0IiwiYWRkaXRpb25hbFByb3BlcnRpZXMiLCJjcmVhdGUiLCJuYW1lc3BhY2VSdWxlIiwiY29udGV4dCIsIm9wdGlvbnMiLCJuYW1lc3BhY2VzIiwiTWFwIiwibWFrZU1lc3NhZ2UiLCJsYXN0IiwibmFtZXBhdGgiLCJuYW1lIiwibGVuZ3RoIiwiam9pbiIsIlByb2dyYW0iLCJib2R5IiwicHJvY2Vzc0JvZHlTdGF0ZW1lbnQiLCJkZWNsYXJhdGlvbiIsInNwZWNpZmllcnMiLCJpbXBvcnRzIiwiRXhwb3J0cyIsImdldCIsInNvdXJjZSIsInZhbHVlIiwiZXJyb3JzIiwicmVwb3J0RXJyb3JzIiwic3BlY2lmaWVyIiwic2l6ZSIsInJlcG9ydCIsInNldCIsImxvY2FsIiwiaW1wb3J0ZWQiLCJuYW1lc3BhY2UiLCJmb3JFYWNoIiwiRXhwb3J0TmFtZXNwYWNlU3BlY2lmaWVyIiwiTWVtYmVyRXhwcmVzc2lvbiIsImRlcmVmZXJlbmNlIiwib2JqZWN0IiwiaGFzIiwicGFyZW50IiwibGVmdCIsImNvbXB1dGVkIiwicHJvcGVydHkiLCJleHBvcnRlZCIsInB1c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