rule.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license
  3. * Copyright 2013 Palantir Technologies, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import * as ts from "typescript";
  18. import { IWalker } from "../walker";
  19. export interface RuleConstructor {
  20. metadata: IRuleMetadata;
  21. new (options: IOptions): IRule;
  22. }
  23. export interface IRuleMetadata {
  24. /**
  25. * The kebab-case name of the rule.
  26. */
  27. ruleName: string;
  28. /**
  29. * The type of the rule - its overall purpose
  30. */
  31. type: RuleType;
  32. /**
  33. * A rule deprecation message, if applicable.
  34. */
  35. deprecationMessage?: string;
  36. /**
  37. * A short, one line description of what the rule does.
  38. */
  39. description: string;
  40. /**
  41. * More elaborate details about the rule.
  42. */
  43. descriptionDetails?: string;
  44. /**
  45. * Whether or not the rule will provide fix suggestions.
  46. */
  47. hasFix?: boolean;
  48. /**
  49. * An explanation of the available options for the rule.
  50. */
  51. optionsDescription: string;
  52. /**
  53. * Schema of the options the rule accepts.
  54. * The first boolean for whether the rule is enabled or not is already implied.
  55. * This field describes the options after that boolean.
  56. * If null, this rule has no options and is not configurable.
  57. */
  58. options: any;
  59. /**
  60. * Examples of what a standard config for the rule might look like.
  61. * Using a string[] here is deprecated. Write the options as a JSON object instead.
  62. */
  63. optionExamples?: Array<true | any[]> | string[];
  64. /**
  65. * An explanation of why the rule is useful.
  66. */
  67. rationale?: string;
  68. /**
  69. * Whether or not the rule requires type info to run.
  70. */
  71. requiresTypeInfo?: boolean;
  72. /**
  73. * Whether or not the rule use for TypeScript only. If `false`, this rule may be used with .js files.
  74. */
  75. typescriptOnly: boolean;
  76. }
  77. export declare type RuleType = "functionality" | "maintainability" | "style" | "typescript";
  78. export declare type RuleSeverity = "warning" | "error" | "off";
  79. export interface IOptions {
  80. ruleArguments: any[];
  81. ruleSeverity: RuleSeverity;
  82. ruleName: string;
  83. /**
  84. * @deprecated
  85. * Tslint now handles disables itself.
  86. * This will be empty.
  87. */
  88. disabledIntervals: IDisabledInterval[];
  89. }
  90. /**
  91. * @deprecated
  92. * These are now handled internally.
  93. */
  94. export interface IDisabledInterval {
  95. startPosition: number;
  96. endPosition: number;
  97. }
  98. export interface IRule {
  99. getOptions(): IOptions;
  100. isEnabled(): boolean;
  101. apply(sourceFile: ts.SourceFile): RuleFailure[];
  102. applyWithWalker(walker: IWalker): RuleFailure[];
  103. }
  104. export interface ITypedRule extends IRule {
  105. applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[];
  106. }
  107. export interface IRuleFailureJson {
  108. endPosition: IRuleFailurePositionJson;
  109. failure: string;
  110. fix?: FixJson;
  111. name: string;
  112. ruleSeverity: string;
  113. ruleName: string;
  114. startPosition: IRuleFailurePositionJson;
  115. }
  116. export interface IRuleFailurePositionJson {
  117. character: number;
  118. line: number;
  119. position: number;
  120. }
  121. export declare function isTypedRule(rule: IRule): rule is ITypedRule;
  122. export interface ReplacementJson {
  123. innerStart: number;
  124. innerLength: number;
  125. innerText: string;
  126. }
  127. export declare class Replacement {
  128. readonly start: number;
  129. readonly length: number;
  130. readonly text: string;
  131. static applyFixes(content: string, fixes: Fix[]): string;
  132. static applyAll(content: string, replacements: Replacement[]): string;
  133. static replaceNode(node: ts.Node, text: string, sourceFile?: ts.SourceFile): Replacement;
  134. static replaceFromTo(start: number, end: number, text: string): Replacement;
  135. static deleteText(start: number, length: number): Replacement;
  136. static deleteFromTo(start: number, end: number): Replacement;
  137. static appendText(start: number, text: string): Replacement;
  138. constructor(start: number, length: number, text: string);
  139. readonly end: number;
  140. apply(content: string): string;
  141. toJson(): ReplacementJson;
  142. }
  143. export declare class RuleFailurePosition {
  144. private readonly position;
  145. private readonly lineAndCharacter;
  146. constructor(position: number, lineAndCharacter: ts.LineAndCharacter);
  147. getPosition(): number;
  148. getLineAndCharacter(): ts.LineAndCharacter;
  149. toJson(): IRuleFailurePositionJson;
  150. equals(ruleFailurePosition: RuleFailurePosition): boolean;
  151. }
  152. export declare type Fix = Replacement | Replacement[];
  153. export declare type FixJson = ReplacementJson | ReplacementJson[];
  154. export declare class RuleFailure {
  155. private readonly sourceFile;
  156. private readonly failure;
  157. private readonly ruleName;
  158. private readonly fix;
  159. private readonly fileName;
  160. private readonly startPosition;
  161. private readonly endPosition;
  162. private readonly rawLines;
  163. private ruleSeverity;
  164. static compare(a: RuleFailure, b: RuleFailure): number;
  165. constructor(sourceFile: ts.SourceFile, start: number, end: number, failure: string, ruleName: string, fix?: Replacement | Replacement[] | undefined);
  166. getFileName(): string;
  167. getRuleName(): string;
  168. getStartPosition(): RuleFailurePosition;
  169. getEndPosition(): RuleFailurePosition;
  170. getFailure(): string;
  171. hasFix(): boolean;
  172. getFix(): Replacement | Replacement[] | undefined;
  173. getRawLines(): string;
  174. getRuleSeverity(): RuleSeverity;
  175. setRuleSeverity(value: RuleSeverity): void;
  176. toJson(): IRuleFailureJson;
  177. equals(ruleFailure: RuleFailure): boolean;
  178. private createFailurePosition(position);
  179. }