UNPKG

5.23 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14import { Analyzer } from '../core/analyzer';
15import { UrlResolver } from '../index';
16import { ParsedDocument } from '../parser/document';
17import { Analysis } from './analysis';
18import { SourceRange } from './source-range';
19import { ResolvedUrl } from './url';
20export interface WarningInit {
21 readonly message: string;
22 readonly sourceRange: SourceRange;
23 readonly severity: Severity;
24 readonly code: string;
25 readonly parsedDocument: ParsedDocument;
26 readonly fix?: Edit;
27 readonly actions?: ReadonlyArray<Action>;
28}
29export declare class Warning {
30 readonly code: string;
31 readonly message: string;
32 readonly sourceRange: SourceRange;
33 readonly severity: Severity;
34 /**
35 * If the problem has a single automatic fix, this is it.
36 *
37 * Whether and how much something is 'automatic' can be a bit tricky to
38 * delineate. Roughly speaking, if 99% of the time the change solves the
39 * issue completely then it should go in `fix`.
40 */
41 readonly fix: Edit | undefined;
42 /**
43 * Other actions that could be taken in response to this warning.
44 *
45 * Each action is separate and they may be mutually exclusive. In the case
46 * of edit actions they often are.
47 */
48 readonly actions: ReadonlyArray<Action> | undefined;
49 private readonly _parsedDocument;
50 constructor(init: WarningInit);
51 toString(options?: Partial<WarningStringifyOptions>): string;
52 private _severityToColorFunction;
53 private _severityToString;
54 toJSON(): {
55 code: string;
56 message: string;
57 severity: Severity;
58 sourceRange: SourceRange;
59 };
60}
61export declare enum Severity {
62 ERROR = 0,
63 WARNING = 1,
64 INFO = 2
65}
66export declare class WarningCarryingException extends Error {
67 readonly warning: Warning;
68 constructor(warning: Warning);
69}
70export declare type Verbosity = 'one-line' | 'full' | 'code-only';
71export interface WarningStringifyOptions {
72 readonly verbosity: Verbosity;
73 readonly color: boolean;
74 /**
75 * If given, we will use resolver.relative to get a relative path
76 * to the reported file.
77 */
78 readonly resolver?: UrlResolver;
79 /**
80 * If given, we will only print this many lines of code. Otherwise print all
81 * lines in the source range.
82 */
83 readonly maxCodeLines?: number;
84}
85export declare type Action = EditAction | {
86 /** To ensure that type safe code actually checks for the action kind. */
87 kind: 'never';
88};
89/**
90 * An EditAction is like a fix, only it's not applied automatically when the
91 * user runs `polymer lint --fix`. Often this is because it's less safe to
92 * apply automatically, and there may be caveats, or multiple ways to resolve
93 * the warning.
94 *
95 * For example, a change to an element that updates it to no longer use a
96 * deprecated feature, but that involves a change in the element's API should
97 * not be a fix, but should instead be an EditAction.
98 */
99export interface EditAction {
100 kind: 'edit';
101 /**
102 * A unique string code for the edit action. Useful so that the user can
103 * request that all actions with a given code should be applied.
104 */
105 code: string;
106 /**
107 * A short description of the change, noting caveats and important information
108 * for the user.
109 */
110 description: string;
111 edit: Edit;
112}
113/**
114 * Represents an action for replacing a range in a document with some text.
115 *
116 * This is sufficient to represent all operations on text files, including
117 * inserting and deleting text (using empty ranges or empty replacement
118 * text, respectively).
119 */
120export interface Replacement {
121 readonly range: SourceRange;
122 readonly replacementText: string;
123}
124/**
125 * A set of replacements that must all be applied as a single atomic unit.
126 */
127export declare type Edit = ReadonlyArray<Replacement>;
128export interface EditResult {
129 /** The edits that had no conflicts, and are reflected in editedFiles. */
130 appliedEdits: Edit[];
131 /** Edits that could not be applied due to overlapping ranges. */
132 incompatibleEdits: Edit[];
133 /** A map from urls to their new contents. */
134 editedFiles: Map<ResolvedUrl, string>;
135}
136/**
137 * Takes the given edits and, provided there are no overlaps, applies them to
138 * the contents loadable from the given loader.
139 *
140 * If there are overlapping edits, then edits earlier in the array get priority
141 * over later ones.
142 */
143export declare function applyEdits(edits: Edit[], loader: (url: ResolvedUrl) => Promise<ParsedDocument>): Promise<EditResult>;
144export declare function makeParseLoader(analyzer: Analyzer, analysis?: Analysis): (url: ResolvedUrl) => Promise<ParsedDocument<{} | null | undefined, {}>>;