Skip to content

Commit

Permalink
alightTryCatch indent rule option (#349)
Browse files Browse the repository at this point in the history
* gitignore adjustment

* pretty printer with params

* alignTryCatch intentation opt

* linter fixes

* reverse test refactorings
  • Loading branch information
sbcgua authored and larshp committed Feb 5, 2019
1 parent f47256e commit bcccde3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -8,4 +8,6 @@ build/
coverage/
npm-debug.log
.alm/
dump.json
dump.json
.vscode/
.nyc_output/
16 changes: 11 additions & 5 deletions src/abap/pretty_printer.ts
Expand Up @@ -7,6 +7,10 @@ import {Position} from "../position";

// todo, will break if there is multiple statements per line?

export interface IndentationOptions {
alignTryCatch?: boolean;
}

class Stack {
private items: number[] = [];

Expand All @@ -27,7 +31,7 @@ class Stack {

class Indentation {
// returns list of expected indentation for each line/statement?
public static run(file: ABAPFile): number[] {
public static run(file: ABAPFile, options: IndentationOptions): number[] {
const ret: number[] = [];
const init: number = 1;
let indent: number = init;
Expand Down Expand Up @@ -75,7 +79,7 @@ class Indentation {
|| type instanceof Statements.When) {
indent = stack.peek();
} else if (type instanceof Statements.EndTry) {
indent = stack.pop() - 4;
indent = stack.pop() - (options.alignTryCatch ? 2 : 4);
} else if (type instanceof Statements.EndClass
|| type instanceof Statements.EndCase) {
indent = stack.pop() - 2;
Expand Down Expand Up @@ -116,7 +120,7 @@ class Indentation {
|| type instanceof Statements.Private) {
indent = indent + 2;
} else if (type instanceof Statements.Try) {
indent = indent + 4;
indent = indent + (options.alignTryCatch ? 2 : 4);
stack.push(indent);
} else if (type instanceof Statements.ClassDefinition
|| type instanceof Statements.Case
Expand All @@ -133,10 +137,12 @@ class Indentation {
export class PrettyPrinter {
private result: string;
private file: ABAPFile;
private options: IndentationOptions;

constructor(file: ABAPFile) {
constructor(file: ABAPFile, options?: IndentationOptions) {
this.result = file.getRaw();
this.file = file;
this.options = options || {};
}

public run(): string {
Expand All @@ -156,7 +162,7 @@ export class PrettyPrinter {
}

public getExpectedIndentation(): number[] {
return Indentation.run(this.file);
return Indentation.run(this.file, this.options);
}

private indentCode() {
Expand Down
8 changes: 6 additions & 2 deletions src/rules/indentation.ts
Expand Up @@ -5,10 +5,11 @@ import {IObject} from "../objects/_iobject";
import {Class} from "../objects";
import {Registry} from "../registry";
import {BasicRuleConfig} from "./_basic_rule_config";
import {PrettyPrinter} from "../abap/pretty_printer";
import {PrettyPrinter, IndentationOptions} from "../abap/pretty_printer";

export class IndentationConf extends BasicRuleConfig {
public ignoreExceptions: boolean = true;
public alignTryCatch: boolean = false;
}

export class Indentation extends ABAPRule {
Expand Down Expand Up @@ -44,7 +45,10 @@ export class Indentation extends ABAPRule {
}
}

const expected = new PrettyPrinter(file).getExpectedIndentation();
const indentOpts: IndentationOptions = {
alignTryCatch: this.conf.alignTryCatch,
};
const expected = new PrettyPrinter(file, indentOpts).getExpectedIndentation();

for (const statement of file.getStatements()) {
const position = statement.getFirstToken().getPos();
Expand Down
32 changes: 28 additions & 4 deletions test/abap/pretty_printer.ts
Expand Up @@ -3,6 +3,8 @@ import {PrettyPrinter} from "../../src/abap/pretty_printer";
import {MemoryFile} from "../../src/files";
import {Registry} from "../../src/registry";

const testTitle = (text: string): string => { return text.split("\n")[0]; };

describe("Pretty printer, keywords upper case", () => {
const tests = [
{input: "REPORT zfoo.", expected: "REPORT zfoo."},
Expand All @@ -13,7 +15,7 @@ describe("Pretty printer, keywords upper case", () => {
];

tests.forEach((test) => {
it(test.input, () => {
it(testTitle(test.input), () => {
const reg = new Registry().addFile(new MemoryFile("zfoo.prog.abap", test.input)).parse();
expect(reg.getABAPFiles().length).to.equal(1);
const result = new PrettyPrinter(reg.getABAPFiles()[0]).run();
Expand All @@ -31,7 +33,7 @@ describe("Pretty printer, indent code", () => {
];

tests.forEach((test) => {
it(test.input, () => {
it(testTitle(test.input), () => {
const reg = new Registry().addFile(new MemoryFile("zfoo.prog.abap", test.input)).parse();
expect(reg.getABAPFiles().length).to.equal(1);
const result = new PrettyPrinter(reg.getABAPFiles()[0]).run();
Expand All @@ -49,11 +51,33 @@ describe("Pretty printer, expected indentation", () => {
];

tests.forEach((test) => {
it(test.input, () => {
it(testTitle(test.input), () => {
const reg = new Registry().addFile(new MemoryFile("zfoo.prog.abap", test.input)).parse();
expect(reg.getABAPFiles().length).to.equal(1);
const result = new PrettyPrinter(reg.getABAPFiles()[0]).getExpectedIndentation();
expect(result).to.deep.equal(test.expected);
});
});
});
});

describe("Pretty printer with alignTryCatch", () => {
const tests = [
{
input: "try. \"no align\nwrite moo.\ncatch cx_root.\nwrite err.\nendtry.",
expected: [1, -1, 5, 3, 5, 1],
},
{
input: "try. \"with align\nwrite moo.\ncatch cx_root.\nwrite err.\nendtry.",
expected: [1, -1, 3, 1, 3, 1],
options: {alignTryCatch: true}},
];

tests.forEach((test) => {
it(testTitle(test.input), () => {
const reg = new Registry().addFile(new MemoryFile("zfoo.prog.abap", test.input)).parse();
expect(reg.getABAPFiles().length).to.equal(1);
const result = new PrettyPrinter(reg.getABAPFiles()[0], test.options).getExpectedIndentation();
expect(result).to.deep.equal(test.expected);
});
});
});

0 comments on commit bcccde3

Please sign in to comment.