Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/1263'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Aug 9, 2020
2 parents 2f8d295 + 4decfbe commit cce8bf6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
25 changes: 14 additions & 11 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 @@ -219,14 +220,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 TypeDocOptions): boolean;
isDefault(name: NeverIfInternal<string>): boolean;
isDefault(name: string): boolean {
// getValue will throw if the declaration does not exist.
return this.getValue(name as keyof TypeDocOptions) === this.getDeclaration(name)!.defaultValue;
return isDeepStrictEqual(this.getValue(name as keyof TypeDocOptions), this.getDefaultOptionValue(this.getDeclaration(name)!));
}

/**
Expand Down Expand Up @@ -300,15 +301,17 @@ export class Options {
*/
private setOptionValueToDefault(declaration: Readonly<DeclarationOption>): void {
if (declaration.scope !== ParameterScope.TypeScript) {
// No need 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 if (declaration.type === ParameterType.Number) {
// Don't use convert for number options to allow every possible number as a default value
this._values[declaration.name] = declaration.defaultValue || 0;
} else {
this._values[declaration.name] = convert(declaration.defaultValue, declaration);
}
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
// Also don't use convert for number options to allow every possible number as a default value.
if (declaration.type === ParameterType.Map || declaration.type === ParameterType.Number) {
return declaration.defaultValue;
} else {
return convert(declaration.defaultValue, declaration);
}
}
}
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);
logger?.diagnostics(errors);

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

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'));
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'));
options.setValue('inputFiles', ['foo.ts']);
options.read(new Logger());
equal(options.getValue('inputFiles'), ['foo.ts']);
});
});

0 comments on commit cce8bf6

Please sign in to comment.