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

Added an abstract OptionallyTypedRule #2300

Merged
merged 2 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions src/language/rule/optionallyTypedRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as ts from "typescript";

import {AbstractRule} from "./abstractRule";
import {ITypedRule, RuleFailure} from "./rule";

export abstract class OptionallyTypedRule extends AbstractRule implements ITypedRule {

public abstract applyWithProgram(sourceFile: ts.SourceFile, languageService: ts.LanguageService): RuleFailure[];
}
8 changes: 8 additions & 0 deletions src/language/rule/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export interface IRule {
applyWithWalker(walker: IWalker): RuleFailure[];
}

export interface ITypedRule extends IRule {
applyWithProgram(sourceFile: ts.SourceFile, languageService: ts.LanguageService): RuleFailure[];
}

export interface IRuleFailureJson {
endPosition: IRuleFailurePositionJson;
failure: string;
Expand All @@ -123,6 +127,10 @@ export interface IRuleFailurePositionJson {
position: number;
}

export function isTypedRule(rule: IRule): rule is ITypedRule {
return "applyWithProgram" in rule;
}

export class Replacement {
public static applyAll(content: string, replacements: Replacement[]) {
// sort in reverse so that diffs are properly applied
Expand Down
8 changes: 2 additions & 6 deletions src/language/rule/typedRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
import * as ts from "typescript";

import {AbstractRule} from "./abstractRule";
import {IRule, RuleFailure} from "./rule";
import {ITypedRule, RuleFailure} from "./rule";

export abstract class TypedRule extends AbstractRule {

public static isTypedRule(rule: IRule): rule is TypedRule {
return "applyWithProgram" in rule;
}
export abstract class TypedRule extends AbstractRule implements ITypedRule {

public apply(): RuleFailure[] {
// if no program is given to the linter, throw an error
Expand Down
5 changes: 2 additions & 3 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import { findFormatter } from "./formatterLoader";
import { ILinterOptions, LintResult } from "./index";
import { IFormatter } from "./language/formatter/formatter";
import { createLanguageService, wrapProgram } from "./language/languageServiceHost";
import { Fix, IRule, RuleFailure, RuleSeverity } from "./language/rule/rule";
import { TypedRule } from "./language/rule/typedRule";
import { Fix, IRule, isTypedRule, RuleFailure, RuleSeverity } from "./language/rule/rule";
import * as utils from "./language/utils";
import { loadRules } from "./ruleLoader";
import { arrayify, dedent } from "./utils";
Expand Down Expand Up @@ -179,7 +178,7 @@ class Linter {
private applyRule(rule: IRule, sourceFile: ts.SourceFile) {
let ruleFailures: RuleFailure[] = [];
try {
if (TypedRule.isTypedRule(rule) && this.program) {
if (this.program && isTypedRule(rule)) {
ruleFailures = rule.applyWithProgram(sourceFile, this.languageService);
} else {
ruleFailures = rule.apply(sourceFile, this.languageService);
Expand Down