UNPKG

@abaplint/core

Version:
111 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommentedCode = exports.CommentedCodeConf = void 0; const issue_1 = require("../issue"); const position_1 = require("../position"); const _abap_rule_1 = require("./_abap_rule"); const _basic_rule_config_1 = require("./_basic_rule_config"); const _statement_1 = require("../abap/2_statements/statements/_statement"); const objects_1 = require("../objects"); const statements_1 = require("../abap/2_statements/statements"); const abap_parser_1 = require("../abap/abap_parser"); const _irule_1 = require("./_irule"); const edit_helper_1 = require("../edit_helper"); const memory_file_1 = require("../files/memory_file"); class CommentedCodeConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); /** Allow INCLUDEs in function groups */ this.allowIncludeInFugr = true; } } exports.CommentedCodeConf = CommentedCodeConf; class CommentedCode extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new CommentedCodeConf(); } getMetadata() { return { key: "commented_code", title: "Find commented code", shortDescription: `Detects usage of commented out code.`, extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#delete-code-instead-of-commenting-it https://docs.abapopenchecks.org/checks/14/`, tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.Quickfix, _irule_1.RuleTag.SingleFile], badExample: `* WRITE 'hello world'.`, }; } getMessage() { return "Commented code"; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file, obj) { let issues = []; const rows = file.getRawRows(); let code = ""; let posEnd = undefined; let posStart = undefined; for (let i = 0; i < rows.length; i++) { if (this.isCommentLine(rows[i])) { if (code === "") { posStart = new position_1.Position(i + 1, 1); } code = code + rows[i].trim().substr(1) + "\n"; posEnd = new position_1.Position(i + 1, rows[i].length + 1); } else if (code !== "" && posStart && posEnd) { issues = issues.concat(this.check(code.trim(), file, posStart, posEnd, obj)); code = ""; } } if (posStart && posEnd) { issues = issues.concat(this.check(code.trim(), file, posStart, posEnd, obj)); } return issues; } check(code, file, posStart, posEnd, obj) { // assumption: code must end with "." in order to be valid ABAP if (code === "" || code.charAt(code.length - 1) !== ".") { return []; } const commented = new memory_file_1.MemoryFile("_foobar.prog.abap", code); const abapFile = new abap_parser_1.ABAPParser().parse([commented]).output[0]; const statementNodes = abapFile.getStatements(); if (statementNodes.length === 0) { return []; } let containsStatement = false; for (const statementNode of statementNodes) { const statement = statementNode.get(); if (this.getConfig().allowIncludeInFugr === true && obj instanceof objects_1.FunctionGroup && statement instanceof statements_1.Include) { continue; } if (!(statement instanceof _statement_1.Unknown || statement instanceof _statement_1.Empty || statement instanceof _statement_1.Comment)) { containsStatement = true; break; } } if (!containsStatement) { return []; } const fix = edit_helper_1.EditHelper.deleteRange(file, posStart, posEnd); const issue = issue_1.Issue.atRange(file, posStart, posEnd, this.getMessage(), this.getMetadata().key, this.conf.severity, fix); return [issue]; } isCommentLine(text) { return (text.substr(0, 1) === "*") || (text.trim().substr(0, 1) === "\"" && text.trim().substr(1, 1) !== "!"); } } exports.CommentedCode = CommentedCode; //# sourceMappingURL=commented_code.js.map