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

Deprecate Rule.createFailure and Rule.addFailure #1952

Merged
merged 3 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/language/walker/ruleWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ export class RuleWalker extends SyntaxWalker {
this.position += node.getFullWidth();
}

/** @deprecated Prefer `addFailureAt` and its variants. */
public createFailure(start: number, width: number, failure: string, fix?: Fix): RuleFailure {
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);
}

/** @deprecated Prefer `addFailureAt` and its variants. */
public addFailure(failure: RuleFailure) {
// don't add failures for a rule if the failure intersects an interval where that rule is disabled
if (!this.existsFailure(failure) && !doesIntersect(failure, this.disabledIntervals)) {
Expand Down
19 changes: 13 additions & 6 deletions src/rules/noConsecutiveBlankLinesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ class NoConsecutiveBlankLinesWalker extends Lint.RuleWalker {
line > lastVal + 1 ? sequences.push([line]) : sequences[sequences.length - 1].push(line);
lastVal = line;
}
sequences
.filter((arr) => arr.length > allowedBlanks)
.map((arr) => arr[0])
.map((startLineNum: number) => this.createFailure(lineStarts[startLineNum + 1], 1, failureMessage))
.filter((failure) => !Lint.doesIntersect(failure, templateIntervals))
.forEach((failure) => this.addFailure(failure));

for (const arr of sequences) {
if (arr.length <= allowedBlanks) {
continue;
}

const startLineNum = arr[0];
const pos = lineStarts[startLineNum + 1];
const isInTemplate = templateIntervals.some((interval) => pos >= interval.startPosition && pos < interval.endPosition);
if (!isInTemplate) {
this.addFailureAt(pos, 1, failureMessage);
}
}
}

private getTemplateIntervals(sourceFile: ts.SourceFile): Lint.IDisabledInterval[] {
Expand Down
25 changes: 16 additions & 9 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

interface Failure {
start: number;
width: number;
message: string;
fix?: Lint.Fix;
}

class NoUnusedVariablesWalker extends Lint.RuleWalker {
private skipBindingElement: boolean;
private skipParameterDeclaration: boolean;
Expand All @@ -92,7 +99,7 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
private ignorePattern: RegExp;
private isReactUsed: boolean;
private reactImport: ts.NamespaceImport;
private possibleFailures: Lint.RuleFailure[] = [];
private possibleFailures: Failure[] = [];

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions,
private languageService: ts.LanguageService) {
Expand Down Expand Up @@ -139,15 +146,15 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
if (!this.isIgnored(nameText)) {
const start = this.reactImport.name.getStart();
const msg = Rule.FAILURE_STRING_FACTORY(Rule.FAILURE_TYPE_IMPORT, nameText);
this.possibleFailures.push(this.createFailure(start, nameText.length, msg));
this.possibleFailures.push({ start, width: nameText.length, message: msg });
}
}

let someFixBrokeIt = false;
// Performance optimization: type-check the whole file before verifying individual fixes
if (this.possibleFailures.some((f) => f.hasFix())) {
if (this.possibleFailures.some((f) => !!f.fix)) {
let newText = Lint.Fix.applyAll(this.getSourceFile().getFullText(),
this.possibleFailures.map((f) => f.getFix()).filter((f) => !!f));
this.possibleFailures.map((f) => f.fix).filter((f) => !!f));

// If we have the program, we can verify that the fix doesn't introduce failures
if (Lint.checkEdit(this.languageService, this.getSourceFile(), newText).length > 0) {
Expand All @@ -157,12 +164,12 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
}

this.possibleFailures.forEach((f) => {
if (!someFixBrokeIt || !f.hasFix()) {
this.addFailure(f);
if (!someFixBrokeIt || !f.fix) {
this.addFailureAt(f.start, f.width, f.message, f.fix);
} else {
let newText = f.getFix().apply(this.getSourceFile().getFullText());
let newText = f.fix.apply(this.getSourceFile().getFullText());
if (Lint.checkEdit(this.languageService, this.getSourceFile(), newText).length === 0) {
this.addFailure(f);
this.addFailureAt(f.start, f.width, f.message, f.fix);
}
}
});
Expand Down Expand Up @@ -446,7 +453,7 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
if (replacements && replacements.length) {
fix = new Lint.Fix(Rule.metadata.ruleName, replacements);
}
this.possibleFailures.push(this.createFailure(position, name.length, Rule.FAILURE_STRING_FACTORY(type, name), fix));
this.possibleFailures.push({ start: position, width: name.length, message: Rule.FAILURE_STRING_FACTORY(type, name), fix });
}

private isIgnored(name: string) {
Expand Down
27 changes: 6 additions & 21 deletions src/rules/spaceBeforeFunctionParenRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,15 @@ class FunctionWalker extends Lint.RuleWalker {
}

const hasSpace = this.isSpaceAt(openParen.getStart() - 1);
let failure: Lint.RuleFailure;

if (hasSpace && option === "never") {
failure = this.createInvalidWhitespaceFailure(openParen.getStart() - 1);
const pos = openParen.getStart() - 1;
const fix = new Lint.Fix(Rule.metadata.ruleName, [ this.deleteText(pos, 1) ]);
this.addFailureAt(pos, 1, Rule.INVALID_WHITESPACE_ERROR, fix);
} else if (!hasSpace && option === "always") {
failure = this.createMissingWhitespaceFailure(openParen.getStart());
}

if (failure !== undefined) {
this.addFailure(failure);
const pos = openParen.getStart();
const fix = new Lint.Fix(Rule.metadata.ruleName, [ this.appendText(pos, " ") ]);
this.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, fix);
}
}

Expand All @@ -176,18 +175,4 @@ class FunctionWalker extends Lint.RuleWalker {
const openParen = this.getChildOfType(node, ts.SyntaxKind.OpenParenToken);
this.evaluateRuleAt(openParen, option);
}

private createInvalidWhitespaceFailure(pos: number): Lint.RuleFailure {
const fix = new Lint.Fix(Rule.metadata.ruleName, [
this.deleteText(pos, 1),
]);
return this.createFailure(pos, 1, Rule.INVALID_WHITESPACE_ERROR, fix);
}

private createMissingWhitespaceFailure(pos: number): Lint.RuleFailure {
const fix = new Lint.Fix(Rule.metadata.ruleName, [
this.appendText(pos, " "),
]);
return this.createFailure(pos, 1, Rule.MISSING_WHITESPACE_ERROR, fix);
}
}