UNPKG

@abaplint/core

Version:
153 lines (151 loc) • 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Indentation = exports.IndentationConf = void 0; const issue_1 = require("../issue"); const _abap_rule_1 = require("./_abap_rule"); const objects_1 = require("../objects"); const _basic_rule_config_1 = require("./_basic_rule_config"); const indent_1 = require("../pretty_printer/indent"); const Statements = require("../abap/2_statements/statements"); const Expressions = require("../abap/2_statements/expressions"); const _irule_1 = require("./_irule"); const ddic_1 = require("../ddic"); const position_1 = require("../position"); const virtual_position_1 = require("../virtual_position"); const edit_helper_1 = require("../edit_helper"); const _statement_1 = require("../abap/2_statements/statements/_statement"); class IndentationConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); /** Ignore global exception classes */ this.ignoreExceptions = true; /** Align TRY CATCH, TRY and CATCH should have the same indentation */ this.alignTryCatch = false; /** Add indentation for SELECTION SCREEN BLOCK, standard pretty printer indents this from 754 */ this.selectionScreenBlockIndentation = false; this.globalClassSkipFirst = false; this.ignoreGlobalClassDefinition = false; this.ignoreGlobalInterface = false; } } exports.IndentationConf = IndentationConf; class Indentation extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new IndentationConf(); } getMetadata() { return { key: "indentation", title: "Indentation", shortDescription: `Checks indentation`, tags: [_irule_1.RuleTag.Whitespace, _irule_1.RuleTag.Quickfix, _irule_1.RuleTag.SingleFile], badExample: `CLASS lcl DEFINITION. PRIVATE SECTION. METHODS constructor. ENDCLASS. CLASS lcl IMPLEMENTATION. METHOD constructor. ENDMETHOD. ENDCLASS.`, goodExample: `CLASS lcl DEFINITION. PRIVATE SECTION. METHODS constructor. ENDCLASS. CLASS lcl IMPLEMENTATION. METHOD constructor. ENDMETHOD. ENDCLASS.`, }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file, obj) { var _a, _b; const MAX_ISSUES = 100; let skip = false; if (file.getStructure() === undefined) { return []; // syntax error in file } if (obj instanceof objects_1.Class) { const definition = obj.getClassDefinition(); const ddic = new ddic_1.DDIC(this.reg); if (definition === undefined) { return []; } else if (this.conf.ignoreExceptions && ddic.isException(definition, obj)) { return []; } } const indentOpts = { selectionScreenBlockIndentation: (_a = this.conf) === null || _a === void 0 ? void 0 : _a.selectionScreenBlockIndentation, alignTryCatch: (_b = this.conf) === null || _b === void 0 ? void 0 : _b.alignTryCatch, globalClassSkipFirst: this.conf.globalClassSkipFirst, }; const expected = new indent_1.Indent(indentOpts).getExpectedIndents(file); const ret = []; let previous = undefined; for (const statement of file.getStatements()) { const position = statement.getFirstToken().getStart(); if (position instanceof virtual_position_1.VirtualPosition) { continue; } const indent = expected.shift(); if (this.conf.ignoreGlobalClassDefinition) { if (statement.get() instanceof Statements.ClassDefinition && statement.findFirstExpression(Expressions.ClassGlobal)) { skip = true; continue; } else if (skip === true && statement.get() instanceof Statements.EndClass) { skip = false; continue; } else if (skip === true) { continue; } } if (this.conf.ignoreGlobalInterface) { if (statement.get() instanceof Statements.Interface && statement.findFirstExpression(Expressions.ClassGlobal)) { skip = true; continue; } else if (skip === true && statement.get() instanceof Statements.EndInterface) { skip = false; continue; } else if (skip === true) { continue; } } if (statement.get() instanceof _statement_1.NativeSQL) { continue; } // only apply for the first statement in a chain if (statement.getColon() !== undefined && (previous === null || previous === void 0 ? void 0 : previous.getColon()) !== undefined && statement.getColon().getStart().equals(previous.getColon().getStart())) { continue; } if (indent && indent > 0 && indent !== position.getCol()) { const expected = indent - 1; const fix = edit_helper_1.EditHelper.replaceRange(file, new position_1.Position(position.getRow(), 1), position, " ".repeat(expected)); const message = "Indentation problem, expected " + expected + " spaces"; const issue = issue_1.Issue.atPosition(file, position, message, this.getMetadata().key, this.conf.severity, fix); ret.push(issue); if (ret.length >= MAX_ISSUES) { break; } } previous = statement; } return ret; } } exports.Indentation = Indentation; //# sourceMappingURL=indentation.js.map