UNPKG

@abaplint/core

Version:
119 lines (118 loc) 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InStatementIndentation = exports.InStatementIndentationConf = 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 Statements = require("../abap/2_statements/statements"); const _irule_1 = require("./_irule"); const ddic_1 = require("../ddic"); const _statement_1 = require("../abap/2_statements/statements/_statement"); const edit_helper_1 = require("../edit_helper"); const position_1 = require("../position"); class InStatementIndentationConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); /** Additional indent for first statement of blocks */ this.blockStatements = 2; /** Ignore global exception classes */ this.ignoreExceptions = true; } } exports.InStatementIndentationConf = InStatementIndentationConf; class InStatementIndentation extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new InStatementIndentationConf(); } getMetadata() { return { key: "in_statement_indentation", title: "In-statement indentation", shortDescription: "Checks alignment within statements which span multiple lines.", extendedInformation: `Lines following the first line should be indented once (2 spaces). For block declaration statements, lines after the first should be indented an additional time (default: +2 spaces) to distinguish them better from code within the block.`, badExample: `IF 1 = 1 AND 2 = 2. WRITE 'hello' && 'world'. ENDIF.`, goodExample: `IF 1 = 1 AND 2 = 2. WRITE 'hello' && 'world'. ENDIF.`, tags: [_irule_1.RuleTag.Whitespace, _irule_1.RuleTag.Quickfix, _irule_1.RuleTag.SingleFile], }; } getMessage() { return "Fix in-statement indentation"; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file, obj) { const ret = []; const ddic = new ddic_1.DDIC(this.reg); if (obj instanceof objects_1.Class) { const definition = obj.getClassDefinition(); if (definition === undefined) { return []; } else if (this.conf.ignoreExceptions && ddic.isException(definition, obj)) { return []; } } for (const s of file.getStatements()) { if (s.get() instanceof _statement_1.Comment || s.get() instanceof _statement_1.Unknown || s.get() instanceof _statement_1.NativeSQL) { continue; } const tokens = s.getTokens(); if (tokens.length === 0) { continue; } const beginLine = tokens[0].getRow(); let expected = tokens[0].getCol() + 2; const type = s.get(); if (type instanceof Statements.If || type instanceof Statements.While || type instanceof Statements.Module || type instanceof Statements.SelectLoop || type instanceof Statements.FunctionModule || type instanceof Statements.Do || type instanceof Statements.At || type instanceof Statements.Catch || type instanceof Statements.Case || type instanceof Statements.When || type instanceof Statements.Cleanup || type instanceof Statements.Loop || type instanceof Statements.Form || type instanceof Statements.Else || type instanceof Statements.ElseIf || type instanceof Statements.MethodImplementation) { expected = expected + this.conf.blockStatements; } for (const t of tokens) { if (t.getRow() === beginLine) { continue; } if (t.getCol() < expected) { const fix = edit_helper_1.EditHelper.replaceRange(file, new position_1.Position(t.getRow(), 1), t.getStart(), " ".repeat(expected - 1)); const issue = issue_1.Issue.atToken(file, t, this.getMessage(), this.getMetadata().key, this.conf.severity, fix); ret.push(issue); break; } } } return ret; } } exports.InStatementIndentation = InStatementIndentation; //# sourceMappingURL=in_statement_indentation.js.map