-
Notifications
You must be signed in to change notification settings - Fork 887
[Breaking change] Remove createFix
, and just use the Replacement(s) as the fix.
#2356
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,10 @@ export class Replacement { | |
return replacements.reduce((text, r) => r.apply(text), content); | ||
} | ||
|
||
public static replaceNode(node: ts.Node, text: string): Replacement { | ||
return this.replaceFromTo(node.getStart(), node.getEnd(), text); | ||
} | ||
|
||
public static replaceFromTo(start: number, end: number, text: string) { | ||
return new Replacement(start, end - start, text); | ||
} | ||
|
@@ -188,19 +192,10 @@ export class Fix { | |
return Replacement.applyAll(content, replacements); | ||
} | ||
|
||
constructor(private innerRuleName: string, private innerReplacements: Replacement[]) { | ||
} | ||
|
||
get ruleName() { | ||
return this.innerRuleName; | ||
} | ||
|
||
get replacements() { | ||
return this.innerReplacements; | ||
} | ||
constructor(readonly replacements: Replacement[]) {} | ||
|
||
public apply(content: string) { | ||
return Replacement.applyAll(content, this.innerReplacements); | ||
return Replacement.applyAll(content, this.replacements); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
|
@@ -240,19 +235,21 @@ export class RuleFailure { | |
private endPosition: RuleFailurePosition; | ||
private rawLines: string; | ||
private ruleSeverity: RuleSeverity; | ||
private fix?: Fix; | ||
|
||
constructor(private sourceFile: ts.SourceFile, | ||
start: number, | ||
end: number, | ||
private failure: string, | ||
private ruleName: string, | ||
private fix?: Fix) { | ||
fix?: Replacement | Replacement[]) { | ||
|
||
this.fileName = sourceFile.fileName; | ||
this.startPosition = this.createFailurePosition(start); | ||
this.endPosition = this.createFailurePosition(end); | ||
this.rawLines = sourceFile.text; | ||
this.ruleSeverity = "error"; | ||
this.fix = fix ? new Fix(Array.isArray(fix) ? fix : [fix]) : undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
} | ||
|
||
public getFileName() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
|
||
import * as ts from "typescript"; | ||
|
||
import {Fix, IOptions, Replacement, RuleFailure} from "../rule/rule"; | ||
import {IOptions, Replacement, RuleFailure} from "../rule/rule"; | ||
import {SyntaxWalker} from "./syntaxWalker"; | ||
import {IWalker} from "./walker"; | ||
|
||
|
@@ -65,7 +65,7 @@ export class RuleWalker extends SyntaxWalker implements IWalker { | |
} | ||
|
||
/** @deprecated Prefer `addFailureAt` and its variants. */ | ||
public createFailure(start: number, width: number, failure: string, fix?: Fix): RuleFailure { | ||
public createFailure(start: number, width: number, failure: string, fix?: Replacement | Replacement[]): RuleFailure { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer a rest parameter like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that at first, but that can make life difficult if you have a function returning optional fixes. You would need to use |
||
const from = (start > this.limit) ? this.limit : start; | ||
const to = ((start + width) > this.limit) ? this.limit : (start + width); | ||
return new RuleFailure(this.sourceFile, from, to, failure, this.ruleName, fix); | ||
|
@@ -77,17 +77,17 @@ export class RuleWalker extends SyntaxWalker implements IWalker { | |
} | ||
|
||
/** Add a failure with any arbitrary span. Prefer `addFailureAtNode` if possible. */ | ||
public addFailureAt(start: number, width: number, failure: string, fix?: Fix) { | ||
public addFailureAt(start: number, width: number, failure: string, fix?: Replacement | Replacement[]) { | ||
this.addFailure(this.createFailure(start, width, failure, fix)); | ||
} | ||
|
||
/** Like `addFailureAt` but uses start and end instead of start and width. */ | ||
public addFailureFromStartToEnd(start: number, end: number, failure: string, fix?: Fix) { | ||
public addFailureFromStartToEnd(start: number, end: number, failure: string, fix?: Replacement | Replacement[]) { | ||
this.addFailureAt(start, end - start, failure, fix); | ||
} | ||
|
||
/** Add a failure using a node's span. */ | ||
public addFailureAtNode(node: ts.Node, failure: string, fix?: Fix) { | ||
public addFailureAtNode(node: ts.Node, failure: string, fix?: Replacement | Replacement[]) { | ||
this.addFailureAt(node.getStart(this.sourceFile), node.getWidth(this.sourceFile), failure, fix); | ||
} | ||
|
||
|
@@ -110,8 +110,4 @@ export class RuleWalker extends SyntaxWalker implements IWalker { | |
public getRuleName(): string { | ||
return this.ruleName; | ||
} | ||
|
||
public createFix(...replacements: Replacement[]): Fix { | ||
return new Fix(this.ruleName, replacements); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,31 +17,27 @@ | |
|
||
import * as ts from "typescript"; | ||
|
||
import { Fix, Replacement, RuleFailure } from "../rule/rule"; | ||
import { Replacement, RuleFailure } from "../rule/rule"; | ||
|
||
export class WalkContext<T> { | ||
public readonly failures: RuleFailure[] = []; | ||
|
||
constructor(public readonly sourceFile: ts.SourceFile, public readonly ruleName: string, public readonly options: T) {} | ||
|
||
/** Add a failure with any arbitrary span. Prefer `addFailureAtNode` if possible. */ | ||
public addFailureAt(start: number, width: number, failure: string, fix?: Fix) { | ||
public addFailureAt(start: number, width: number, failure: string, fix?: Replacement | Replacement[]) { | ||
this.addFailure(start, start + width, failure, fix); | ||
} | ||
|
||
public addFailure(start: number, end: number, failure: string, fix?: Fix) { | ||
public addFailure(start: number, end: number, failure: string, fix?: Replacement | Replacement[]) { | ||
const fileLength = this.sourceFile.end; | ||
this.failures.push( | ||
new RuleFailure(this.sourceFile, Math.min(start, fileLength), Math.min(end, fileLength), failure, this.ruleName, fix), | ||
); | ||
start = Math.min(start, fileLength); | ||
end = Math.min(end, fileLength); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are already changing the style of the code, you could inline |
||
this.failures.push(new RuleFailure(this.sourceFile, start, end, failure, this.ruleName, fix)); | ||
} | ||
|
||
/** Add a failure using a node's span. */ | ||
public addFailureAtNode(node: ts.Node, failure: string, fix?: Fix) { | ||
public addFailureAtNode(node: ts.Node, failure: string, fix?: Replacement | Replacement[]) { | ||
this.addFailure(node.getStart(this.sourceFile), node.getEnd(), failure, fix); | ||
} | ||
|
||
public createFix(...replacements: Replacement[]) { | ||
return new Fix(this.ruleName, replacements); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,13 +108,9 @@ class AlignWalker extends Lint.RuleWalker { | |
const curPos = this.getLineAndCharacterOfPosition(node.getStart()); | ||
if (curPos.line !== prevPos.line && curPos.character !== alignToColumn) { | ||
const diff = alignToColumn - curPos.character; | ||
let fix; | ||
if (0 < diff) { | ||
fix = this.createFix(this.appendText(node.getStart(), " ".repeat(diff))); | ||
} else { | ||
fix = this.createFix(this.deleteText(node.getStart() + diff, -diff)); | ||
} | ||
this.addFailureAtNode(node, kind + Rule.FAILURE_STRING_SUFFIX, fix); | ||
this.addFailureAtNode(node, kind + Rule.FAILURE_STRING_SUFFIX, 0 < diff | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO the inlined |
||
? this.appendText(node.getStart(), " ".repeat(diff)) | ||
: this.deleteText(node.getStart() + diff, -diff)); | ||
} | ||
prevPos = curPos; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add an optional
sourceFile
parameter, that will be passed tonode.getStart
(for performance)