@abaplint/core
Version:
abaplint - Core API
264 lines (261 loc) • 9.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnusedVariables = exports.UnusedVariablesConf = void 0;
const issue_1 = require("../issue");
const _basic_rule_config_1 = require("./_basic_rule_config");
const _irule_1 = require("./_irule");
const syntax_1 = require("../abap/5_syntax/syntax");
const _abap_object_1 = require("../objects/_abap_object");
const _scope_type_1 = require("../abap/5_syntax/_scope_type");
const objects_1 = require("../objects");
const edit_helper_1 = require("../edit_helper");
const Statements = require("../abap/2_statements/statements");
const _statement_1 = require("../abap/2_statements/statements/_statement");
const _reference_1 = require("../abap/5_syntax/_reference");
class UnusedVariablesConf extends _basic_rule_config_1.BasicRuleConfig {
constructor() {
super(...arguments);
/** skip specific names, case insensitive
* @uniqueItems true
*/
this.skipNames = [];
/** skip parameters from abstract methods */
this.skipAbstract = false;
}
}
exports.UnusedVariablesConf = UnusedVariablesConf;
class WorkArea {
constructor() {
this.workarea = [];
}
push(id, count = 1) {
for (const w of this.workarea) {
if (id.equals(w.id)) {
return;
}
}
this.workarea.push({ id, count });
}
removeIfExists(id) {
if (id === undefined) {
return;
}
for (let i = 0; i < this.workarea.length; i++) {
if (id.equals(this.workarea[i].id)) {
this.workarea[i].count--;
if (this.workarea[i].count === 0) {
this.workarea.splice(i, 1);
}
return;
}
}
}
get() {
return this.workarea;
}
count() {
return this.workarea.length;
}
}
class UnusedVariables {
constructor() {
this.conf = new UnusedVariablesConf();
}
getMetadata() {
return {
key: "unused_variables",
title: "Unused variables",
shortDescription: `Checks for unused variables and constants`,
extendedInformation: `Skips event parameters.
Note that this currently does not work if the source code uses macros.
Unused variables are not reported if the object contains parser or syntax errors.
Errors found in INCLUDES are reported for the main program.`,
tags: [_irule_1.RuleTag.Quickfix],
pragma: "##NEEDED",
pseudoComment: "EC NEEDED",
badExample: `DATA: BEGIN OF blah1,
test TYPE string,
test2 TYPE string,
END OF blah1.`,
goodExample: `DATA: BEGIN OF blah2 ##NEEDED,
test TYPE string,
test2 TYPE string,
END OF blah2.`,
};
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
if (this.conf.skipNames === undefined) {
this.conf.skipNames = [];
}
}
initialize(reg) {
this.reg = reg;
return this;
}
run(obj) {
if (!(obj instanceof _abap_object_1.ABAPObject)) {
return [];
}
else if (obj instanceof objects_1.Interface) { // todo, how to handle interfaces?
return [];
}
for (const file of obj.getABAPFiles()) {
for (const statement of file.getStatements()) {
if (statement.get() instanceof _statement_1.Unknown) {
return []; // contains parser errors
}
}
}
// dont report unused variables when there are syntax errors
const syntax = new syntax_1.SyntaxLogic(this.reg, obj).run();
if (syntax.issues.length > 0) {
return []; // contains syntax errors
}
this.workarea = new WorkArea();
const top = syntax.spaghetti.getTop();
this.buildWorkarea(top, obj);
if (this.workarea.count() === 0) {
return this.buildIssues(obj); // exit early if all variables are used
}
this.findUses(top, obj);
for (const o of this.reg.getObjects()) {
if (o === obj) {
continue;
}
else if (o instanceof _abap_object_1.ABAPObject) {
if (this.reg.isDependency(o)) {
continue; // do not search in dependencies
}
const syntax = new syntax_1.SyntaxLogic(this.reg, o).run();
this.findUses(syntax.spaghetti.getTop(), o);
if (this.workarea.count() === 0) {
return this.buildIssues(obj); // exit early if all variables are used
}
}
}
return this.buildIssues(obj);
}
findUses(node, obj) {
for (const r of node.getData().references) {
if (r.referenceType === _reference_1.ReferenceType.DataReadReference
|| r.referenceType === _reference_1.ReferenceType.DataWriteReference
|| r.referenceType === _reference_1.ReferenceType.TypeReference) {
this.workarea.removeIfExists(r.resolved);
}
}
for (const c of node.getChildren()) {
this.findUses(c, obj);
}
}
buildWorkarea(node, obj) {
const stype = node.getIdentifier().stype;
if (stype === _scope_type_1.ScopeType.OpenSQL) {
return;
}
for (const c of node.getChildren()) {
this.buildWorkarea(c, obj);
}
if (stype !== _scope_type_1.ScopeType.BuiltIn) {
const vars = node.getData().vars;
for (const name in vars) {
const meta = vars[name].getMeta();
if (this.conf.skipNames
&& this.conf.skipNames.length > 0
&& this.conf.skipNames.some((a) => a.toUpperCase() === name)) {
continue;
}
else if (this.conf.skipAbstract === true && meta.includes("abstract" /* IdentifierMeta.Abstract */)) {
continue;
}
else if (name === "ME"
|| name === "SUPER"
|| meta.includes("selection_screen_tab" /* IdentifierMeta.SelectionScreenTab */)
|| meta.includes("event_parameter" /* IdentifierMeta.EventParameter */)) {
// todo, workaround for "me" and "super", these should somehow be typed to built-in
continue;
}
const isInline = meta.includes("inline" /* IdentifierMeta.InlineDefinition */);
this.workarea.push(vars[name], isInline ? 2 : 1);
}
}
}
buildIssues(obj) {
const ret = [];
for (const w of this.workarea.get()) {
const filename = w.id.getFilename();
if (this.reg.isFileDependency(filename) === true) {
continue;
}
else if (obj instanceof objects_1.Program === false && obj.containsFile(filename) === false) {
continue;
}
const statement = this.findStatement(w.id);
if (statement === null || statement === void 0 ? void 0 : statement.getPragmas().map(t => t.getStr()).includes(this.getMetadata().pragma + "")) {
continue;
}
else if (this.suppressedbyPseudo(statement, w.id, obj)) {
continue;
}
const name = w.id.getName();
const message = "Variable \"" + name.toLowerCase() + "\" not used";
const fix = this.buildFix(w.id, obj);
ret.push(issue_1.Issue.atIdentifier(w.id, message, this.getMetadata().key, this.conf.severity, fix));
}
return ret;
}
suppressedbyPseudo(statement, v, obj) {
if (statement === undefined) {
return false;
}
const file = obj.getABAPFileByName(v.getFilename());
if (file === undefined) {
return false;
}
let next = false;
for (const s of file.getStatements()) {
if (next === true && s.get() instanceof _statement_1.Comment) {
return s.concatTokens().includes(this.getMetadata().pseudoComment + "");
}
if (s === statement) {
next = true;
}
}
return false;
}
findStatement(v) {
const file = this.reg.getFileByName(v.getFilename());
if (file === undefined) {
return undefined;
}
const object = this.reg.findObjectForFile(file);
if (!(object instanceof _abap_object_1.ABAPObject)) {
return undefined;
}
const abapfile = object.getABAPFileByName(v.getFilename());
if (abapfile === undefined) {
return undefined;
}
const statement = edit_helper_1.EditHelper.findStatement(v.getToken(), abapfile);
return statement;
}
buildFix(v, obj) {
const file = obj.getABAPFileByName(v.getFilename());
if (file === undefined) {
return undefined;
}
const statement = edit_helper_1.EditHelper.findStatement(v.getToken(), file);
if (statement === undefined) {
return undefined;
}
else if (statement.get() instanceof Statements.Data) {
return edit_helper_1.EditHelper.deleteStatement(file, statement);
}
return undefined;
}
}
exports.UnusedVariables = UnusedVariables;
//# sourceMappingURL=unused_variables.js.map