Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

[Breaking change] Remove createFix, and just use the Replacement(s) as the fix. #2356

Closed
wants to merge 2 commits into from

Conversation

andy-hanson
Copy link
Contributor

@andy-hanson andy-hanson commented Mar 16, 2017

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

Overview of change:

The Fix class is not really necessary. It's easier to just pass parameters to addFailure.
This could be viewed as a continuation of #1845. So now instead of this.addFailure(this.createFailure(..., this.createFix(foo))), you just this.addFailure(..., foo).

CHANGELOG.md entry:

[api] Remove createFix. Replacements should be passed directly into addFailure.

return this.innerRuleName;
}

get replacements() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get innerReplacements() { return this.innerInnerReplacements; } 😜

Copy link
Contributor

@ajafff ajafff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea.
There are many style changes included. That's no blocker, but distracts from the real changes.

@@ -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 {
Copy link
Contributor

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 to node.getStart (for performance)


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use arrayify?

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a rest parameter like createFix had

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 fix = optionalFix(); ctx.addFailureAtNode(node, failure, ...(fix ? fix : [])), instead of just ctx.addFailureAtNode(node, failure, optionalFix()).

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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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.sourceFile.end here and remove fileLength

Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajafff is right, there are a lot of stylistic changes here that detract from the PR and make it harder to review. it also increases the chance of merge conflicts as the PR waits for review (as there are now). overall I like this direction and appreciate the PR, but it will help us all to keep it smaller & more focused whenever possible.


constructor(private sourceFile: ts.SourceFile,
start: number,
end: number,
private failure: string,
private ruleName: string,
private fix?: Fix) {
fix?: Fix) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the inlined fix variable is hard to read, can you save it a variable before calling this method?

@andy-hanson
Copy link
Contributor Author

#2403 merged, so closing.

@andy-hanson andy-hanson closed this Apr 1, 2017
@andy-hanson andy-hanson deleted the fix branch April 1, 2017 01:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants