1 |
|
2 |
|
3 |
|
4 | export namespace parser {
|
5 | export class ParseResult {
|
6 |
|
7 | dependencies: depGraph.Dependency[];
|
8 |
|
9 | errors: ParseError[];
|
10 |
|
11 | hasFatalError: boolean;
|
12 |
|
13 | source: ParseResult.Source;
|
14 |
|
15 | isFromDepsFile: boolean;
|
16 | constructor(
|
17 | dependencies: depGraph.Dependency[],
|
18 | errors: ParseError[],
|
19 | source: ParseResult.Source
|
20 | );
|
21 | }
|
22 |
|
23 | namespace ParseResult {
|
24 |
|
25 |
|
26 | enum Source {
|
27 | |
28 |
|
29 |
|
30 | SOURCE_FILE = "f",
|
31 |
|
32 | |
33 |
|
34 |
|
35 | GOOG_ADD_DEPENDENCY = "d",
|
36 | }
|
37 | }
|
38 |
|
39 | export class ParseError {
|
40 |
|
41 | fatal: boolean;
|
42 |
|
43 | message: string;
|
44 |
|
45 | sourceName: string;
|
46 |
|
47 | line: number;
|
48 |
|
49 | lineOffset: number;
|
50 |
|
51 | constructor(
|
52 | fatal: boolean,
|
53 | message: string,
|
54 | sourceName: string,
|
55 | line: number,
|
56 | lineOffset: number
|
57 | );
|
58 | }
|
59 |
|
60 | export function parseFile(path: string): ParseResult;
|
61 |
|
62 | export function parseFileAsync(path: string): Promise<ParseResult>;
|
63 |
|
64 | /**
|
65 | * Parses a file that contains only goog.addDependency statements. This is regex
|
66 | * based to be lightweight and avoid addtional dependencies.
|
67 | */
|
68 | export function parseDependencyFile(
|
69 | text: string,
|
70 | filePath: string
|
71 | ): ParseResult;
|
72 | }
|
73 |
|
74 | export namespace depGraph {
|
75 | export enum DependencyType {
|
76 |
|
77 | CLOSURE_PROVIDE = "closure provide",
|
78 |
|
79 | CLOSURE_MODULE = "closure module",
|
80 |
|
81 | ES6_MODULE = "es6 module",
|
82 |
|
83 | SCRIPT = "script",
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 | export class Dependency {
|
90 |
|
91 | type: DependencyType;
|
92 | |
93 |
|
94 |
|
95 |
|
96 | path: string;
|
97 | |
98 |
|
99 |
|
100 |
|
101 | closureSymbols: string[];
|
102 | |
103 |
|
104 |
|
105 |
|
106 | imports: Import[];
|
107 | |
108 |
|
109 |
|
110 |
|
111 | language: string | null;
|
112 |
|
113 | constructor(
|
114 | type: DependencyType,
|
115 | filepath: string,
|
116 | closureSymbols: string[],
|
117 | imports: Import[],
|
118 | language?: string
|
119 | );
|
120 |
|
121 | /**
|
122 | * Updates the path to Closure Library for this file. This is useful for
|
123 | * ParsedDependency, which cannot know the full path of a file on until it
|
124 | * knows the path to Closure Library, as the path in the goog.addDependency
|
125 | * call is relative from Closure Library.
|
126 | */
|
127 | setClosurePath(path: string): void;
|
128 |
|
129 | isParsedFromDepsFile(): boolean;
|
130 | }
|
131 |
|
132 | /**
|
133 | * Generic super class for all types of imports. This acts as an edge in the
|
134 | * dependency graph between two dependencies.
|
135 | */
|
136 | export abstract class Import {
|
137 | |
138 |
|
139 |
|
140 | from: Dependency;
|
141 | |
142 |
|
143 |
|
144 |
|
145 | symOrPath: string;
|
146 |
|
147 | constructor(symOrPath: string);
|
148 |
|
149 | /**
|
150 | * Asserts that this import edge is valid.
|
151 | */
|
152 | abstract validate(to: Dependency): void;
|
153 | abstract isGoogRequire(): boolean;
|
154 | abstract isEs6Import(): boolean;
|
155 | }
|
156 |
|
157 | export class GoogRequire extends Import {
|
158 | validate(to: Dependency): void;
|
159 | isGoogRequire(): true;
|
160 | isEs6Import(): false;
|
161 | }
|
162 |
|
163 | export class Es6Import extends Import {
|
164 | validate(to: Dependency): void;
|
165 | isGoogRequire(): true;
|
166 | isEs6Import(): false;
|
167 | }
|
168 |
|
169 | |
170 |
|
171 |
|
172 | export interface ModuleResolver {
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | resolve(fromPath: string, importSpec: string): string;
|
180 | }
|
181 |
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | export class Graph {
|
190 |
|
191 | depsBySymbol: Map<string, Dependency>;
|
192 |
|
193 | depsByPath: Map<string, Dependency>;
|
194 |
|
195 | moduleResolver: ModuleResolver;
|
196 |
|
197 | constructor(
|
198 | dependencies: readonly Dependency[],
|
199 | moduleResolver?: ModuleResolver
|
200 | );
|
201 |
|
202 | /**
|
203 | * Validates the dependency graph. Throws an error if the graph is invalid.
|
204 | *
|
205 | * This method uses Tarjan's algorithm to ensure Closure files are not part
|
206 | * of any cycle. Check it out:
|
207 | * https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
|
208 | */
|
209 | validate(): void;
|
210 |
|
211 | private resolve_(i: Import): Dependency;
|
212 |
|
213 | /**
|
214 | * Provides a topological sorting of dependencies given the entrypoints.
|
215 | */
|
216 | order(...entrypoints: Dependency[]): Dependency[];
|
217 | }
|
218 | }
|
219 |
|
220 | export namespace depFile {
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | export function getDepFileText(
|
231 | pathToClosure: string,
|
232 | dependencies: depGraph.Dependency[],
|
233 | moduleResolver?: depGraph.ModuleResolver
|
234 | ): string;
|
235 | }
|