Skip to content

Commit

Permalink
2.58.12, definitions_top, quick fix (#1486)
Browse files Browse the repository at this point in the history
* definitions_top, quick fix, #1477

* 2.58.12
  • Loading branch information
larshp committed Oct 24, 2020
1 parent a0b0526 commit 5c33e91
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/cli/package.json
@@ -1,6 +1,6 @@
{
"name": "@abaplint/cli",
"version": "2.58.11",
"version": "2.58.12",
"description": "abaplint - Command Line Interface",
"bin": {
"abaplint": "./abaplint"
Expand Down Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://abaplint.org",
"devDependencies": {
"@abaplint/core": "^2.58.11",
"@abaplint/core": "^2.58.12",
"@types/chai": "^4.2.14",
"@types/glob": "^7.1.3",
"@types/minimist": "^1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/package.json
@@ -1,6 +1,6 @@
{
"name": "@abaplint/core",
"version": "2.58.11",
"version": "2.58.12",
"description": "abaplint - Core API",
"main": "build/src/index.js",
"typings": "build/abaplint.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/scripts/schema.json
Expand Up @@ -1405,7 +1405,7 @@
"type": "boolean"
}
],
"description": "Checks that definitions are placed at the beginning of methods.\nTags: SingleFile\nhttps://rules.abaplint.org/definitions_top"
"description": "Checks that definitions are placed at the beginning of methods.\nTags: SingleFile, Quickfix\nhttps://rules.abaplint.org/definitions_top"
},
"description_empty": {
"anyOf": [
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/rules/definitions_top.ts
Expand Up @@ -5,6 +5,8 @@ import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper, IEdit} from "../edit_helper";
import {StatementNode} from "../abap/nodes/statement_node";

export class DefinitionsTopConf extends BasicRuleConfig {
}
Expand All @@ -25,7 +27,7 @@ export class DefinitionsTop extends ABAPRule {
title: "Place definitions in top of routine",
shortDescription: `Checks that definitions are placed at the beginning of methods.`,
extendedInformation: `https://docs.abapopenchecks.org/checks/17/`,
tags: [RuleTag.SingleFile],
tags: [RuleTag.SingleFile, RuleTag.Quickfix],
};
}

Expand All @@ -45,13 +47,15 @@ export class DefinitionsTop extends ABAPRule {
const issues: Issue[] = [];

let mode = ANY;
let start: StatementNode | undefined = undefined;
let issue: Issue | undefined = undefined;

// todo, this needs refactoring when the paser has become better
for (const statement of file.getStatements()) {
if (statement.get() instanceof Statements.Form
|| statement.get() instanceof Statements.Method) {
mode = DEFINITION;
start = statement;
issue = undefined;
} else if (statement.get() instanceof Comment) {
continue;
Expand All @@ -78,7 +82,8 @@ export class DefinitionsTop extends ABAPRule {
|| statement.get() instanceof Statements.StaticEnd
|| statement.get() instanceof Statements.FieldSymbol) {
if (mode === AFTER) {
issue = Issue.atStatement(file, statement, this.getMessage(), this.getMetadata().key, this.conf.severity);
const fix = issues.length === 0 && start ? this.buildFix(file, statement, start) : undefined;
issue = Issue.atStatement(file, statement, this.getMessage(), this.getMetadata().key, this.conf.severity, fix);
mode = ANY;
}
} else if (statement.get() instanceof Statements.Define
Expand All @@ -91,4 +96,15 @@ export class DefinitionsTop extends ABAPRule {

return issues;
}

//////////////////

private buildFix(file: ABAPFile, statement: StatementNode, start: StatementNode): IEdit {
const concat = statement.concatTokens();

const fix1 = EditHelper.deleteStatement(file, statement);
const fix2 = EditHelper.insertAt(file, start.getEnd(), "\n" + concat);

return EditHelper.merge(fix1, fix2);
}
}
38 changes: 36 additions & 2 deletions packages/core/test/rules/definitions_top.ts
@@ -1,5 +1,9 @@
import {DefinitionsTop} from "../../src/rules/definitions_top";
import {testRule} from "./_utils";
import {testRule, testRuleFixSingle} from "./_utils";

function testFix(input: string, expected: string) {
testRuleFixSingle(input, expected, new DefinitionsTop());
}

const tests = [
{
Expand Down Expand Up @@ -84,4 +88,34 @@ ENDFORM.`,

];

testRule(tests, DefinitionsTop);
testRule(tests, DefinitionsTop);


describe("Rule: definitions_top", () => {

it("quick fix 1", async () => {
const abap = `CLASS lcl_bar DEFINITION.
PUBLIC SECTION.
METHODS bar.
ENDCLASS.
CLASS lcl_bar IMPLEMENTATION.
METHOD bar.
WRITE 2.
DATA foo TYPE c.
ENDMETHOD.
ENDCLASS.`;
const expected = `CLASS lcl_bar DEFINITION.
PUBLIC SECTION.
METHODS bar.
ENDCLASS.
CLASS lcl_bar IMPLEMENTATION.
METHOD bar.
DATA foo TYPE c.
WRITE 2.
` + " " + `
ENDMETHOD.
ENDCLASS.`;
testFix(abap, expected);
});

});
8 changes: 4 additions & 4 deletions packages/monaco/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/monaco/package.json
@@ -1,6 +1,6 @@
{
"name": "@abaplint/monaco",
"version": "2.58.11",
"version": "2.58.12",
"description": "ABAP language support for Monaco Editor",
"main": "build/index.js",
"typings": "build/index.d.ts",
Expand All @@ -24,6 +24,6 @@
"vscode-languageserver-types": "^3.15.1"
},
"dependencies": {
"@abaplint/core": "^2.58.11"
"@abaplint/core": "^2.58.12"
}
}
14 changes: 7 additions & 7 deletions web/playground/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/playground/package.json
Expand Up @@ -21,7 +21,7 @@
"vscode-languageserver-types": "^3.15.1"
},
"devDependencies": {
"@abaplint/monaco": "^2.58.11",
"@abaplint/monaco": "^2.58.12",
"css-loader": "^5.0.0",
"file-loader": "^6.1.1",
"html-webpack-plugin": "^4.5.0",
Expand Down

0 comments on commit 5c33e91

Please sign in to comment.