Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #196 from buzinas/ter-indent
Browse files Browse the repository at this point in the history
[feat] added fixer to ter-indent (closes #185)
  • Loading branch information
jmlopez-rod authored Mar 16, 2017
2 parents 676698b + 7d0325c commit fb70d42
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/rules/terIndentRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function isOneOf(node: ts.Node, kinds: string[]) {
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: RULE_NAME,
hasFix: true,
description: 'enforce consistent indentation',
rationale: Lint.Utils.dedent`
Using only one of tabs or spaces for indentation leads to more consistent editor behavior,
Expand Down Expand Up @@ -316,7 +317,10 @@ class IndentWalker extends Lint.RuleWalker {
}
const msg = this.createErrorMessage(needed, gottenSpaces, gottenTabs);
const width = gottenSpaces + gottenTabs;
this.addFailure(this.createFailure((loc !== undefined ? loc : node.getStart()) - width, width, msg));
const start = (loc !== undefined ? loc : node.getStart()) - width;
const desiredIndent = ((indentType === 'space' ? ' ' : '\t') as any).repeat(needed);
const fix = this.createFix(this.createReplacement(start, width, desiredIndent));
this.addFailure(this.createFailure(start, width, msg, fix));
}

/**
Expand Down Expand Up @@ -390,6 +394,7 @@ class IndentWalker extends Lint.RuleWalker {
const tabs = indentChars.filter(char => char === '\t').length;

return {
contentStart: pos + spaces + tabs + 1,
firstInLine: spaces + tabs === str.length || this._firstInLineCommentHelper(node),
space: spaces,
tab: tabs,
Expand All @@ -406,7 +411,7 @@ class IndentWalker extends Lint.RuleWalker {
(actualIndent.goodChar !== neededIndent || actualIndent.badChar !== 0) &&
actualIndent.firstInLine
) {
this.report(node, neededIndent, actualIndent.space, actualIndent.tab);
this.report(node, neededIndent, actualIndent.space, actualIndent.tab, actualIndent.contentStart);
}

if (isKind(node, 'IfStatement')) {
Expand Down Expand Up @@ -556,7 +561,7 @@ class IndentWalker extends Lint.RuleWalker {
const startIndent = this.getNodeIndent(node);
const firstInLine = startIndent.firstInLine;
if (firstInLine && (startIndent.goodChar !== firstLineIndent || startIndent.badChar !== 0)) {
this.report(node, firstLineIndent, startIndent.space, startIndent.tab);
this.report(node, firstLineIndent, startIndent.space, startIndent.tab, startIndent.contentStart);
}
}

Expand Down
27 changes: 26 additions & 1 deletion src/test/rules/terIndentRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RuleTester, Failure, Position, dedent, readFixture } from './ruleTester
// TODO: Split tests into better categories for easy finding

type NumStr = number | string;
const ruleTester = new RuleTester('ter-indent');
const ruleTester = new RuleTester('ter-indent', true);

function expecting(errors: [[number, NumStr, NumStr]], indentType: string = 'space'): Failure[] {
return errors.map((err) => {
Expand Down Expand Up @@ -2892,6 +2892,18 @@ ruleTester.addTestGroup('methods', 'should handle methods body and parameters',
bar();
}
}`,
output: dedent`
class A {
constructor(
aaa,
bbb,
ccc,
ddd
)
{
bar();
}
}`,
options: [2, { FunctionExpression: { parameters: 1, body: 2 } }],
errors: expecting([
[3, 4, 3],
Expand Down Expand Up @@ -3293,6 +3305,11 @@ ruleTester.addTestGroup('interfaces', 'should check indentation on interfaces',
a: number;
}
`,
output: dedent`
interface Foo {
a: number;
}
`,
errors: expecting([[2, 4, 2]])
},
{
Expand All @@ -3304,6 +3321,14 @@ ruleTester.addTestGroup('interfaces', 'should check indentation on interfaces',
};
}
`,
output: dedent`
interface Foo extends Bar {
a: number;
b: {
c: string;
};
}
`,
errors: expecting([
[2, 4, 2],
[4, 8, 7],
Expand Down

0 comments on commit fb70d42

Please sign in to comment.