Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/lib/utils/options/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDeepStrictEqual } from 'util';
import * as _ from 'lodash';
import * as ts from 'typescript';

Expand Down Expand Up @@ -214,14 +215,14 @@ export class Options {
}

/**
* Checks if the given option has a set value or if the value is the default value.
* Checks if the given option's value is deeply strict equal to the default.
* @param name
*/
isDefault(name: keyof TypeDocAndTSOptions): boolean;
isDefault(name: string): boolean;
isDefault(name: string): boolean {
// getValue will throw if the declaration does not exist.
return this.getValue(name) === this.getDeclaration(name)!.defaultValue;
return isDeepStrictEqual(this.getValue(name), this.getDefaultOptionValue(this.getDeclaration(name)!));
}

/**
Expand Down Expand Up @@ -317,17 +318,21 @@ export class Options {

/**
* Sets the value of a given option to its default value.
* @param declaration The option whoes value should be reset.
* @param declaration The option whose value should be reset.
*/
private setOptionValueToDefault(declaration: Readonly<DeclarationOption>): void {
if (declaration.scope !== ParameterScope.TypeScript) {
// No nead to convert the defaultValue for a map type as it has to be of a specific type
if (declaration.type === ParameterType.Map) {
this._values[declaration.name] = declaration.defaultValue;
} else {
this._values[declaration.name] = convert(declaration.defaultValue, declaration)
.expect(`Failed to validate default value for ${declaration.name}`);
}
this._values[declaration.name] = this.getDefaultOptionValue(declaration);
}
}

private getDefaultOptionValue(declaration: Readonly<DeclarationOption>): unknown {
// No need to convert the defaultValue for a map type as it has to be of a specific type
if (declaration.type === ParameterType.Map) {
return declaration.defaultValue;
} else {
return convert(declaration.defaultValue, declaration)
.expect(`Failed to validate default value for ${declaration.name}`);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/lib/utils/options/readers/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@ export class TSConfigReader implements OptionsReader {
fileToRead = resolve(fileToRead);

const { config } = ts.readConfigFile(fileToRead, ts.sys.readFile);
const { fileNames, options, raw: { typedocOptions = {} }} = ts.parseJsonConfigFileContent(
const { fileNames, errors, options, raw: { typedocOptions = {} }} = ts.parseJsonConfigFileContent(
config,
ts.sys,
dirname(fileToRead),
{},
fileToRead);

container.setValue('inputFiles', fileNames).unwrap();
logger?.diagnostics(errors);

if (container.isDefault('inputFiles')) {
container.setValue('inputFiles', fileNames).unwrap();
}

for (const key of IGNORED) {
delete options[key];
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/utils/options/readers/data/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This is referenced in valid.tsconfig.json
export const test = true;
5 changes: 5 additions & 0 deletions src/test/utils/options/readers/data/valid.tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"compilerOptions": {
"target": "ESNext"
},
"files": [
// This has to specify a file that exists or TS will drop it.
"./file.ts"
],
"typedocOptions": {
"help": true
}
Expand Down
17 changes: 16 additions & 1 deletion src/test/utils/options/readers/tsconfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path';
import { join, resolve } from 'path';
import { deepStrictEqual as equal } from 'assert';

import { TSConfigReader } from '../../../../lib/utils/options/readers';
Expand Down Expand Up @@ -48,4 +48,19 @@ describe('Options - TSConfigReader', () => {
equal(options.getValue('help'), true);
equal(options.getCompilerOptions().target, ScriptTarget.ESNext);
});

it('Sets inputFiles if they have not been set', () => {
options.reset();
options.setValue('tsconfig', join(__dirname, 'data/valid.tsconfig.json')).unwrap();
options.read(new Logger());
equal(options.getValue('inputFiles').map(f => resolve(f)), [resolve(__dirname, './data/file.ts')]);
});

it('Does not set inputFiles if they have been set', () => {
options.reset();
options.setValue('tsconfig', join(__dirname, 'data/valid.tsconfig.json')).unwrap();
options.setValue('inputFiles', ['foo.ts']).unwrap();
options.read(new Logger());
equal(options.getValue('inputFiles'), ['foo.ts']);
});
});