Skip to content

Commit

Permalink
fix: Only set inputFiles from tsconfig if not already set
Browse files Browse the repository at this point in the history
fix: Options.isDefault was always false when passed non-literals.

Closes #1263
  • Loading branch information
Gerrit0 committed Apr 12, 2020
1 parent 2f38fc5 commit 5878278
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/lib/utils/options/options.ts
@@ -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
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
@@ -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
@@ -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
@@ -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']);
})
});

0 comments on commit 5878278

Please sign in to comment.