UNPKG

3.97 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 { AstNodeWithLanguage } from '../model/inline-document';
15import { LocationOffset, SourcePosition, SourceRange } from '../model/source-range';
16import { ResolvedUrl } from '../model/url';
17/**
18 * A parsed Document.
19 *
20 * @template AstNode The AST type of the document.
21 * @template Visitor The type of the visitors that can walk the document.
22 */
23export declare abstract class ParsedDocument<AstNode = {} | null | undefined, Visitor = {}> {
24 abstract type: string;
25 url: ResolvedUrl;
26 baseUrl: ResolvedUrl;
27 contents: string;
28 ast: AstNode;
29 isInline: boolean;
30 /**
31 * If not null, this is an inline document, and astNode is the AST Node of
32 * this document inside of the parent. (e.g. the <style> or <script> tag)
33 */
34 readonly astNode: AstNodeWithLanguage | undefined;
35 sourceRange: SourceRange;
36 private readonly _locationOffset;
37 /**
38 * The 0-based offsets into `contents` of all newline characters.
39 *
40 * Useful for converting between string offsets and SourcePositions.
41 */
42 readonly newlineIndexes: number[];
43 constructor(from: Options<AstNode>);
44 /**
45 * Runs a set of document-type specific visitors against the document.
46 */
47 abstract visit(visitors: Visitor[]): void;
48 sourceRangeForNode(node: AstNode): SourceRange | undefined;
49 protected abstract _sourceRangeForNode(node: AstNode): SourceRange | undefined;
50 /**
51 * Convert `this.ast` back into a string document.
52 */
53 abstract stringify(options: StringifyOptions): string;
54 offsetToSourcePosition(offset: number): SourcePosition;
55 offsetsToSourceRange(start: number, end: number): SourceRange;
56 sourcePositionToOffset(position: SourcePosition): number;
57 relativeToAbsoluteSourceRange(sourceRange: SourceRange): SourceRange;
58 relativeToAbsoluteSourceRange(sourceRange: undefined): undefined;
59 relativeToAbsoluteSourceRange(sourceRange: SourceRange | undefined): SourceRange | undefined;
60 absoluteToRelativeSourceRange(sourceRange: SourceRange): SourceRange;
61 absoluteToRelativeSourceRange(sourceRange: undefined): undefined;
62 absoluteToRelativeSourceRange(sourceRange: SourceRange | undefined): SourceRange | undefined;
63 sourceRangeToOffsets(range: SourceRange): [number, number];
64 toString(): string;
65}
66export interface Options<A> {
67 url: ResolvedUrl;
68 baseUrl: ResolvedUrl | undefined;
69 contents: string;
70 ast: A;
71 locationOffset: LocationOffset | undefined;
72 astNode: AstNodeWithLanguage | undefined;
73 isInline: boolean;
74}
75export interface StringifyOptions {
76 /** The desired level of indentation of to stringify at. */
77 indent?: number;
78 /**
79 * Parsed (and possibly modified) documents that exist inside this document
80 * whose stringified contents should be used instead of what is in `ast`.
81 */
82 inlineDocuments?: ParsedDocument[];
83 /**
84 * Decide if the document should be formatted for readability, if supported.
85 * This option should default to `true` in subclasses.
86 */
87 prettyPrint?: boolean;
88}
89/**
90 * Used solely for constructing warnings about unparsable or unloadable
91 * documents.
92 */
93export declare class UnparsableParsedDocument extends ParsedDocument {
94 type: string;
95 constructor(url: ResolvedUrl, contents: string);
96 visit(_visitors: {}[]): void;
97 protected _sourceRangeForNode(_node: {}): undefined;
98 stringify(): string;
99}