UNPKG

@abaplint/core

Version:
190 lines (188 loc) • 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NoPrefixes = exports.NoPrefixesConf = void 0; const issue_1 = require("../issue"); const _basic_rule_config_1 = require("./_basic_rule_config"); const _abap_rule_1 = require("./_abap_rule"); const Expressions = require("../abap/2_statements/expressions"); const Statements = require("../abap/2_statements/statements"); const _irule_1 = require("./_irule"); class NoPrefixesConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); /** DATA, CLASS-DATA, DATA BEGIN OF, CLASS-DATA BEGIN OF, FINAL(), DATA(), case insensitive regex */ this.data = "^[LGM].?_"; /** STATICS, STATICS BEGIN OF, case insensitive regex */ this.statics = "^S.?_"; /** FIELD-SYMBOLS and inline FIELD-SYMBOLS(), case insensitive regex */ this.fieldSymbols = "^<[LGM].?_"; /** CONSTANTS, CONSTANTS BEGIN OF, case insensitive regex */ this.constants = "^[LGM]?C.?_"; /** TYPES, ENUM, MESH, case insensitive regex */ this.types = "^TY_"; /** importing, exporting, returning and changing parameters, case insensitive regex */ this.methodParameters = "^[ICER].?_"; this.allowIsPrefixBoolean = true; // todo, public localClass: string = ""; // todo, public localInterface: string = ""; // todo, public functionModuleParameters: string = ""; // todo, public parameters: string = ""; // todo, public selectOptions: string = ""; // todo, public formParameters: string = ""; } } exports.NoPrefixesConf = NoPrefixesConf; const MESSAGE = "Avoid hungarian notation"; class NoPrefixes extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new NoPrefixesConf(); } getMetadata() { return { key: "no_prefixes", title: "No Prefixes", shortDescription: `Dont use hungarian notation`, extendedInformation: ` Note: not prefixing TYPES will require changing the errorNamespace in the abaplint configuration, allowing all types to become voided, abaplint will then provide less precise syntax errors. https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#avoid-encodings-esp-hungarian-notation-and-prefixes https://github.com/SAP/styleguides/blob/main/clean-abap/sub-sections/AvoidEncodings.md`, tags: [_irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Styleguide], badExample: `DATA lv_foo TYPE i.`, goodExample: `DATA foo TYPE i.`, }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { const ret = []; const config = this.getConfig(); const structure = file.getStructure(); if (structure === undefined) { // syntax error, skip return []; } if (config.data !== undefined && config.data !== "") { ret.push(...this.checkData(structure, new RegExp(config.data, "i"), file)); } if (config.statics !== undefined && config.statics !== "") { ret.push(...this.checkStatics(structure, new RegExp(config.statics, "i"), file)); } if (config.fieldSymbols !== undefined && config.fieldSymbols !== "") { ret.push(...this.checkFieldSymbols(structure, new RegExp(config.fieldSymbols, "i"), file)); } if (config.constants !== undefined && config.constants !== "") { ret.push(...this.checkConstants(structure, new RegExp(config.constants, "i"), file)); } if (config.types !== undefined && config.types !== "") { ret.push(...this.checkTypes(structure, new RegExp(config.types, "i"), file)); } if (config.methodParameters !== undefined && config.methodParameters !== "") { ret.push(...this.checkMethodParameters(structure, new RegExp(config.methodParameters, "i"), file)); } return ret; } checkData(topNode, regex, file) { const ret = []; for (const data of topNode.findAllStatements(Statements.Data).concat(topNode.findAllStatements(Statements.DataBegin)).concat(topNode.findAllStatements(Statements.ClassDataBegin)).concat(topNode.findAllStatements(Statements.ClassData))) { const nameExpression = data.findFirstExpression(Expressions.DefinitionName) || data.findFirstExpression(Expressions.NamespaceSimpleName); const name = (nameExpression === null || nameExpression === void 0 ? void 0 : nameExpression.concatTokens()) || ""; if (name !== "" && nameExpression && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } for (const data of topNode.findAllExpressions(Expressions.InlineData)) { const nameExpression = data.findFirstExpression(Expressions.TargetField); const name = (nameExpression === null || nameExpression === void 0 ? void 0 : nameExpression.concatTokens()) || ""; if (name !== "" && nameExpression && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } return ret; } checkStatics(topNode, regex, file) { const ret = []; for (const data of topNode.findAllStatements(Statements.Static).concat(topNode.findAllStatements(Statements.StaticBegin))) { const nameExpression = data.findFirstExpression(Expressions.DefinitionName); const name = (nameExpression === null || nameExpression === void 0 ? void 0 : nameExpression.concatTokens()) || ""; if (name !== "" && nameExpression && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } return ret; } checkFieldSymbols(topNode, regex, file) { const ret = []; for (const data of topNode.findAllStatements(Statements.FieldSymbol)) { const nameExpression = data.findFirstExpression(Expressions.FieldSymbol); const name = (nameExpression === null || nameExpression === void 0 ? void 0 : nameExpression.concatTokens()) || ""; if (name !== "" && nameExpression && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } for (const data of topNode.findAllExpressions(Expressions.InlineFS)) { const nameExpression = data.findFirstExpression(Expressions.FieldSymbol); const name = (nameExpression === null || nameExpression === void 0 ? void 0 : nameExpression.concatTokens()) || ""; if (name !== "" && nameExpression && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } return ret; } checkConstants(topNode, regex, file) { const ret = []; for (const data of topNode.findAllStatements(Statements.Constant).concat(topNode.findAllStatements(Statements.ConstantBegin))) { const nameExpression = data.findFirstExpression(Expressions.DefinitionName); const name = (nameExpression === null || nameExpression === void 0 ? void 0 : nameExpression.concatTokens()) || ""; if (name !== "" && nameExpression && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } return ret; } checkTypes(topNode, regex, file) { const ret = []; for (const data of topNode.findAllStatements(Statements.Type).concat(topNode.findAllStatements(Statements.TypeEnum)).concat(topNode.findAllStatements(Statements.TypeEnumBegin)).concat(topNode.findAllStatements(Statements.TypeMesh)).concat(topNode.findAllStatements(Statements.TypeMeshBegin)).concat(topNode.findAllStatements(Statements.TypeBegin))) { const nameExpression = data.findFirstExpression(Expressions.NamespaceSimpleName); const name = (nameExpression === null || nameExpression === void 0 ? void 0 : nameExpression.concatTokens()) || ""; if (name !== "" && nameExpression && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } return ret; } checkMethodParameters(topNode, regex, file) { var _a, _b; const ret = []; for (const method of topNode.findAllStatements(Statements.MethodDef)) { for (const param of method.findAllExpressionsMulti([Expressions.MethodDefReturning, Expressions.MethodParam])) { const nameToken = param === null || param === void 0 ? void 0 : param.findFirstExpression(Expressions.MethodParamName); const type = (_b = (_a = param === null || param === void 0 ? void 0 : param.findFirstExpression(Expressions.TypeParam)) === null || _a === void 0 ? void 0 : _a.concatTokens()) === null || _b === void 0 ? void 0 : _b.toUpperCase(); if (this.getConfig().allowIsPrefixBoolean === true && (type === null || type === void 0 ? void 0 : type.endsWith("TYPE ABAP_BOOL"))) { continue; } const name = nameToken === null || nameToken === void 0 ? void 0 : nameToken.concatTokens(); if (nameToken && name && name !== "" && name.match(regex)) { const issue = issue_1.Issue.atToken(file, nameToken.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity); ret.push(issue); } } } return ret; } } exports.NoPrefixes = NoPrefixes; //# sourceMappingURL=no_prefixes.js.map