UNPKG

@abaplint/core

Version:
93 lines (91 loc) 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UseNew = exports.UseNewConf = void 0; const issue_1 = require("../issue"); const Statements = require("../abap/2_statements/statements"); const Expressions = require("../abap/2_statements/expressions"); 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 version_1 = require("../version"); const _irule_1 = require("./_irule"); const edit_helper_1 = require("../edit_helper"); class UseNewConf extends _basic_rule_config_1.BasicRuleConfig { } exports.UseNewConf = UseNewConf; class UseNew extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new UseNewConf(); } getMetadata() { return { key: "use_new", title: "Use NEW", shortDescription: `Checks for deprecated CREATE OBJECT statements.`, extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-new-to-create-object If the target variable is referenced in the CREATE OBJECT statement, no errors are issued Applicable from v740sp02 and up`, badExample: `CREATE OBJECT ref.`, goodExample: `ref = NEW #( ).`, tags: [_irule_1.RuleTag.Upport, _irule_1.RuleTag.Styleguide, _irule_1.RuleTag.Quickfix, _irule_1.RuleTag.SingleFile], }; } getMessage() { return "Use NEW #( ) to instantiate object."; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file, obj) { var _a; const issues = []; if (obj.getType() === "INTF") { return []; } if (this.reg.getConfig().getVersion() < version_1.Version.v740sp02 && this.reg.getConfig().getVersion() !== version_1.Version.Cloud) { return []; } for (const statement of file.getStatements()) { if (statement.get() instanceof Statements.CreateObject) { if (statement.findFirstExpression(expressions_1.Dynamic)) { continue; } else if (statement.findDirectExpression(expressions_1.ParameterListExceptions)) { continue; } else if (statement.findDirectTokenByText("AREA")) { continue; } const target = ((_a = statement.findDirectExpression(expressions_1.Target)) === null || _a === void 0 ? void 0 : _a.concatTokens()) + "->"; if (statement.concatTokens().includes(target)) { continue; } const fix = this.buildFix(file, statement); const issue = issue_1.Issue.atPosition(file, statement.getStart(), this.getMessage(), this.getMetadata().key, this.conf.severity, fix); issues.push(issue); } } return issues; } buildFix(file, statement) { var _a, _b; const target = (_a = statement.findDirectExpression(Expressions.Target)) === null || _a === void 0 ? void 0 : _a.concatTokens(); if (target === undefined) { return undefined; } const parameters = statement.findDirectExpression(Expressions.ParameterListS); const param = parameters ? parameters.concatTokens() + " " : ""; let type = (_b = statement.findDirectExpression(Expressions.ClassName)) === null || _b === void 0 ? void 0 : _b.getFirstToken().getStr(); if (type === undefined) { type = "#"; } const string = `${target} = NEW ${type}( ${param}).`; return edit_helper_1.EditHelper.replaceRange(file, statement.getStart(), statement.getEnd(), string); } } exports.UseNew = UseNew; //# sourceMappingURL=use_new.js.map