You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
21 KiB
184 lines
21 KiB
2 years ago
|
'use strict';
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
var _staticRequire = require('../core/staticRequire');var _staticRequire2 = _interopRequireDefault(_staticRequire);
|
||
|
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);
|
||
|
|
||
|
var _debug = require('debug');var _debug2 = _interopRequireDefault(_debug);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
|
||
|
var log = (0, _debug2['default'])('eslint-plugin-import:rules:newline-after-import');
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Rule Definition
|
||
|
//------------------------------------------------------------------------------
|
||
|
/**
|
||
|
* @fileoverview Rule to enforce new line after import not followed by another import.
|
||
|
* @author Radek Benkel
|
||
|
*/function containsNodeOrEqual(outerNode, innerNode) {return outerNode.range[0] <= innerNode.range[0] && outerNode.range[1] >= innerNode.range[1];}
|
||
|
|
||
|
function getScopeBody(scope) {
|
||
|
if (scope.block.type === 'SwitchStatement') {
|
||
|
log('SwitchStatement scopes not supported');
|
||
|
return null;
|
||
|
}var
|
||
|
|
||
|
body = scope.block.body;
|
||
|
if (body && body.type === 'BlockStatement') {
|
||
|
return body.body;
|
||
|
}
|
||
|
|
||
|
return body;
|
||
|
}
|
||
|
|
||
|
function findNodeIndexInScopeBody(body, nodeToFind) {
|
||
|
return body.findIndex(function (node) {return containsNodeOrEqual(node, nodeToFind);});
|
||
|
}
|
||
|
|
||
|
function getLineDifference(node, nextNode) {
|
||
|
return nextNode.loc.start.line - node.loc.end.line;
|
||
|
}
|
||
|
|
||
|
function isClassWithDecorator(node) {
|
||
|
return node.type === 'ClassDeclaration' && node.decorators && node.decorators.length;
|
||
|
}
|
||
|
|
||
|
function isExportDefaultClass(node) {
|
||
|
return node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ClassDeclaration';
|
||
|
}
|
||
|
|
||
|
function isExportNameClass(node) {
|
||
|
|
||
|
return node.type === 'ExportNamedDeclaration' && node.declaration && node.declaration.type === 'ClassDeclaration';
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'layout',
|
||
|
docs: {
|
||
|
url: (0, _docsUrl2['default'])('newline-after-import') },
|
||
|
|
||
|
fixable: 'whitespace',
|
||
|
schema: [
|
||
|
{
|
||
|
'type': 'object',
|
||
|
'properties': {
|
||
|
'count': {
|
||
|
'type': 'integer',
|
||
|
'minimum': 1 } },
|
||
|
|
||
|
|
||
|
'additionalProperties': false }] },
|
||
|
|
||
|
|
||
|
|
||
|
create: function () {function create(context) {
|
||
|
var level = 0;
|
||
|
var requireCalls = [];
|
||
|
|
||
|
function checkForNewLine(node, nextNode, type) {
|
||
|
if (isExportDefaultClass(nextNode) || isExportNameClass(nextNode)) {
|
||
|
var classNode = nextNode.declaration;
|
||
|
|
||
|
if (isClassWithDecorator(classNode)) {
|
||
|
nextNode = classNode.decorators[0];
|
||
|
}
|
||
|
} else if (isClassWithDecorator(nextNode)) {
|
||
|
nextNode = nextNode.decorators[0];
|
||
|
}
|
||
|
|
||
|
var options = context.options[0] || { count: 1 };
|
||
|
var lineDifference = getLineDifference(node, nextNode);
|
||
|
var EXPECTED_LINE_DIFFERENCE = options.count + 1;
|
||
|
|
||
|
if (lineDifference < EXPECTED_LINE_DIFFERENCE) {
|
||
|
var column = node.loc.start.column;
|
||
|
|
||
|
if (node.loc.start.line !== node.loc.end.line) {
|
||
|
column = 0;
|
||
|
}
|
||
|
|
||
|
context.report({
|
||
|
loc: {
|
||
|
line: node.loc.end.line,
|
||
|
column: column },
|
||
|
|
||
|
message: 'Expected ' + String(options.count) + ' empty line' + (options.count > 1 ? 's' : '') + ' after ' + String(
|
||
|
type) + ' statement not followed by another ' + String(type) + '.',
|
||
|
fix: function () {function fix(fixer) {return fixer.insertTextAfter(
|
||
|
node,
|
||
|
'\n'.repeat(EXPECTED_LINE_DIFFERENCE - lineDifference));}return fix;}() });
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function incrementLevel() {
|
||
|
level++;
|
||
|
}
|
||
|
function decrementLevel() {
|
||
|
level--;
|
||
|
}
|
||
|
|
||
|
function checkImport(node) {var
|
||
|
parent = node.parent;
|
||
|
var nodePosition = parent.body.indexOf(node);
|
||
|
var nextNode = parent.body[nodePosition + 1];
|
||
|
|
||
|
// skip "export import"s
|
||
|
if (node.type === 'TSImportEqualsDeclaration' && node.isExport) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (nextNode && nextNode.type !== 'ImportDeclaration' && (nextNode.type !== 'TSImportEqualsDeclaration' || nextNode.isExport)) {
|
||
|
checkForNewLine(node, nextNode, 'import');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
ImportDeclaration: checkImport,
|
||
|
TSImportEqualsDeclaration: checkImport,
|
||
|
CallExpression: function () {function CallExpression(node) {
|
||
|
if ((0, _staticRequire2['default'])(node) && level === 0) {
|
||
|
requireCalls.push(node);
|
||
|
}
|
||
|
}return CallExpression;}(),
|
||
|
'Program:exit': function () {function ProgramExit() {
|
||
|
log('exit processing for', context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename());
|
||
|
var scopeBody = getScopeBody(context.getScope());
|
||
|
log('got scope:', scopeBody);
|
||
|
|
||
|
requireCalls.forEach(function (node, index) {
|
||
|
var nodePosition = findNodeIndexInScopeBody(scopeBody, node);
|
||
|
log('node position in scope:', nodePosition);
|
||
|
|
||
|
var statementWithRequireCall = scopeBody[nodePosition];
|
||
|
var nextStatement = scopeBody[nodePosition + 1];
|
||
|
var nextRequireCall = requireCalls[index + 1];
|
||
|
|
||
|
if (nextRequireCall && containsNodeOrEqual(statementWithRequireCall, nextRequireCall)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (nextStatement && (
|
||
|
!nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) {
|
||
|
|
||
|
checkForNewLine(statementWithRequireCall, nextStatement, 'require');
|
||
|
}
|
||
|
});
|
||
|
}return ProgramExit;}(),
|
||
|
FunctionDeclaration: incrementLevel,
|
||
|
FunctionExpression: incrementLevel,
|
||
|
ArrowFunctionExpression: incrementLevel,
|
||
|
BlockStatement: incrementLevel,
|
||
|
ObjectExpression: incrementLevel,
|
||
|
Decorator: incrementLevel,
|
||
|
'FunctionDeclaration:exit': decrementLevel,
|
||
|
'FunctionExpression:exit': decrementLevel,
|
||
|
'ArrowFunctionExpression:exit': decrementLevel,
|
||
|
'BlockStatement:exit': decrementLevel,
|
||
|
'ObjectExpression:exit': decrementLevel,
|
||
|
'Decorator:exit': decrementLevel };
|
||
|
|
||
|
}return create;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uZXdsaW5lLWFmdGVyLWltcG9ydC5qcyJdLCJuYW1lcyI6WyJsb2ciLCJjb250YWluc05vZGVPckVxdWFsIiwib3V0ZXJOb2RlIiwiaW5uZXJOb2RlIiwicmFuZ2UiLCJnZXRTY29wZUJvZHkiLCJzY29wZSIsImJsb2NrIiwidHlwZSIsImJvZHkiLCJmaW5kTm9kZUluZGV4SW5TY29wZUJvZHkiLCJub2RlVG9GaW5kIiwiZmluZEluZGV4Iiwibm9kZSIsImdldExpbmVEaWZmZXJlbmNlIiwibmV4dE5vZGUiLCJsb2MiLCJzdGFydCIsImxpbmUiLCJlbmQiLCJpc0NsYXNzV2l0aERlY29yYXRvciIsImRlY29yYXRvcnMiLCJsZW5ndGgiLCJpc0V4cG9ydERlZmF1bHRDbGFzcyIsImRlY2xhcmF0aW9uIiwiaXNFeHBvcnROYW1lQ2xhc3MiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJ1cmwiLCJmaXhhYmxlIiwic2NoZW1hIiwiY3JlYXRlIiwiY29udGV4dCIsImxldmVs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