-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathTSDocConfigFile.ts
More file actions
446 lines (386 loc) · 14.5 KB
/
TSDocConfigFile.ts
File metadata and controls
446 lines (386 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import {
TSDocTagDefinition,
TSDocTagSyntaxKind,
TSDocConfiguration,
ParserMessageLog,
TSDocMessageId,
ParserMessage,
TextRange,
IParserMessageParameters
} from '@microsoft/tsdoc';
import * as fs from 'fs';
import * as resolve from 'resolve';
import * as path from 'path';
import * as Ajv from 'ajv';
import * as jju from 'jju';
const ajv: Ajv.Ajv = new Ajv({ verbose: true });
function initializeSchemaValidator(): Ajv.ValidateFunction {
const jsonSchemaPath: string = resolve.sync('@microsoft/tsdoc/schemas/tsdoc.schema.json', { basedir: __dirname });
const jsonSchemaContent: string = fs.readFileSync(jsonSchemaPath).toString();
const jsonSchema: object = jju.parse(jsonSchemaContent, { mode: 'cjson' });
return ajv.compile(jsonSchema);
}
// Warning: AJV has a fairly strange API. Each time this function is called, the function object's
// properties get overwritten with the results of the latest validation. Thus we need to be careful
// to read the properties before a subsequent call may occur.
const tsdocSchemaValidator: Ajv.ValidateFunction = initializeSchemaValidator();
interface ITagConfigJson {
tagName: string;
syntaxKind: 'inline' | 'block' | 'modifier';
allowMultiple?: boolean;
synonyms?: string[];
}
interface ISynonymConfigJson {
add?: ISynonymSetJson;
remove?: ISynonymSetJson;
}
interface ISynonymSetJson {
[tagName: string]: string[];
}
interface IConfigJson {
$schema: string;
tsdocVersion: string;
extends?: string[];
tagDefinitions: ITagConfigJson[];
synonyms?: ISynonymConfigJson;
}
/**
* Represents an individual `tsdoc.json` file.
*
* @public
*/
export class TSDocConfigFile {
public static readonly FILENAME: string = 'tsdoc.json';
public static readonly CURRENT_SCHEMA_URL: string
= 'https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json';
/**
* A queryable log that reports warnings and error messages that occurred during parsing.
*/
public readonly log: ParserMessageLog;
private readonly _extendsFiles: TSDocConfigFile[];
private _filePath: string;
private _fileNotFound: boolean;
private _fileMTime: number;
private _hasErrors: boolean;
private _tsdocSchema: string;
private readonly _extendsPaths: string[];
private readonly _tagDefinitions: TSDocTagDefinition[];
private readonly _synonymAdditions: Map<string, string[]>;
private readonly _synonymDeletions: Map<string, string[]>;
private constructor() {
this.log = new ParserMessageLog();
this._extendsFiles = [];
this._filePath = '';
this._fileNotFound = true;
this._hasErrors = false;
this._fileMTime = 0;
this._tsdocSchema = '';
this._extendsPaths = [];
this._tagDefinitions = [];
this._synonymAdditions = new Map<string, string[]>();
this._synonymDeletions = new Map<string, string[]>();
}
/**
* Other config files that this file extends from.
*/
public get extendsFiles(): ReadonlyArray<TSDocConfigFile> {
return this._extendsFiles;
}
/**
* The full path of the file that was attempted to load.
*/
public get filePath(): string {
return this._filePath;
}
/**
* If true, then the TSDocConfigFile object contains an empty state, because the `tsdoc.json` file could
* not be found by the loader.
*/
public get fileNotFound(): boolean {
return this._fileNotFound;
}
/**
* If true, then at least one error was encountered while loading this file or one of its "extends" files.
*
* @remarks
* You can use {@link TSDocConfigFile.getErrorSummary} to report these errors.
*
* The individual messages can be retrieved from the {@link TSDocConfigFile.log} property of each `TSDocConfigFile`
* object (including the {@link TSDocConfigFile.extendsFiles} tree).
*/
public get hasErrors(): boolean {
return this._hasErrors;
}
/**
* The `$schema` field from the `tsdoc.json` file.
*/
public get tsdocSchema(): string {
return this._tsdocSchema;
}
/**
* The `extends` field from the `tsdoc.json` file. For the parsed file contents,
* use the `extendsFiles` property instead.
*/
public get extendsPaths(): ReadonlyArray<string> {
return this._extendsPaths;
}
public get tagDefinitions(): ReadonlyArray<TSDocTagDefinition> {
return this._tagDefinitions;
}
public get synonymAdditions(): ReadonlyMap<string, ReadonlyArray<string>> {
return this._synonymAdditions;
}
public get synonymDeletions(): ReadonlyMap<string, ReadonlyArray<string>> {
return this._synonymDeletions;
}
/**
* This can be used for cache eviction. It returns true if the modification timestamp has changed for
* any of the files that were read when loading this `TSDocConfigFile`, which indicates that the file should be
* reloaded. It does not consider cases where `TSDocConfigFile.fileNotFound` was `true`.
*
* @remarks
* This can be used for cache eviction. An example eviction strategy might be like this:
*
* - call `checkForModifiedFiles()` once per second, and reload the configuration if it returns true
*
* - otherwise, reload the configuration when it is more than 10 seconds old (to handle less common cases such
* as creation of a missing file, or creation of a file at an earlier location in the search path).
*/
public checkForModifiedFiles(): boolean {
if (this._checkForModifiedFile()) {
return true;
}
for (const extendsFile of this.extendsFiles) {
if (extendsFile.checkForModifiedFiles()) {
return true;
}
}
return false;
}
/**
* Checks the last modification time for `TSDocConfigFile.filePath` and returns `true` if it has changed
* since the file was loaded. If the file is missing, this returns `false`. If the timestamp cannot be read,
* then this returns `true`.
*/
private _checkForModifiedFile(): boolean {
if (this._fileNotFound || !this._filePath) {
return false;
}
try {
const mtimeMs: number = fs.statSync(this._filePath).mtimeMs;
return mtimeMs !== this._fileMTime;
} catch (error) {
return true;
}
}
private _reportError(parserMessageParameters: IParserMessageParameters): void {
this.log.addMessage(new ParserMessage(parserMessageParameters));
this._hasErrors = true;
}
private _loadJsonFile(): void {
const configJsonContent: string = fs.readFileSync(this._filePath).toString();
this._fileMTime = fs.statSync(this._filePath).mtimeMs;
this._fileNotFound = false;
const configJson: IConfigJson = jju.parse(configJsonContent, { mode: 'cjson' });
if (configJson.$schema !== TSDocConfigFile.CURRENT_SCHEMA_URL) {
this._reportError({
messageId: TSDocMessageId.ConfigFileUnsupportedSchema,
messageText: `Unsupported JSON "$schema" value; expecting "${TSDocConfigFile.CURRENT_SCHEMA_URL}"`,
textRange: TextRange.empty
});
return;
}
const success: boolean = tsdocSchemaValidator(configJson) as boolean;
if (!success) {
const description: string = ajv.errorsText(tsdocSchemaValidator.errors);
this._reportError({
messageId: TSDocMessageId.ConfigFileSchemaError,
messageText: 'Error loading config file: ' + description,
textRange: TextRange.empty
});
return;
}
this._tsdocSchema = configJson.$schema;
if (configJson.extends) {
this._extendsPaths.push(...configJson.extends);
}
for (const jsonTagDefinition of configJson.tagDefinitions || []) {
let syntaxKind: TSDocTagSyntaxKind;
switch (jsonTagDefinition.syntaxKind) {
case 'inline': syntaxKind = TSDocTagSyntaxKind.InlineTag; break;
case 'block': syntaxKind = TSDocTagSyntaxKind.BlockTag; break;
case 'modifier': syntaxKind = TSDocTagSyntaxKind.ModifierTag; break;
default:
// The JSON schema should have caught this error
throw new Error('Unexpected tag kind');
}
this._tagDefinitions.push(new TSDocTagDefinition({
tagName: jsonTagDefinition.tagName,
syntaxKind: syntaxKind,
synonyms: jsonTagDefinition.synonyms,
allowMultiple: jsonTagDefinition.allowMultiple
}));
}
if (configJson.synonyms) {
if (configJson.synonyms.add) {
for (const tagName of Object.keys(configJson.synonyms.add)) {
this._synonymAdditions.set(tagName, configJson.synonyms.add[tagName]);
}
}
if (configJson.synonyms.remove) {
for (const tagName of Object.keys(configJson.synonyms.remove)) {
this._synonymDeletions.set(tagName, configJson.synonyms.remove[tagName]);
}
}
}
}
private _loadWithExtends(configFilePath: string, referencingConfigFile: TSDocConfigFile | undefined,
alreadyVisitedPaths: Set<string>): void {
if (!configFilePath) {
this._reportError({
messageId: TSDocMessageId.ConfigFileNotFound,
messageText: 'File not found',
textRange: TextRange.empty
});
return;
}
this._filePath = path.resolve(configFilePath);
if (!fs.existsSync(this._filePath)) {
this._reportError({
messageId: TSDocMessageId.ConfigFileNotFound,
messageText: 'File not found',
textRange: TextRange.empty
});
return;
}
const hashKey: string = fs.realpathSync(this._filePath);
if (referencingConfigFile && alreadyVisitedPaths.has(hashKey)) {
this._reportError({
messageId: TSDocMessageId.ConfigFileCyclicExtends,
messageText: `Circular reference encountered for "extends" field of "${referencingConfigFile.filePath}"`,
textRange: TextRange.empty
});
return;
}
alreadyVisitedPaths.add(hashKey);
this._loadJsonFile();
const configFileFolder: string = path.dirname(this.filePath);
for (const extendsField of this.extendsPaths) {
const resolvedExtendsPath: string = resolve.sync(extendsField, { basedir: configFileFolder });
const baseConfigFile: TSDocConfigFile = new TSDocConfigFile();
baseConfigFile._loadWithExtends(resolvedExtendsPath, this, alreadyVisitedPaths);
if (baseConfigFile.fileNotFound) {
this._reportError({
messageId: TSDocMessageId.ConfigFileUnresolvedExtends,
messageText: `Unable to resolve "extends" reference to "${extendsField}"`,
textRange: TextRange.empty
});
}
this._extendsFiles.push(baseConfigFile);
if (baseConfigFile.hasErrors) {
this._hasErrors = true;
}
}
}
/**
* For the given folder, look for the relevant tsdoc.json file (if any), and return its path.
*
* @param folderPath - the path to a folder where the search should start
* @returns the (possibly relative) path to tsdoc.json, or an empty string if not found
*/
public static findConfigPathForFolder(folderPath: string): string {
if (folderPath) {
let foundFolder: string = folderPath;
for (;;) {
const tsconfigJsonPath: string = path.join(foundFolder, 'tsconfig.json');
if (fs.existsSync(tsconfigJsonPath)) {
// Stop when we reach a folder containing tsconfig.json
return path.join(foundFolder, TSDocConfigFile.FILENAME);
}
const packageJsonPath: string = path.join(foundFolder, 'package.json');
if (fs.existsSync(packageJsonPath)) {
// Stop when we reach a folder containing package.json; this avoids crawling out of the current package
return path.join(foundFolder, TSDocConfigFile.FILENAME);
}
const previousFolder: string = foundFolder;
foundFolder = path.dirname(foundFolder);
if (!foundFolder || foundFolder === previousFolder) {
// Give up if we reach the filesystem root directory
break;
}
}
}
return '';
}
/**
* Calls `TSDocConfigFile.findConfigPathForFolder()` to find the relevant tsdoc.json config file, if one exists.
* Then calls `TSDocConfigFile.findConfigPathForFolder()` to return the loaded result.
* @param folderPath - the path to a folder where the search should start
*/
public static loadForFolder(folderPath: string): TSDocConfigFile {
const rootConfigPath: string = TSDocConfigFile.findConfigPathForFolder(folderPath);
return TSDocConfigFile.loadFile(rootConfigPath);
}
/**
* Loads the specified tsdoc.json and any base files that it refers to using the "extends" option.
* @param tsdocJsonFilePath - the path to the tsdoc.json config file
*/
public static loadFile(tsdocJsonFilePath: string): TSDocConfigFile {
const configFile: TSDocConfigFile = new TSDocConfigFile();
const alreadyVisitedPaths: Set<string> = new Set<string>();
configFile._loadWithExtends(tsdocJsonFilePath, undefined, alreadyVisitedPaths);
return configFile;
}
/**
* Returns a report of any errors that occurred while attempting to load this file or any files
* referenced via the "extends" field.
*
* @remarks
* Use {@link TSDocConfigFile.hasErrors} to determine whether any errors occurred.
*/
public getErrorSummary(): string {
if (!this._hasErrors) {
return 'No errors.';
}
let result: string = `Errors encountered for ${this.filePath}:\n`;
for (const message of this.log.messages) {
result += ` ${message.text}\n`;
}
for (const extendsFile of this.extendsFiles) {
if (extendsFile.hasErrors) {
result += extendsFile.getErrorSummary();
}
}
return result;
}
/**
* Applies the settings from this config file to a TSDoc parser configuration.
* Any `extendsFile` settings will also applied.
*/
public configureParser(configuration: TSDocConfiguration): void {
// First apply the base config files
for (const extendsFile of this.extendsFiles) {
extendsFile.configureParser(configuration);
}
// Then apply this one
for (const tagDefinition of this.tagDefinitions) {
configuration.addTagDefinition(tagDefinition);
}
this.synonymDeletions.forEach((synonyms, tagName) => {
const tagDefinition: TSDocTagDefinition | undefined
= configuration.tryGetTagDefinition(tagName);
if (!tagDefinition) {
throw new Error(`A tag with the name ${tagName} could not be found.`);
}
configuration.removeSynonym(tagDefinition, ...synonyms);
});
this.synonymAdditions.forEach((synonyms, tagName) => {
const tagDefinition: TSDocTagDefinition | undefined
= configuration.tryGetTagDefinition(tagName);
if (!tagDefinition) {
throw new Error(`A tag with the name ${tagName} could not be found.`);
}
configuration.addSynonym(tagDefinition, ...synonyms);
});
}
}