securityos/node_modules/eslint-plugin-import/lib/rules/no-empty-named-blocks.js

105 lines
15 KiB
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
'use strict';var _slicedToArray = function () {function sliceIterator(arr, i) {var _arr = [];var _n = true;var _d = false;var _e = undefined;try {for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {_arr.push(_s.value);if (i && _arr.length === i) break;}} catch (err) {_d = true;_e = err;} finally {try {if (!_n && _i["return"]) _i["return"]();} finally {if (_d) throw _e;}}return _arr;}return function (arr, i) {if (Array.isArray(arr)) {return arr;} else if (Symbol.iterator in Object(arr)) {return sliceIterator(arr, i);} else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}();var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
function getEmptyBlockRange(tokens, index) {
var token = tokens[index];
var nextToken = tokens[index + 1];
var prevToken = tokens[index - 1];
var start = token.range[0];
var end = nextToken.range[1];
// Remove block tokens and the previous comma
if (prevToken.value === ',' || prevToken.value === 'type' || prevToken.value === 'typeof') {
start = prevToken.range[0];
}
return [start, end];
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
category: 'Helpful warnings',
description: 'Forbid empty named import blocks.',
url: (0, _docsUrl2['default'])('no-empty-named-blocks') },
fixable: 'code',
schema: [],
hasSuggestions: true },
create: function () {function create(context) {
var importsWithoutNameds = [];
return {
ImportDeclaration: function () {function ImportDeclaration(node) {
if (!node.specifiers.some(function (x) {return x.type === 'ImportSpecifier';})) {
importsWithoutNameds.push(node);
}
}return ImportDeclaration;}(),
'Program:exit': function () {function ProgramExit(program) {
var importsTokens = importsWithoutNameds.map(function (node) {return [node, program.tokens.filter(function (x) {return x.range[0] >= node.range[0] && x.range[1] <= node.range[1];})];});
importsTokens.forEach(function (_ref) {var _ref2 = _slicedToArray(_ref, 2),node = _ref2[0],tokens = _ref2[1];
tokens.forEach(function (token) {
var idx = program.tokens.indexOf(token);
var nextToken = program.tokens[idx + 1];
if (nextToken && token.value === '{' && nextToken.value === '}') {
var hasOtherIdentifiers = tokens.some(function (token) {return token.type === 'Identifier' &&
token.value !== 'from' &&
token.value !== 'type' &&
token.value !== 'typeof';});
// If it has no other identifiers it's the only thing in the import, so we can either remove the import
// completely or transform it in a side-effects only import
if (!hasOtherIdentifiers) {
context.report({
node: node,
message: 'Unexpected empty named import block',
suggest: [
{
desc: 'Remove unused import',
fix: function () {function fix(fixer) {
// Remove the whole import
return fixer.remove(node);
}return fix;}() },
{
desc: 'Remove empty import block',
fix: function () {function fix(fixer) {
// Remove the empty block and the 'from' token, leaving the import only for its side
// effects, e.g. `import 'mod'`
var sourceCode = context.getSourceCode();
var fromToken = program.tokens.find(function (t) {return t.value === 'from';});
var importToken = program.tokens.find(function (t) {return t.value === 'import';});
var hasSpaceAfterFrom = sourceCode.isSpaceBetween(fromToken, sourceCode.getTokenAfter(fromToken));
var hasSpaceAfterImport = sourceCode.isSpaceBetween(importToken, sourceCode.getTokenAfter(fromToken));var _getEmptyBlockRange =
getEmptyBlockRange(program.tokens, idx),_getEmptyBlockRange2 = _slicedToArray(_getEmptyBlockRange, 1),start = _getEmptyBlockRange2[0];var _fromToken$range = _slicedToArray(
fromToken.range, 2),end = _fromToken$range[1];
var range = [start, hasSpaceAfterFrom ? end + 1 : end];
return fixer.replaceTextRange(range, hasSpaceAfterImport ? '' : ' ');
}return fix;}() }] });
} else {
context.report({
node: node,
message: 'Unexpected empty named import block',
fix: function () {function fix(fixer) {
return fixer.removeRange(getEmptyBlockRange(program.tokens, idx));
}return fix;}() });
}
}
});
});
}return ProgramExit;}() };
}return create;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1lbXB0eS1uYW1lZC1ibG9ja3MuanMiXSwibmFtZXMiOlsiZ2V0RW1wdHlCbG9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