diff options
Diffstat (limited to 'node_modules/tslint/lib/language/rule')
8 files changed, 540 insertions, 0 deletions
diff --git a/node_modules/tslint/lib/language/rule/abstractRule.d.ts b/node_modules/tslint/lib/language/rule/abstractRule.d.ts new file mode 100644 index 000000000..8a2362b5c --- /dev/null +++ b/node_modules/tslint/lib/language/rule/abstractRule.d.ts @@ -0,0 +1,39 @@ +/** + * @license + * Copyright 2013 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 { IWalker, WalkContext } from "../walker"; +import { IOptions, IRule, IRuleMetadata, RuleFailure, RuleSeverity } from "./rule"; +export declare abstract class AbstractRule implements IRule { + private options; + static metadata: IRuleMetadata; + protected readonly ruleArguments: any[]; + protected readonly ruleSeverity: RuleSeverity; + ruleName: string; + constructor(options: IOptions); + getOptions(): IOptions; + abstract apply(sourceFile: ts.SourceFile): RuleFailure[]; + applyWithWalker(walker: IWalker): RuleFailure[]; + isEnabled(): boolean; + protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext<void>) => void): RuleFailure[]; + protected applyWithFunction<T, U extends T>(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext<T>) => void, options: U): RuleFailure[]; + /** + * @deprecated + * Failures will be filtered based on `tslint:disable` comments by tslint. + * This method now does nothing. + */ + protected filterFailures(failures: RuleFailure[]): RuleFailure[]; +} diff --git a/node_modules/tslint/lib/language/rule/abstractRule.js b/node_modules/tslint/lib/language/rule/abstractRule.js new file mode 100644 index 000000000..52f4c8f55 --- /dev/null +++ b/node_modules/tslint/lib/language/rule/abstractRule.js @@ -0,0 +1,50 @@ +"use strict"; +/** + * @license + * Copyright 2013 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. + */ +Object.defineProperty(exports, "__esModule", { value: true }); +var walker_1 = require("../walker"); +var AbstractRule = (function () { + function AbstractRule(options) { + this.options = options; + this.ruleName = options.ruleName; + this.ruleArguments = options.ruleArguments; + this.ruleSeverity = options.ruleSeverity; + } + AbstractRule.prototype.getOptions = function () { + return this.options; + }; + AbstractRule.prototype.applyWithWalker = function (walker) { + walker.walk(walker.getSourceFile()); + return walker.getFailures(); + }; + AbstractRule.prototype.isEnabled = function () { + return this.ruleSeverity !== "off"; + }; + AbstractRule.prototype.applyWithFunction = function (sourceFile, walkFn, options) { + var ctx = new walker_1.WalkContext(sourceFile, this.ruleName, options); + walkFn(ctx); + return ctx.failures; + }; + /** + * @deprecated + * Failures will be filtered based on `tslint:disable` comments by tslint. + * This method now does nothing. + */ + AbstractRule.prototype.filterFailures = function (failures) { return failures; }; + return AbstractRule; +}()); +exports.AbstractRule = AbstractRule; diff --git a/node_modules/tslint/lib/language/rule/optionallyTypedRule.d.ts b/node_modules/tslint/lib/language/rule/optionallyTypedRule.d.ts new file mode 100644 index 000000000..f12d74f44 --- /dev/null +++ b/node_modules/tslint/lib/language/rule/optionallyTypedRule.d.ts @@ -0,0 +1,22 @@ +/** + * @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 declare abstract class OptionallyTypedRule extends AbstractRule implements ITypedRule { + abstract applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[]; +} diff --git a/node_modules/tslint/lib/language/rule/optionallyTypedRule.js b/node_modules/tslint/lib/language/rule/optionallyTypedRule.js new file mode 100644 index 000000000..1914eda40 --- /dev/null +++ b/node_modules/tslint/lib/language/rule/optionallyTypedRule.js @@ -0,0 +1,28 @@ +"use strict"; +/** + * @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. + */ +Object.defineProperty(exports, "__esModule", { value: true }); +var tslib_1 = require("tslib"); +var abstractRule_1 = require("./abstractRule"); +var OptionallyTypedRule = (function (_super) { + tslib_1.__extends(OptionallyTypedRule, _super); + function OptionallyTypedRule() { + return _super !== null && _super.apply(this, arguments) || this; + } + return OptionallyTypedRule; +}(abstractRule_1.AbstractRule)); +exports.OptionallyTypedRule = OptionallyTypedRule; diff --git a/node_modules/tslint/lib/language/rule/rule.d.ts b/node_modules/tslint/lib/language/rule/rule.d.ts new file mode 100644 index 000000000..e432ce375 --- /dev/null +++ b/node_modules/tslint/lib/language/rule/rule.d.ts @@ -0,0 +1,178 @@ +/** + * @license + * Copyright 2013 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 { IWalker } from "../walker"; +export interface RuleConstructor { + metadata: IRuleMetadata; + new (options: IOptions): IRule; +} +export interface IRuleMetadata { + /** + * The kebab-case name of the rule. + */ + ruleName: string; + /** + * The type of the rule - its overall purpose + */ + type: RuleType; + /** + * A rule deprecation message, if applicable. + */ + deprecationMessage?: string; + /** + * A short, one line description of what the rule does. + */ + description: string; + /** + * More elaborate details about the rule. + */ + descriptionDetails?: string; + /** + * Whether or not the rule will provide fix suggestions. + */ + hasFix?: boolean; + /** + * An explanation of the available options for the rule. + */ + optionsDescription: string; + /** + * Schema of the options the rule accepts. + * The first boolean for whether the rule is enabled or not is already implied. + * This field describes the options after that boolean. + * If null, this rule has no options and is not configurable. + */ + options: any; + /** + * Examples of what a standard config for the rule might look like. + * Using a string[] here is deprecated. Write the options as a JSON object instead. + */ + optionExamples?: Array<true | any[]> | string[]; + /** + * An explanation of why the rule is useful. + */ + rationale?: string; + /** + * Whether or not the rule requires type info to run. + */ + requiresTypeInfo?: boolean; + /** + * Whether or not the rule use for TypeScript only. If `false`, this rule may be used with .js files. + */ + typescriptOnly: boolean; +} +export declare type RuleType = "functionality" | "maintainability" | "style" | "typescript"; +export declare type RuleSeverity = "warning" | "error" | "off"; +export interface IOptions { + ruleArguments: any[]; + ruleSeverity: RuleSeverity; + ruleName: string; + /** + * @deprecated + * Tslint now handles disables itself. + * This will be empty. + */ + disabledIntervals: IDisabledInterval[]; +} +/** + * @deprecated + * These are now handled internally. + */ +export interface IDisabledInterval { + startPosition: number; + endPosition: number; +} +export interface IRule { + getOptions(): IOptions; + isEnabled(): boolean; + apply(sourceFile: ts.SourceFile): RuleFailure[]; + applyWithWalker(walker: IWalker): RuleFailure[]; +} +export interface ITypedRule extends IRule { + applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[]; +} +export interface IRuleFailureJson { + endPosition: IRuleFailurePositionJson; + failure: string; + fix?: FixJson; + name: string; + ruleSeverity: string; + ruleName: string; + startPosition: IRuleFailurePositionJson; +} +export interface IRuleFailurePositionJson { + character: number; + line: number; + position: number; +} +export declare function isTypedRule(rule: IRule): rule is ITypedRule; +export interface ReplacementJson { + innerStart: number; + innerLength: number; + innerText: string; +} +export declare class Replacement { + readonly start: number; + readonly length: number; + readonly text: string; + static applyFixes(content: string, fixes: Fix[]): string; + static applyAll(content: string, replacements: Replacement[]): string; + static replaceNode(node: ts.Node, text: string, sourceFile?: ts.SourceFile): Replacement; + static replaceFromTo(start: number, end: number, text: string): Replacement; + static deleteText(start: number, length: number): Replacement; + static deleteFromTo(start: number, end: number): Replacement; + static appendText(start: number, text: string): Replacement; + constructor(start: number, length: number, text: string); + readonly end: number; + apply(content: string): string; + toJson(): ReplacementJson; +} +export declare class RuleFailurePosition { + private position; + private lineAndCharacter; + constructor(position: number, lineAndCharacter: ts.LineAndCharacter); + getPosition(): number; + getLineAndCharacter(): ts.LineAndCharacter; + toJson(): IRuleFailurePositionJson; + equals(ruleFailurePosition: RuleFailurePosition): boolean; +} +export declare type Fix = Replacement | Replacement[]; +export declare type FixJson = ReplacementJson | ReplacementJson[]; +export declare class RuleFailure { + private sourceFile; + private failure; + private ruleName; + private fix; + private fileName; + private startPosition; + private endPosition; + private rawLines; + private ruleSeverity; + constructor(sourceFile: ts.SourceFile, start: number, end: number, failure: string, ruleName: string, fix?: Fix); + getFileName(): string; + getRuleName(): string; + getStartPosition(): RuleFailurePosition; + getEndPosition(): RuleFailurePosition; + getFailure(): string; + hasFix(): boolean; + getFix(): Replacement | Replacement[] | undefined; + getRawLines(): string; + getRuleSeverity(): RuleSeverity; + setRuleSeverity(value: RuleSeverity): void; + toJson(): IRuleFailureJson; + equals(ruleFailure: RuleFailure): boolean; + private createFailurePosition(position); +} diff --git a/node_modules/tslint/lib/language/rule/rule.js b/node_modules/tslint/lib/language/rule/rule.js new file mode 100644 index 000000000..98291f25a --- /dev/null +++ b/node_modules/tslint/lib/language/rule/rule.js @@ -0,0 +1,168 @@ +"use strict"; +/** + * @license + * Copyright 2013 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. + */ +Object.defineProperty(exports, "__esModule", { value: true }); +var utils_1 = require("../../utils"); +function isTypedRule(rule) { + return "applyWithProgram" in rule; +} +exports.isTypedRule = isTypedRule; +var Replacement = (function () { + function Replacement(start, length, text) { + this.start = start; + this.length = length; + this.text = text; + } + Replacement.applyFixes = function (content, fixes) { + return this.applyAll(content, utils_1.flatMap(fixes, utils_1.arrayify)); + }; + Replacement.applyAll = function (content, replacements) { + // sort in reverse so that diffs are properly applied + replacements.sort(function (a, b) { return b.end !== a.end ? b.end - a.end : b.start - a.start; }); + return replacements.reduce(function (text, r) { return r.apply(text); }, content); + }; + Replacement.replaceNode = function (node, text, sourceFile) { + return this.replaceFromTo(node.getStart(sourceFile), node.getEnd(), text); + }; + Replacement.replaceFromTo = function (start, end, text) { + return new Replacement(start, end - start, text); + }; + Replacement.deleteText = function (start, length) { + return new Replacement(start, length, ""); + }; + Replacement.deleteFromTo = function (start, end) { + return new Replacement(start, end - start, ""); + }; + Replacement.appendText = function (start, text) { + return new Replacement(start, 0, text); + }; + Object.defineProperty(Replacement.prototype, "end", { + get: function () { + return this.start + this.length; + }, + enumerable: true, + configurable: true + }); + Replacement.prototype.apply = function (content) { + return content.substring(0, this.start) + this.text + content.substring(this.start + this.length); + }; + Replacement.prototype.toJson = function () { + // tslint:disable object-literal-sort-keys + return { + innerStart: this.start, + innerLength: this.length, + innerText: this.text, + }; + // tslint:enable object-literal-sort-keys + }; + return Replacement; +}()); +exports.Replacement = Replacement; +var RuleFailurePosition = (function () { + function RuleFailurePosition(position, lineAndCharacter) { + this.position = position; + this.lineAndCharacter = lineAndCharacter; + } + RuleFailurePosition.prototype.getPosition = function () { + return this.position; + }; + RuleFailurePosition.prototype.getLineAndCharacter = function () { + return this.lineAndCharacter; + }; + RuleFailurePosition.prototype.toJson = function () { + return { + character: this.lineAndCharacter.character, + line: this.lineAndCharacter.line, + position: this.position, + }; + }; + RuleFailurePosition.prototype.equals = function (ruleFailurePosition) { + var ll = this.lineAndCharacter; + var rr = ruleFailurePosition.lineAndCharacter; + return this.position === ruleFailurePosition.position + && ll.line === rr.line + && ll.character === rr.character; + }; + return RuleFailurePosition; +}()); +exports.RuleFailurePosition = RuleFailurePosition; +var RuleFailure = (function () { + function RuleFailure(sourceFile, start, end, failure, ruleName, fix) { + this.sourceFile = sourceFile; + this.failure = failure; + this.ruleName = ruleName; + this.fix = fix; + this.fileName = sourceFile.fileName; + this.startPosition = this.createFailurePosition(start); + this.endPosition = this.createFailurePosition(end); + this.rawLines = sourceFile.text; + this.ruleSeverity = "error"; + } + RuleFailure.prototype.getFileName = function () { + return this.fileName; + }; + RuleFailure.prototype.getRuleName = function () { + return this.ruleName; + }; + RuleFailure.prototype.getStartPosition = function () { + return this.startPosition; + }; + RuleFailure.prototype.getEndPosition = function () { + return this.endPosition; + }; + RuleFailure.prototype.getFailure = function () { + return this.failure; + }; + RuleFailure.prototype.hasFix = function () { + return this.fix !== undefined; + }; + RuleFailure.prototype.getFix = function () { + return this.fix; + }; + RuleFailure.prototype.getRawLines = function () { + return this.rawLines; + }; + RuleFailure.prototype.getRuleSeverity = function () { + return this.ruleSeverity; + }; + RuleFailure.prototype.setRuleSeverity = function (value) { + this.ruleSeverity = value; + }; + RuleFailure.prototype.toJson = function () { + return { + endPosition: this.endPosition.toJson(), + failure: this.failure, + fix: this.fix === undefined ? undefined : Array.isArray(this.fix) ? this.fix.map(function (r) { return r.toJson(); }) : this.fix.toJson(), + name: this.fileName, + ruleName: this.ruleName, + ruleSeverity: this.ruleSeverity.toUpperCase(), + startPosition: this.startPosition.toJson(), + }; + }; + RuleFailure.prototype.equals = function (ruleFailure) { + return this.failure === ruleFailure.getFailure() + && this.fileName === ruleFailure.getFileName() + && this.startPosition.equals(ruleFailure.getStartPosition()) + && this.endPosition.equals(ruleFailure.getEndPosition()); + }; + RuleFailure.prototype.createFailurePosition = function (position) { + var lineAndCharacter = this.sourceFile.getLineAndCharacterOfPosition(position); + return new RuleFailurePosition(position, lineAndCharacter); + }; + return RuleFailure; +}()); +exports.RuleFailure = RuleFailure; diff --git a/node_modules/tslint/lib/language/rule/typedRule.d.ts b/node_modules/tslint/lib/language/rule/typedRule.d.ts new file mode 100644 index 000000000..a4d1fcca0 --- /dev/null +++ b/node_modules/tslint/lib/language/rule/typedRule.d.ts @@ -0,0 +1,23 @@ +/** + * @license + * Copyright 2016 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 declare abstract class TypedRule extends AbstractRule implements ITypedRule { + apply(): RuleFailure[]; + abstract applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[]; +} diff --git a/node_modules/tslint/lib/language/rule/typedRule.js b/node_modules/tslint/lib/language/rule/typedRule.js new file mode 100644 index 000000000..45a2fb30d --- /dev/null +++ b/node_modules/tslint/lib/language/rule/typedRule.js @@ -0,0 +1,32 @@ +"use strict"; +/** + * @license + * Copyright 2016 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. + */ +Object.defineProperty(exports, "__esModule", { value: true }); +var tslib_1 = require("tslib"); +var abstractRule_1 = require("./abstractRule"); +var TypedRule = (function (_super) { + tslib_1.__extends(TypedRule, _super); + function TypedRule() { + return _super !== null && _super.apply(this, arguments) || this; + } + TypedRule.prototype.apply = function () { + // if no program is given to the linter, throw an error + throw new Error("The '" + this.ruleName + "' rule requires type checking"); + }; + return TypedRule; +}(abstractRule_1.AbstractRule)); +exports.TypedRule = TypedRule; |