UNPKG

@abaplint/core

Version:
58 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FixCase = void 0; const nodes_1 = require("../abap/nodes"); const tokens_1 = require("../abap/1_lexer/tokens"); const keyword_case_1 = require("../rules/keyword_case"); const Tokens = require("../abap/1_lexer/tokens"); class FixCase { constructor(fileContents, config) { this.keywordCase = new keyword_case_1.KeywordCase(); this.keywordCase.setConfig(config.readByRule(this.keywordCase.getMetadata().key)); this.fileContents = fileContents; this.config = config; } execute(statement) { for (const child of statement.getChildren()) { if (child instanceof nodes_1.TokenNodeRegex) { const token = child.get(); if (token instanceof Tokens.StringToken) { continue; } this.replaceString(token.getStart(), this.formatNonKeyword(token.getStr())); continue; } else if (child instanceof nodes_1.TokenNode) { const token = child.get(); const str = token.getStr(); if (this.keywordCase.violatesRule(str) && token instanceof tokens_1.Identifier) { this.replaceString(token.getStart(), this.formatKeyword(str)); } } else if (child instanceof nodes_1.ExpressionNode) { this.execute(child); } else { throw new Error("pretty printer, traverse, unexpected node type"); } } return this.fileContents; } formatNonKeyword(str) { return str.toLowerCase(); } formatKeyword(keyword) { const ruleKey = this.keywordCase.getMetadata().key; const rule = this.config.readByRule(ruleKey); const style = rule ? rule["style"] : keyword_case_1.KeywordCaseStyle.Upper; return style === keyword_case_1.KeywordCaseStyle.Lower ? keyword.toLowerCase() : keyword.toUpperCase(); } replaceString(pos, str) { const lines = this.fileContents.split("\n"); const line = lines[pos.getRow() - 1]; lines[pos.getRow() - 1] = line.substr(0, pos.getCol() - 1) + str + line.substr(pos.getCol() + str.length - 1); this.fileContents = lines.join("\n"); } } exports.FixCase = FixCase; //# sourceMappingURL=fix_keyword_case.js.map