@abaplint/core
Version:
abaplint - Core API
151 lines (146 loc) • 7.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AvoidUse = exports.AvoidUseConf = void 0;
const Statements = require("../abap/2_statements/statements");
const issue_1 = require("../issue");
const _abap_rule_1 = require("./_abap_rule");
const _basic_rule_config_1 = require("./_basic_rule_config");
const expressions_1 = require("../abap/2_statements/expressions");
const _irule_1 = require("./_irule");
const version_1 = require("../version");
const edit_helper_1 = require("../edit_helper");
const _statement_1 = require("../abap/2_statements/statements/_statement");
class AvoidUseConf extends _basic_rule_config_1.BasicRuleConfig {
constructor() {
super(...arguments);
/** Do not emit quick fix suggestion */
this.skipQuickFix = false;
/** Detects DEFINE (macro definitions) */
this.define = true;
/** Detects statics */
this.statics = true;
/** Detects DEFAULT KEY definitions, from version v740sp02 and up. Use pseudo comment DEFAULT_KEY to ignore */
this.defaultKey = true;
/** Detects BREAK and BREAK-POINTS */
this.break = true;
/** Detects TEST SEAMS. Use pseudo comment TEST_SEAM_USAGE to ignore */
this.testSeams = true;
/** Detects DESCRIBE TABLE LINES, use lines() instead */
this.describeLines = true;
/** Detects EXPORT TO MEMORY */
this.exportToMemory = true;
/** Detects EXPORT TO DATABASE */
this.exportToDatabase = true;
}
}
exports.AvoidUseConf = AvoidUseConf;
class AvoidUse extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new AvoidUseConf();
}
getMetadata() {
return {
key: "avoid_use",
title: "Avoid use of certain statements",
shortDescription: `Detects usage of certain statements.`,
extendedInformation: `DEFAULT KEY: https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#avoid-default-key
Macros: https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenmacros_guidl.htm
STATICS: use CLASS-DATA instead
DESCRIBE TABLE LINES: use lines() instead (quickfix exists)
TEST-SEAMS: https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#use-test-seams-as-temporary-workaround
BREAK points`,
tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.SingleFile],
};
}
getDescription(statement) {
return "Avoid use of " + statement;
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file) {
var _a;
const issues = [];
let isStaticsBlock = false;
const statements = file.getStatements();
for (let i = 0; i < statements.length; i++) {
const statementNode = statements[i];
const statement = statementNode.get();
let message = undefined;
let fix = undefined;
if (this.conf.define && statement instanceof Statements.Define) {
message = "DEFINE";
}
else if (this.conf.describeLines && statement instanceof Statements.Describe) {
const children = statementNode.getChildren();
if (children.length === 6 && children[3].getFirstToken().getStr().toUpperCase() === "LINES") {
message = "DESCRIBE LINES, use lines() instead";
fix = this.conf.skipQuickFix === true ? undefined : this.getDescribeLinesFix(file, statementNode);
}
}
else if (this.conf.statics && statement instanceof Statements.StaticBegin) {
isStaticsBlock = true;
message = "STATICS";
}
else if (this.conf.statics && statement instanceof Statements.StaticEnd) {
isStaticsBlock = false;
}
else if (this.conf.exportToMemory && statement instanceof Statements.Export && statementNode.concatTokens().includes("TO MEMORY ")) {
message = "EXPORT TO MEMORY";
}
else if (this.conf.exportToDatabase && statement instanceof Statements.Export && statementNode.concatTokens().includes("TO DATABASE ")) {
message = "EXPORT TO DATABASE";
}
else if (this.conf.testSeams && statement instanceof Statements.TestSeam) {
const next = statements[i + 1];
if ((next === null || next === void 0 ? void 0 : next.get()) instanceof _statement_1.Comment && next.concatTokens().includes("EC TEST_SEAM_USAGE")) {
continue;
}
message = "TEST-SEAM";
}
else if (this.conf.statics && statement instanceof Statements.Static && isStaticsBlock === false) {
message = "STATICS";
}
else if (this.conf.break && statement instanceof Statements.Break) {
message = "BREAK/BREAK-POINT";
fix = this.conf.skipQuickFix === true ? undefined : edit_helper_1.EditHelper.deleteStatement(file, statementNode);
}
if (message) {
issues.push(issue_1.Issue.atStatement(file, statementNode, this.getDescription(message), this.getMetadata().key, this.conf.severity, fix));
}
if (this.conf.defaultKey
&& (this.reg.getConfig().getVersion() >= version_1.Version.v740sp02
|| this.reg.getConfig().getVersion() === version_1.Version.Cloud)
&& (statement instanceof Statements.Data || statement instanceof Statements.Type)) {
const tt = (_a = statementNode.findFirstExpression(expressions_1.TypeTable)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(expressions_1.TypeTableKey);
const token = tt === null || tt === void 0 ? void 0 : tt.findDirectTokenByText("DEFAULT");
if (tt && token) {
const next = statements[i + 1];
if ((next === null || next === void 0 ? void 0 : next.get()) instanceof _statement_1.Comment && next.concatTokens().includes("EC DEFAULT_KEY")) {
continue;
}
message = "DEFAULT KEY";
issues.push(issue_1.Issue.atToken(file, token, this.getDescription(message), this.getMetadata().key, this.conf.severity));
}
}
}
return issues;
}
getDescribeLinesFix(file, statementNode) {
const children = statementNode.getChildren();
const target = children[4].concatTokens();
const source = children[2].concatTokens();
const startPosition = children[0].getFirstToken().getStart();
const insertText = target + " = lines( " + source + " ).";
const deleteFix = edit_helper_1.EditHelper.deleteStatement(file, statementNode);
const insertFix = edit_helper_1.EditHelper.insertAt(file, startPosition, insertText);
const finalFix = edit_helper_1.EditHelper.merge(deleteFix, insertFix);
return finalFix;
}
}
exports.AvoidUse = AvoidUse;
//# sourceMappingURL=avoid_use.js.map