Skip to content

Commit 79fc78f

Browse files
authored
Merge pull request #290 from microsoft/octogonz/more-tsdoc-config-fixes
More error handling improvements
2 parents 1eb6706 + 80ead2d commit 79fc78f

18 files changed

+489
-203
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/tsdoc-config",
5+
"comment": "Improve reporting of tsdoc-config-unresolved-extends",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/tsdoc-config",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/tsdoc-config",
5+
"comment": "Fix some issues with the logic for TSDocConfigFile.hasErrors and TSDocConfigFile.fileNotFound",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/tsdoc-config",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/tsdoc",
5+
"comment": "Fix an issue where \"tsdoc-inline-tag-missing-braces\" and \"tsdoc-tag-should-not-have-braces\" were reported incorrectly",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/tsdoc",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}

tsdoc-config/src/TSDocConfigFile.ts

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class TSDocConfigFile {
7575

7676
this._extendsFiles = [];
7777
this._filePath = '';
78-
this._fileNotFound = true;
78+
this._fileNotFound = false;
7979
this._hasErrors = false;
8080
this._fileMTime = 0;
8181
this._tsdocSchema = '';
@@ -102,8 +102,11 @@ export class TSDocConfigFile {
102102
}
103103

104104
/**
105-
* If true, then the TSDocConfigFile object contains an empty state, because the `tsdoc.json` file could
106-
* not be found by the loader.
105+
* If true, then the TSDocConfigFile object contains an empty state, because the `tsdoc.json` file
106+
* was not found by the loader.
107+
*
108+
* @remarks
109+
* A missing "tsdoc.json" file is not considered an error. It simply means that the defaults will be used.
107110
*/
108111
public get fileNotFound(): boolean {
109112
return this._fileNotFound;
@@ -278,8 +281,6 @@ export class TSDocConfigFile {
278281
}
279282

280283
private _loadJsonObject(configJson: IConfigJson): void {
281-
this._fileNotFound = false;
282-
283284
if (configJson.$schema !== TSDocConfigFile.CURRENT_SCHEMA_URL) {
284285
this._reportError({
285286
messageId: TSDocMessageId.ConfigFileUnsupportedSchema,
@@ -347,6 +348,10 @@ export class TSDocConfigFile {
347348
referencingConfigFile: TSDocConfigFile | undefined,
348349
alreadyVisitedPaths: Set<string>
349350
): void {
351+
// In case an exception is thrown, start by assuming that the file was not found; we'll revise
352+
// this later upon success
353+
this._fileNotFound = true;
354+
350355
if (!configFilePath) {
351356
this._reportError({
352357
messageId: TSDocMessageId.ConfigFileNotFound,
@@ -367,6 +372,11 @@ export class TSDocConfigFile {
367372
return;
368373
}
369374

375+
const configJsonContent: string = fs.readFileSync(this._filePath).toString();
376+
this._fileMTime = fs.statSync(this._filePath).mtimeMs;
377+
378+
this._fileNotFound = false;
379+
370380
const hashKey: string = fs.realpathSync(this._filePath);
371381
if (referencingConfigFile && alreadyVisitedPaths.has(hashKey)) {
372382
this._reportError({
@@ -378,20 +388,15 @@ export class TSDocConfigFile {
378388
}
379389
alreadyVisitedPaths.add(hashKey);
380390

381-
const configJsonContent: string = fs.readFileSync(this._filePath).toString();
382-
this._fileMTime = fs.statSync(this._filePath).mtimeMs;
383-
384391
let configJson: IConfigJson;
385392
try {
386393
configJson = jju.parse(configJsonContent, { mode: 'cjson' });
387394
} catch (e) {
388-
this.log.addMessage(
389-
new ParserMessage({
390-
messageId: TSDocMessageId.ConfigInvalidJson,
391-
messageText: 'Error parsing JSON input: ' + e.message,
392-
textRange: TextRange.empty,
393-
})
394-
);
395+
this._reportError({
396+
messageId: TSDocMessageId.ConfigInvalidJson,
397+
messageText: 'Error parsing JSON input: ' + e.message,
398+
textRange: TextRange.empty,
399+
});
395400
return;
396401
}
397402

@@ -400,7 +405,18 @@ export class TSDocConfigFile {
400405
const configFileFolder: string = path.dirname(this.filePath);
401406

402407
for (const extendsField of this.extendsPaths) {
403-
const resolvedExtendsPath: string = resolve.sync(extendsField, { basedir: configFileFolder });
408+
let resolvedExtendsPath: string;
409+
try {
410+
resolvedExtendsPath = resolve.sync(extendsField, { basedir: configFileFolder });
411+
} catch (e) {
412+
this._reportError({
413+
messageId: TSDocMessageId.ConfigFileUnresolvedExtends,
414+
messageText: `Unable to resolve "extends" reference to "${extendsField}": ` + e.message,
415+
textRange: TextRange.empty,
416+
});
417+
418+
return;
419+
}
404420

405421
const baseConfigFile: TSDocConfigFile = new TSDocConfigFile();
406422

@@ -616,20 +632,26 @@ export class TSDocConfigFile {
616632
return 'No errors.';
617633
}
618634

619-
let result: string;
635+
let result: string = '';
620636

621-
if (this.filePath) {
622-
result = `Errors encountered for ${this.filePath}:\n`;
623-
} else {
624-
result = `Errors encountered when loading TSDoc configuration:\n`;
625-
}
637+
if (this.log.messages.length > 0) {
638+
const errorNoun: string = this.log.messages.length > 1 ? 'Errors' : 'Error';
639+
if (this.filePath) {
640+
result += `${errorNoun} encountered for ${this.filePath}:\n`;
641+
} else {
642+
result += `${errorNoun} encountered when loading TSDoc configuration:\n`;
643+
}
626644

627-
for (const message of this.log.messages) {
628-
result += ` ${message.text}\n`;
645+
for (const message of this.log.messages) {
646+
result += ` ${message.text}\n`;
647+
}
629648
}
630649

631650
for (const extendsFile of this.extendsFiles) {
632651
if (extendsFile.hasErrors) {
652+
if (result !== '') {
653+
result += '\n';
654+
}
633655
result += extendsFile.getErrorSummary();
634656
}
635657
}
@@ -681,6 +703,8 @@ export class TSDocConfigFile {
681703
// Note that setSupportForTag() automatically enables configuration.validation.reportUnsupportedTags
682704
configuration.setSupportForTag(tagDefinition, supported);
683705
} else {
706+
// Note that this validation may depend partially on the preexisting state of the TSDocConfiguration
707+
// object, so it cannot be performed during the TSConfigFile.loadFile() stage.
684708
this._reportError({
685709
messageId: TSDocMessageId.ConfigFileUndefinedTag,
686710
messageText: `The "supportForTags" field refers to an undefined tag ${JSON.stringify(tagName)}.`,

0 commit comments

Comments
 (0)