Gazebo_simulation-Frontend/node_modules/eslint-plugin-import/lib/rules/namespace.js

218 lines
23 KiB
JavaScript
Raw Normal View History

2020-12-21 11:29:31 -04:00
'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: {
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: function (_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: function (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: function (dereference) {
if (dereference.object.type !== 'Identifier') return;
if (!namespaces.has(dereference.object.name)) 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: function (_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);
testKey(property.value, namespace.get(property.key.name).namespace, path);
path.pop();
}
}
testKey(id, namespaces.get(init.name));
},
JSXMemberExpression: function (_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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25hbWVzcGFjZS5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJjcmVhdGUiLCJuYW1lc3BhY2VSdWxlIiwiY29udGV4dCIsIm9wdGlvbnMiLCJhbGxvd0NvbXB1dGVkIiwibmFtZXNwYWNlcyIsIk1hcCIsIm1ha2VNZXNzYWdlIiwibGFzdCIsIm5hbWVwYXRoIiwibmFtZSIsImxlbmd0aCIsImpvaW4iLCJQcm9ncmFtIiwiYm9keSIsInByb2Nlc3NCb2R5U3RhdGVtZW50IiwiZGVjbGFyYXRpb24iLCJ0eXBlIiwic3BlY2lmaWVycyIsImltcG9ydHMiLCJFeHBvcnRzIiwiZ2V0Iiwic291cmNlIiwidmFsdWUiLCJlcnJvcnMiLCJyZXBvcnRFcnJvcnMiLCJzcGVjaWZpZXIiLCJzaXplIiwicmVwb3J0Iiwic2V0IiwibG9jYWwiLCJpbXBvcnRlZCIsIm5hbWVzcGFjZSIsImZvckVhY2giLCJFeHBvcnROYW1lc3BhY2VTcGVjaWZpZXIiLCJNZW1iZXJFeHByZXNzaW9uIiwiZGVyZWZlcmVuY2UiLCJvYmplY3QiLCJoYXMiLCJwYXJlbnQiLCJsZWZ0IiwiY29tcHV0ZWQi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