rule.d.ts 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * Examples demonstrating what the lint rule will pass and fail
  78. */
  79. codeExamples?: ICodeExample[];
  80. }
  81. export declare type RuleType = "functionality" | "maintainability" | "style" | "typescript";
  82. export declare type RuleSeverity = "warning" | "error" | "off";
  83. export interface ICodeExample {
  84. config: string;
  85. description: string;
  86. pass: string;
  87. fail?: string;
  88. }
  89. export interface IOptions {
  90. ruleArguments: any[];
  91. ruleSeverity: RuleSeverity;
  92. ruleName: string;
  93. /**
  94. * @deprecated
  95. * Tslint now handles disables itself.
  96. * This will be empty.
  97. */
  98. disabledIntervals: IDisabledInterval[];
  99. }
  100. /**
  101. * @deprecated
  102. * These are now handled internally.
  103. */
  104. export interface IDisabledInterval {
  105. startPosition: number;
  106. endPosition: number;
  107. }
  108. export interface IRule {
  109. getOptions(): IOptions;
  110. isEnabled(): boolean;
  111. apply(sourceFile: ts.SourceFile): RuleFailure[];
  112. applyWithWalker(walker: IWalker): RuleFailure[];
  113. }
  114. export interface ITypedRule extends IRule {
  115. applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[];
  116. }
  117. export interface IRuleFailureJson {
  118. endPosition: IRuleFailurePositionJson;
  119. failure: string;
  120. fix?: FixJson;
  121. name: string;
  122. ruleSeverity: string;
  123. ruleName: string;
  124. startPosition: IRuleFailurePositionJson;
  125. }
  126. export interface IRuleFailurePositionJson {
  127. character: number;
  128. line: number;
  129. position: number;
  130. }
  131. export declare function isTypedRule(rule: IRule): rule is ITypedRule;
  132. export interface ReplacementJson {
  133. innerStart: number;
  134. innerLength: number;
  135. innerText: string;
  136. }
  137. export declare class Replacement {
  138. readonly start: number;
  139. readonly length: number;
  140. readonly text: string;
  141. static applyFixes(content: string, fixes: Fix[]): string;
  142. static applyAll(content: string, replacements: Replacement[]): string;
  143. static replaceNode(node: ts.Node, text: string, sourceFile?: ts.SourceFile): Replacement;
  144. static replaceFromTo(start: number, end: number, text: string): Replacement;
  145. static deleteText(start: number, length: number): Replacement;
  146. static deleteFromTo(start: number, end: number): Replacement;
  147. static appendText(start: number, text: string): Replacement;
  148. constructor(start: number, length: number, text: string);
  149. readonly end: number;
  150. apply(content: string): string;
  151. toJson(): ReplacementJson;
  152. }
  153. export declare class RuleFailurePosition {
  154. private readonly position;
  155. private readonly lineAndCharacter;
  156. constructor(position: number, lineAndCharacter: ts.LineAndCharacter);
  157. getPosition(): number;
  158. getLineAndCharacter(): ts.LineAndCharacter;
  159. toJson(): IRuleFailurePositionJson;
  160. equals(ruleFailurePosition: RuleFailurePosition): boolean;
  161. }
  162. export declare type Fix = Replacement | Replacement[];
  163. export declare type FixJson = ReplacementJson | ReplacementJson[];
  164. export declare class RuleFailure {
  165. private readonly sourceFile;
  166. private readonly failure;
  167. private readonly ruleName;
  168. private readonly fix;
  169. private readonly fileName;
  170. private readonly startPosition;
  171. private readonly endPosition;
  172. private readonly rawLines;
  173. private ruleSeverity;
  174. static compare(a: RuleFailure, b: RuleFailure): number;
  175. constructor(sourceFile: ts.SourceFile, start: number, end: number, failure: string, ruleName: string, fix?: Replacement | Replacement[] | undefined);
  176. getFileName(): string;
  177. getRuleName(): string;
  178. getStartPosition(): RuleFailurePosition;
  179. getEndPosition(): RuleFailurePosition;
  180. getFailure(): string;
  181. hasFix(): boolean;
  182. getFix(): Replacement | Replacement[] | undefined;
  183. getRawLines(): string;
  184. getRuleSeverity(): RuleSeverity;
  185. setRuleSeverity(value: RuleSeverity): void;
  186. toJson(): IRuleFailureJson;
  187. equals(ruleFailure: RuleFailure): boolean;
  188. private createFailurePosition(position);
  189. }