Skip to content

Commit 7db88ef

Browse files
committed
Implement TSDocConfigFile state
1 parent 107100b commit 7db88ef

File tree

5 files changed

+121
-18
lines changed

5 files changed

+121
-18
lines changed

tsdoc-config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"typings": "lib/index.d.ts",
2121
"license": "MIT",
2222
"dependencies": {
23+
"@microsoft/tsdoc": "0.12.15",
2324
"ajv": "~6.10.2",
2425
"resolve": "~1.12.0"
2526
},

tsdoc-config/src/TSDocConfigFile.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1+
import { TSDocTagDefinition, TSDocTagSyntaxKind } from '@microsoft/tsdoc';
12
import * as fs from 'fs';
23
import * as path from 'path';
34
import * as Ajv from 'ajv';
45

5-
interface ITSDocConfigFileData {
6-
filePath: string;
7-
}
8-
96
const ajv: Ajv.Ajv = new Ajv({ verbose: true });
107

118
function initializeSchemaValidator(): Ajv.ValidateFunction {
@@ -21,15 +18,52 @@ function initializeSchemaValidator(): Ajv.ValidateFunction {
2118
// to read the properties before a subsequent call may occur.
2219
const tsdocSchemaValidator: Ajv.ValidateFunction = initializeSchemaValidator();
2320

21+
interface ITagConfigJson {
22+
tagName: string;
23+
syntaxKind: 'inline' | 'block' | 'modifier';
24+
allowMultiple?: boolean;
25+
}
26+
2427
interface IConfigJson {
28+
tsdocVersion: string;
29+
extends?: string[];
30+
tagDefinitions: ITagConfigJson[];
2531
}
2632

2733
export class TSDocConfigFile {
2834

2935
public readonly filePath: string;
3036

31-
private constructor(data: ITSDocConfigFileData) {
32-
this.filePath = data.filePath;
37+
public readonly tsdocVersion: string;
38+
39+
public readonly extends: ReadonlyArray<string>;
40+
41+
public readonly tagDefinitions: ReadonlyArray<TSDocTagDefinition>;
42+
43+
private constructor(filePath: string, configJson: IConfigJson) {
44+
this.filePath = filePath;
45+
this.tsdocVersion = configJson.tsdocVersion;
46+
this.extends = configJson.extends || [];
47+
const tagDefinitions: TSDocTagDefinition[] = [];
48+
49+
for (const jsonTagDefinition of configJson.tagDefinitions || []) {
50+
let syntaxKind: TSDocTagSyntaxKind;
51+
switch (jsonTagDefinition.syntaxKind) {
52+
case 'inline': syntaxKind = TSDocTagSyntaxKind.InlineTag; break;
53+
case 'block': syntaxKind = TSDocTagSyntaxKind.BlockTag; break;
54+
case 'modifier': syntaxKind = TSDocTagSyntaxKind.ModifierTag; break;
55+
default:
56+
// The JSON schema should have caught this error
57+
throw new Error('Unexpected tag kind');
58+
}
59+
tagDefinitions.push(new TSDocTagDefinition({
60+
tagName: jsonTagDefinition.tagName,
61+
syntaxKind: syntaxKind,
62+
allowMultiple: jsonTagDefinition.allowMultiple
63+
}));
64+
}
65+
66+
this.tagDefinitions = tagDefinitions;
3367
}
3468

3569
public static load(jsonFilePath: string): TSDocConfigFile {
@@ -46,8 +80,6 @@ export class TSDocConfigFile {
4680
+ '\nError in file: ' + jsonFilePath);
4781
}
4882

49-
return new TSDocConfigFile({
50-
filePath: fullJsonFilePath
51-
});
83+
return new TSDocConfigFile(fullJsonFilePath, configJson);
5284
}
5385
}

tsdoc-config/src/__tests__/ConfigLoader.test.ts

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,75 @@ import { TSDocConfigFile } from '../TSDocConfigFile';
44
import { ConfigLoader } from '../ConfigLoader';
55

66
function getRelativePath(testPath: string): string {
7-
return path.relative(__dirname, testPath).split('\\').join('/');
7+
return path
8+
.relative(__dirname, testPath)
9+
.split('\\')
10+
.join('/');
811
}
912

10-
test('Resolve p1', () => {
13+
test('Load p1', () => {
1114
const configFile: TSDocConfigFile | undefined = ConfigLoader.tryLoadFromPackageFolder(
12-
path.join(__dirname, 'assets/p1'));
15+
path.join(__dirname, 'assets/p1')
16+
);
1317
expect(configFile).toBeDefined();
1418
expect(getRelativePath(configFile!.filePath)).toEqual('assets/p1/folder/tsdoc-config.json');
1519
});
16-
test('Resolve p2', () => {
20+
test('Load p2', () => {
1721
const configFile: TSDocConfigFile | undefined = ConfigLoader.tryLoadFromPackageFolder(
18-
path.join(__dirname, 'assets/p2'));
22+
path.join(__dirname, 'assets/p2')
23+
);
1924
expect(configFile).toBeDefined();
2025
expect(getRelativePath(configFile!.filePath)).toEqual('assets/p2/folder/tsdoc-config.json');
2126
});
22-
test('Resolve p3', () => {
27+
test('Load p3', () => {
2328
const configFile: TSDocConfigFile | undefined = ConfigLoader.tryLoadFromPackageFolder(
24-
path.join(__dirname, 'assets/p3'));
29+
path.join(__dirname, 'assets/p3')
30+
);
2531
expect(configFile).toBeDefined();
2632
expect(getRelativePath(configFile!.filePath)).toEqual('assets/p3/folder/tsdoc-config.json');
2733
});
28-
test('Resolve p4', () => {
34+
test('Load p4', () => {
2935
const configFile: TSDocConfigFile | undefined = ConfigLoader.tryLoadFromPackageFolder(
30-
path.join(__dirname, 'assets/p4'));
36+
path.join(__dirname, 'assets/p4')
37+
);
3138
expect(configFile).toBeDefined();
3239
expect(getRelativePath(configFile!.filePath)).toEqual('assets/p4/tsdoc-config.json');
3340
});
41+
test('Load p5', () => {
42+
const configFile: TSDocConfigFile | undefined = ConfigLoader.tryLoadFromPackageFolder(
43+
path.join(__dirname, 'assets/p5')
44+
);
45+
expect(configFile).toMatchInlineSnapshot(
46+
{ filePath: expect.any(String) },
47+
`
48+
Object {
49+
"extends": Array [],
50+
"filePath": Any<String>,
51+
"tagDefinitions": Array [
52+
TSDocTagDefinition {
53+
"allowMultiple": true,
54+
"standardization": "None",
55+
"syntaxKind": 0,
56+
"tagName": "@myInlineTag",
57+
"tagNameWithUpperCase": "@MYINLINETAG",
58+
},
59+
TSDocTagDefinition {
60+
"allowMultiple": false,
61+
"standardization": "None",
62+
"syntaxKind": 1,
63+
"tagName": "@myBlockTag",
64+
"tagNameWithUpperCase": "@MYBLOCKTAG",
65+
},
66+
TSDocTagDefinition {
67+
"allowMultiple": false,
68+
"standardization": "None",
69+
"syntaxKind": 2,
70+
"tagName": "@myModifierTag",
71+
"tagNameWithUpperCase": "@MYMODIFIERTAG",
72+
},
73+
],
74+
"tsdocVersion": "0.12",
75+
}
76+
`
77+
);
78+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "p4",
3+
"version": "1.0.0"
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/tsdoc-config.schema.json",
3+
"tsdocVersion": "0.12",
4+
"extends": [],
5+
"tagDefinitions": [
6+
{
7+
"tagName": "@myInlineTag",
8+
"syntaxKind": "inline",
9+
"allowMultiple": true
10+
},
11+
{
12+
"tagName": "@myBlockTag",
13+
"syntaxKind": "block"
14+
},
15+
{
16+
"tagName": "@myModifierTag",
17+
"syntaxKind": "modifier",
18+
"allowMultiple": false
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)