Skip to content

Commit 1116a0b

Browse files
committed
Improve reporting of tsdoc-config-unresolved-extends
1 parent d24064f commit 1116a0b

File tree

6 files changed

+163
-76
lines changed

6 files changed

+163
-76
lines changed

tsdoc-config/src/TSDocConfigFile.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,18 @@ export class TSDocConfigFile {
405405
const configFileFolder: string = path.dirname(this.filePath);
406406

407407
for (const extendsField of this.extendsPaths) {
408-
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+
}
409420

410421
const baseConfigFile: TSDocConfigFile = new TSDocConfigFile();
411422

Lines changed: 138 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import { TSDocConfiguration } from '@microsoft/tsdoc';
12
import * as path from 'path';
23

34
import { TSDocConfigFile } from '../TSDocConfigFile';
45

56
interface ISnapshot {
6-
_0_filePath: string;
7-
_1_fileNotFound: boolean;
8-
_2_hasErrors: boolean;
9-
_3_errorSummary: string;
10-
_4_log: string[];
11-
_5_extends: ISnapshot[];
7+
s0_filePath: string;
8+
s1_fileNotFound: boolean;
9+
s2_hasErrors: boolean;
10+
s3_errorSummary: string;
11+
s4_log: string[];
12+
s5_extends: ISnapshot[];
1213
}
1314

1415
// To make the unit tests deterministic, we need to replace all OS-dependent absolute paths
@@ -22,9 +23,12 @@ function makeStablePath(testPath: string): string {
2223

2324
// Build a map from absolute path --> stable path, for each TSDocConfigFile.filePath value
2425
function buildStablePathMap(stablePathMap: Map<string, string>, configFile: TSDocConfigFile): void {
25-
if (!stablePathMap.has(configFile.filePath)) {
26-
stablePathMap.set(configFile.filePath, makeStablePath(configFile.filePath));
26+
for (const absolutePath of [configFile.filePath, path.dirname(configFile.filePath)]) {
27+
if (!stablePathMap.has(absolutePath)) {
28+
stablePathMap.set(absolutePath, makeStablePath(absolutePath));
29+
}
2730
}
31+
2832
for (const extendsFile of configFile.extendsFiles) {
2933
buildStablePathMap(stablePathMap, extendsFile);
3034
}
@@ -41,12 +45,12 @@ function convertToStablePaths(text: string, stablePathMap: Map<string, string>):
4145

4246
function createSnapshot(configFile: TSDocConfigFile, pathFixupMap: Map<string, string>): ISnapshot {
4347
return {
44-
_0_filePath: convertToStablePaths(configFile.filePath, pathFixupMap),
45-
_1_fileNotFound: configFile.fileNotFound,
46-
_2_hasErrors: configFile.hasErrors,
47-
_4_log: configFile.log.messages.map((x) => `[${x.messageId}] ${convertToStablePaths(x.text, pathFixupMap)}`),
48-
_5_extends: configFile.extendsFiles.map((x) => createSnapshot(x, pathFixupMap)),
49-
_3_errorSummary: convertToStablePaths(configFile.getErrorSummary(), pathFixupMap),
48+
s0_filePath: convertToStablePaths(configFile.filePath, pathFixupMap),
49+
s1_fileNotFound: configFile.fileNotFound,
50+
s2_hasErrors: configFile.hasErrors,
51+
s4_log: configFile.log.messages.map((x) => `[${x.messageId}] ${convertToStablePaths(x.text, pathFixupMap)}`),
52+
s5_extends: configFile.extendsFiles.map((x) => createSnapshot(x, pathFixupMap)),
53+
s3_errorSummary: convertToStablePaths(configFile.getErrorSummary(), pathFixupMap),
5054
};
5155
}
5256

@@ -62,139 +66,198 @@ function testLoadingFolder(assetPath: string): ISnapshot {
6266
test('Load e1', () => {
6367
expect(testLoadingFolder('assets/e1')).toMatchInlineSnapshot(`
6468
Object {
65-
"_0_filePath": ".../assets/e1/tsdoc.json",
66-
"_1_fileNotFound": false,
67-
"_2_hasErrors": true,
68-
"_3_errorSummary": "Error encountered for .../assets/e1/tsdoc.json:
69+
"s0_filePath": ".../assets/e1/tsdoc.json",
70+
"s1_fileNotFound": false,
71+
"s2_hasErrors": true,
72+
"s3_errorSummary": "Error encountered for .../assets/e1/tsdoc.json:
6973
Error loading config file: data should NOT have additional properties
7074
",
71-
"_4_log": Array [
75+
"s4_log": Array [
7276
"[tsdoc-config-schema-error] Error loading config file: data should NOT have additional properties",
7377
],
74-
"_5_extends": Array [],
78+
"s5_extends": Array [],
7579
}
7680
`);
7781
});
7882

7983
test('Load e2', () => {
8084
expect(testLoadingFolder('assets/e2')).toMatchInlineSnapshot(`
8185
Object {
82-
"_0_filePath": ".../assets/e2/tsdoc.json",
83-
"_1_fileNotFound": false,
84-
"_2_hasErrors": true,
85-
"_3_errorSummary": "Error encountered for .../assets/e2/tsdoc.json:
86+
"s0_filePath": ".../assets/e2/tsdoc.json",
87+
"s1_fileNotFound": false,
88+
"s2_hasErrors": true,
89+
"s3_errorSummary": "Error encountered for .../assets/e2/tsdoc.json:
8690
Error parsing JSON input: Unexpected token '\\\\n' at 3:12
8791
\\"invalid
8892
^
8993
",
90-
"_4_log": Array [
94+
"s4_log": Array [
9195
"[tsdoc-config-invalid-json] Error parsing JSON input: Unexpected token '\\\\n' at 3:12
9296
\\"invalid
9397
^",
9498
],
95-
"_5_extends": Array [],
99+
"s5_extends": Array [],
96100
}
97101
`);
98102
});
99103

100104
test('Load e3', () => {
101105
expect(testLoadingFolder('assets/e3')).toMatchInlineSnapshot(`
102106
Object {
103-
"_0_filePath": ".../assets/e3/tsdoc.json",
104-
"_1_fileNotFound": false,
105-
"_2_hasErrors": true,
106-
"_3_errorSummary": "Error encountered for .../assets/e3/tsdoc.json:
107+
"s0_filePath": ".../assets/e3/tsdoc.json",
108+
"s1_fileNotFound": false,
109+
"s2_hasErrors": true,
110+
"s3_errorSummary": "Error encountered for .../assets/e3/tsdoc.json:
107111
Unsupported JSON \\"$schema\\" value; expecting \\"https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json\\"
108112
",
109-
"_4_log": Array [
113+
"s4_log": Array [
110114
"[tsdoc-config-unsupported-schema] Unsupported JSON \\"$schema\\" value; expecting \\"https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json\\"",
111115
],
112-
"_5_extends": Array [],
116+
"s5_extends": Array [],
113117
}
114118
`);
115119
});
116120

117121
test('Load e4', () => {
118122
expect(testLoadingFolder('assets/e4')).toMatchInlineSnapshot(`
119123
Object {
120-
"_0_filePath": ".../assets/e4/tsdoc.json",
121-
"_1_fileNotFound": false,
122-
"_2_hasErrors": true,
123-
"_3_errorSummary": "Error encountered for .../assets/e4/tsdoc.json:
124+
"s0_filePath": ".../assets/e4/tsdoc.json",
125+
"s1_fileNotFound": false,
126+
"s2_hasErrors": true,
127+
"s3_errorSummary": "Error encountered for .../assets/e4/tsdoc.json:
124128
The \\"tagDefinitions\\" field specifies more than one tag with the name \\"@dupe\\"
125129
",
126-
"_4_log": Array [
130+
"s4_log": Array [
127131
"[tsdoc-config-duplicate-tag-name] The \\"tagDefinitions\\" field specifies more than one tag with the name \\"@dupe\\"",
128132
],
129-
"_5_extends": Array [],
133+
"s5_extends": Array [],
130134
}
131135
`);
132136
});
133137

134138
test('Load e5', () => {
135139
expect(testLoadingFolder('assets/e5')).toMatchInlineSnapshot(`
136140
Object {
137-
"_0_filePath": ".../assets/e5/tsdoc.json",
138-
"_1_fileNotFound": false,
139-
"_2_hasErrors": true,
140-
"_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
141-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
141+
"s0_filePath": ".../assets/e5/tsdoc.json",
142+
"s1_fileNotFound": false,
143+
"s2_hasErrors": true,
144+
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
145+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
142146
143-
Error encountered for .../assets/e5/tsdoc-c.json:
147+
Error encountered for .../assets/e5\\\\tsdoc-c.json:
144148
Error loading config file: data should NOT have additional properties
145149
",
146-
"_4_log": Array [],
147-
"_5_extends": Array [
150+
"s4_log": Array [],
151+
"s5_extends": Array [
148152
Object {
149-
"_0_filePath": ".../assets/e5/tsdoc-a.json",
150-
"_1_fileNotFound": false,
151-
"_2_hasErrors": true,
152-
"_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
153-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
153+
"s0_filePath": ".../assets/e5\\\\tsdoc-a.json",
154+
"s1_fileNotFound": false,
155+
"s2_hasErrors": true,
156+
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
157+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
154158
155-
Error encountered for .../assets/e5/tsdoc-c.json:
159+
Error encountered for .../assets/e5\\\\tsdoc-c.json:
156160
Error loading config file: data should NOT have additional properties
157161
",
158-
"_4_log": Array [],
159-
"_5_extends": Array [
162+
"s4_log": Array [],
163+
"s5_extends": Array [
160164
Object {
161-
"_0_filePath": ".../assets/e5/tsdoc-b.json",
162-
"_1_fileNotFound": false,
163-
"_2_hasErrors": true,
164-
"_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
165-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
165+
"s0_filePath": ".../assets/e5\\\\tsdoc-b.json",
166+
"s1_fileNotFound": false,
167+
"s2_hasErrors": true,
168+
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
169+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
166170
",
167-
"_4_log": Array [],
168-
"_5_extends": Array [
171+
"s4_log": Array [],
172+
"s5_extends": Array [
169173
Object {
170-
"_0_filePath": ".../assets/e5/tsdoc-a.json",
171-
"_1_fileNotFound": false,
172-
"_2_hasErrors": true,
173-
"_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
174-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
174+
"s0_filePath": ".../assets/e5\\\\tsdoc-a.json",
175+
"s1_fileNotFound": false,
176+
"s2_hasErrors": true,
177+
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
178+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
175179
",
176-
"_4_log": Array [
177-
"[tsdoc-config-cyclic-extends] Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"",
180+
"s4_log": Array [
181+
"[tsdoc-config-cyclic-extends] Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"",
178182
],
179-
"_5_extends": Array [],
183+
"s5_extends": Array [],
180184
},
181185
],
182186
},
183187
Object {
184-
"_0_filePath": ".../assets/e5/tsdoc-c.json",
185-
"_1_fileNotFound": false,
186-
"_2_hasErrors": true,
187-
"_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-c.json:
188+
"s0_filePath": ".../assets/e5\\\\tsdoc-c.json",
189+
"s1_fileNotFound": false,
190+
"s2_hasErrors": true,
191+
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-c.json:
188192
Error loading config file: data should NOT have additional properties
189193
",
190-
"_4_log": Array [
194+
"s4_log": Array [
191195
"[tsdoc-config-schema-error] Error loading config file: data should NOT have additional properties",
192196
],
193-
"_5_extends": Array [],
197+
"s5_extends": Array [],
194198
},
195199
],
196200
},
197201
],
198202
}
199203
`);
200204
});
205+
206+
test('Load e6', () => {
207+
expect(testLoadingFolder('assets/e6')).toMatchInlineSnapshot(`
208+
Object {
209+
"s0_filePath": ".../assets/e6/tsdoc.json",
210+
"s1_fileNotFound": false,
211+
"s2_hasErrors": true,
212+
"s3_errorSummary": "Error encountered for .../assets/e6/tsdoc.json:
213+
Unable to resolve \\"extends\\" reference to \\"@rushstack/nonexistent-package/tsdoc.json\\": Cannot find module '@rushstack/nonexistent-package/tsdoc.json' from '.../assets/e6'
214+
",
215+
"s4_log": Array [
216+
"[tsdoc-config-unresolved-extends] Unable to resolve \\"extends\\" reference to \\"@rushstack/nonexistent-package/tsdoc.json\\": Cannot find module '@rushstack/nonexistent-package/tsdoc.json' from '.../assets/e6'",
217+
],
218+
"s5_extends": Array [],
219+
}
220+
`);
221+
});
222+
223+
test('Load e7', () => {
224+
const configFile: TSDocConfigFile = TSDocConfigFile.loadForFolder(path.join(__dirname, 'assets/e7'));
225+
226+
const pathFixupMap: Map<string, string> = new Map();
227+
buildStablePathMap(pathFixupMap, configFile);
228+
229+
expect(createSnapshot(configFile, pathFixupMap)).toMatchInlineSnapshot(`
230+
Object {
231+
"s0_filePath": ".../assets/e7/tsdoc.json",
232+
"s1_fileNotFound": false,
233+
"s2_hasErrors": false,
234+
"s3_errorSummary": "No errors.",
235+
"s4_log": Array [],
236+
"s5_extends": Array [],
237+
}
238+
`);
239+
240+
// The "tsdoc-config-undefined-tag" error is NOT detected by TSDocConfigFile.loadForFolder()
241+
expect(configFile.hasErrors).toBe(false);
242+
243+
const configuration: TSDocConfiguration = new TSDocConfiguration();
244+
configFile.configureParser(configuration);
245+
246+
expect(createSnapshot(configFile, pathFixupMap)).toMatchInlineSnapshot(`
247+
Object {
248+
"s0_filePath": ".../assets/e7/tsdoc.json",
249+
"s1_fileNotFound": false,
250+
"s2_hasErrors": true,
251+
"s3_errorSummary": "Error encountered for .../assets/e7/tsdoc.json:
252+
The \\"supportForTags\\" field refers to an undefined tag \\"@nonExistentTag\\".
253+
",
254+
"s4_log": Array [
255+
"[tsdoc-config-undefined-tag] The \\"supportForTags\\" field refers to an undefined tag \\"@nonExistentTag\\".",
256+
],
257+
"s5_extends": Array [],
258+
}
259+
`);
260+
261+
// The "tsdoc-config-undefined-tag" error IS detected by TSDocConfigFile.configureParser()
262+
expect(configFile.hasErrors).toBe(true);
263+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
3+
"extends": [ "@rushstack/nonexistent-package/tsdoc.json" ]
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
3+
4+
"supportForTags": {
5+
"@nonExistentTag": true
6+
}
7+
}

0 commit comments

Comments
 (0)