From 5878278a2dcff02020fc646b1768b91459291224 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 11 Apr 2020 20:35:42 -0600 Subject: [PATCH 1/2] fix: Only set inputFiles from tsconfig if not already set fix: Options.isDefault was always false when passed non-literals. Closes #1263 --- src/lib/utils/options/options.ts | 25 +++++++++++-------- src/lib/utils/options/readers/tsconfig.ts | 9 +++++-- src/test/utils/options/readers/data/file.ts | 2 ++ .../options/readers/data/valid.tsconfig.json | 5 ++++ .../utils/options/readers/tsconfig.test.ts | 17 ++++++++++++- 5 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 src/test/utils/options/readers/data/file.ts diff --git a/src/lib/utils/options/options.ts b/src/lib/utils/options/options.ts index ef7fbd3e7..3f3cd1f12 100644 --- a/src/lib/utils/options/options.ts +++ b/src/lib/utils/options/options.ts @@ -1,3 +1,4 @@ +import { isDeepStrictEqual } from 'util'; import * as _ from 'lodash'; import * as ts from 'typescript'; @@ -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)!)); } /** @@ -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): 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): 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}`); } } } diff --git a/src/lib/utils/options/readers/tsconfig.ts b/src/lib/utils/options/readers/tsconfig.ts index 07c28d01b..3cb7b7a98 100644 --- a/src/lib/utils/options/readers/tsconfig.ts +++ b/src/lib/utils/options/readers/tsconfig.ts @@ -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]; } diff --git a/src/test/utils/options/readers/data/file.ts b/src/test/utils/options/readers/data/file.ts new file mode 100644 index 000000000..4974b1860 --- /dev/null +++ b/src/test/utils/options/readers/data/file.ts @@ -0,0 +1,2 @@ +// This is referenced in valid.tsconfig.json +export const test = true diff --git a/src/test/utils/options/readers/data/valid.tsconfig.json b/src/test/utils/options/readers/data/valid.tsconfig.json index fc47f601e..854836627 100644 --- a/src/test/utils/options/readers/data/valid.tsconfig.json +++ b/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 } diff --git a/src/test/utils/options/readers/tsconfig.test.ts b/src/test/utils/options/readers/tsconfig.test.ts index 8ed918ece..8a92a2f98 100644 --- a/src/test/utils/options/readers/tsconfig.test.ts +++ b/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'; @@ -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']); + }) }); From 4decfbe209ccb4aeee6800b4bfb9bbbd51c3efcf Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 11 Apr 2020 20:39:27 -0600 Subject: [PATCH 2/2] chore: Fix lint --- src/test/utils/options/readers/data/file.ts | 2 +- src/test/utils/options/readers/tsconfig.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/utils/options/readers/data/file.ts b/src/test/utils/options/readers/data/file.ts index 4974b1860..a5e460907 100644 --- a/src/test/utils/options/readers/data/file.ts +++ b/src/test/utils/options/readers/data/file.ts @@ -1,2 +1,2 @@ // This is referenced in valid.tsconfig.json -export const test = true +export const test = true; diff --git a/src/test/utils/options/readers/tsconfig.test.ts b/src/test/utils/options/readers/tsconfig.test.ts index 8a92a2f98..bd7f4a822 100644 --- a/src/test/utils/options/readers/tsconfig.test.ts +++ b/src/test/utils/options/readers/tsconfig.test.ts @@ -54,7 +54,7 @@ describe('Options - TSConfigReader', () => { 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(); @@ -62,5 +62,5 @@ describe('Options - TSConfigReader', () => { options.setValue('inputFiles', ['foo.ts']).unwrap(); options.read(new Logger()); equal(options.getValue('inputFiles'), ['foo.ts']); - }) + }); });