UNPKG

7.43 kBTypeScriptView Raw
1import * as ts from 'typescript';
2import { IndentedWriter } from '../generators/IndentedWriter';
3/**
4 * Choices for SpanModification.indentDocComment.
5 */
6export declare enum IndentDocCommentScope {
7 /**
8 * Do not detect and indent comments.
9 */
10 None = 0,
11 /**
12 * Look for one doc comment in the {@link Span.prefix} text only.
13 */
14 PrefixOnly = 1,
15 /**
16 * Look for one doc comment potentially distributed across the Span and its children.
17 */
18 SpanAndChildren = 2
19}
20/**
21 * Specifies various transformations that will be performed by Span.getModifiedText().
22 */
23export declare class SpanModification {
24 /**
25 * If true, all of the child spans will be omitted from the Span.getModifiedText() output.
26 * @remarks
27 * Also, the modify() operation will not recurse into these spans.
28 */
29 omitChildren: boolean;
30 /**
31 * If true, then the Span.separator will be removed from the Span.getModifiedText() output.
32 */
33 omitSeparatorAfter: boolean;
34 /**
35 * If true, then Span.getModifiedText() will sort the immediate children according to their Span.sortKey
36 * property. The separators will also be fixed up to ensure correct indentation. If the Span.sortKey is undefined
37 * for some items, those items will not be moved, i.e. their array indexes will be unchanged.
38 */
39 sortChildren: boolean;
40 /**
41 * Used if the parent span has Span.sortChildren=true.
42 */
43 sortKey: string | undefined;
44 /**
45 * Optionally configures getModifiedText() to search for a "/*" doc comment and indent it.
46 * At most one comment is detected.
47 *
48 * @remarks
49 * The indentation can be applied to the `Span.modifier.prefix` only, or it can be applied to the
50 * full subtree of nodes (as needed for `ts.SyntaxKind.JSDocComment` trees). However the enabled
51 * scopes must not overlap.
52 *
53 * This feature is enabled selectively because (1) we do not want to accidentally match `/*` appearing
54 * in a string literal or other expression that is not a comment, and (2) parsing comments is relatively
55 * expensive.
56 */
57 indentDocComment: IndentDocCommentScope;
58 private readonly _span;
59 private _prefix;
60 private _suffix;
61 constructor(span: Span);
62 /**
63 * Allows the Span.prefix text to be changed.
64 */
65 get prefix(): string;
66 set prefix(value: string);
67 /**
68 * Allows the Span.suffix text to be changed.
69 */
70 get suffix(): string;
71 set suffix(value: string);
72 /**
73 * Reverts any modifications made to this object.
74 */
75 reset(): void;
76 /**
77 * Effectively deletes the Span from the tree, by skipping its children, skipping its separator,
78 * and setting its prefix/suffix to the empty string.
79 */
80 skipAll(): void;
81}
82/**
83 * The Span class provides a simple way to rewrite TypeScript source files
84 * based on simple syntax transformations, i.e. without having to process deeper aspects
85 * of the underlying grammar. An example transformation might be deleting JSDoc comments
86 * from a source file.
87 *
88 * @remarks
89 * TypeScript's abstract syntax tree (AST) is represented using Node objects.
90 * The Node text ignores its surrounding whitespace, and does not have an ordering guarantee.
91 * For example, a JSDocComment node can be a child of a FunctionDeclaration node, even though
92 * the actual comment precedes the function in the input stream.
93 *
94 * The Span class is a wrapper for a single Node, that provides access to every character
95 * in the input stream, such that Span.getText() will exactly reproduce the corresponding
96 * full Node.getText() output.
97 *
98 * A Span is comprised of these parts, which appear in sequential order:
99 * - A prefix
100 * - A collection of child spans
101 * - A suffix
102 * - A separator (e.g. whitespace between this span and the next item in the tree)
103 *
104 * These parts can be modified via Span.modification. The modification is applied by
105 * calling Span.getModifiedText().
106 */
107export declare class Span {
108 readonly node: ts.Node;
109 readonly startIndex: number;
110 readonly endIndex: number;
111 readonly children: Span[];
112 readonly modification: SpanModification;
113 private _parent;
114 private _previousSibling;
115 private _nextSibling;
116 private _separatorStartIndex;
117 private _separatorEndIndex;
118 constructor(node: ts.Node);
119 get kind(): ts.SyntaxKind;
120 /**
121 * The parent Span, if any.
122 * NOTE: This will be undefined for a root Span, even though the corresponding Node
123 * may have a parent in the AST.
124 */
125 get parent(): Span | undefined;
126 /**
127 * If the current object is this.parent.children[i], then previousSibling corresponds
128 * to this.parent.children[i-1] if it exists.
129 * NOTE: This will be undefined for a root Span, even though the corresponding Node
130 * may have a previous sibling in the AST.
131 */
132 get previousSibling(): Span | undefined;
133 /**
134 * If the current object is this.parent.children[i], then previousSibling corresponds
135 * to this.parent.children[i+1] if it exists.
136 * NOTE: This will be undefined for a root Span, even though the corresponding Node
137 * may have a previous sibling in the AST.
138 */
139 get nextSibling(): Span | undefined;
140 /**
141 * The text associated with the underlying Node, up to its first child.
142 */
143 get prefix(): string;
144 /**
145 * The text associated with the underlying Node, after its last child.
146 * If there are no children, this is always an empty string.
147 */
148 get suffix(): string;
149 /**
150 * Whitespace that appeared after this node, and before the "next" node in the tree.
151 * Here we mean "next" according to an inorder traversal, not necessarily a sibling.
152 */
153 get separator(): string;
154 /**
155 * Returns the separator of this Span, or else recursively calls getLastInnerSeparator()
156 * on the last child.
157 */
158 getLastInnerSeparator(): string;
159 /**
160 * Returns the first parent node with the specified SyntaxKind, or undefined if there is no match.
161 */
162 findFirstParent(kindToMatch: ts.SyntaxKind): Span | undefined;
163 /**
164 * Recursively invokes the callback on this Span and all its children. The callback
165 * can make changes to Span.modification for each node.
166 */
167 forEach(callback: (span: Span) => void): void;
168 /**
169 * Returns the original unmodified text represented by this Span.
170 */
171 getText(): string;
172 /**
173 * Returns the text represented by this Span, after applying all requested modifications.
174 */
175 getModifiedText(): string;
176 writeModifiedText(output: IndentedWriter): void;
177 /**
178 * Returns a diagnostic dump of the tree, showing the prefix/suffix/separator for
179 * each node.
180 */
181 getDump(indent?: string): string;
182 /**
183 * Returns a diagnostic dump of the tree, showing the SpanModification settings for each nodde.
184 */
185 getModifiedDump(indent?: string): string;
186 /**
187 * Recursive implementation of `getModifiedText()` and `writeModifiedText()`.
188 */
189 private _writeModifiedText;
190 private _beginIndentDocComment;
191 private _endIndentDocComment;
192 /**
193 * Writes one chunk of `text` to the `options.writer`, applying the `indentDocComment` rewriting.
194 */
195 private _write;
196 private _getTrimmed;
197 private _getSubstring;
198}
199//# sourceMappingURL=Span.d.ts.map
\No newline at end of file