Skip to content

feat(readers/tsconfig.ts): support "extends" #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
6 changes: 6 additions & 0 deletions src/lib/utils/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class DiscoverEvent extends Event {

mode: OptionsReadMode;

compilerOptions?: CompileOptions;

inputFiles: string[] = [];

errors: string[] = [];
Expand Down Expand Up @@ -72,6 +74,10 @@ export class Options extends ChildableComponent<Application, OptionsComponent> {
this.trigger(event);
this.setValues(event.data, '', event.addError.bind(event));

if (event.compilerOptions) {
_.assign(this.compilerOptions, event.compilerOptions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use event.data for everything instead of having two ways to pass this data?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be used, but currently event.data is more like event.rawOptions so whatever additional data is added it should not be in conflict with any of the options and should be deleted before parsing them.
If the following is OK I'll make the changes:

  1. in readers/tsconfig.ts - add parsedCompilerOptions: CompilerOptions to the event.data
  2. in options/options.ts - delete parsedCompilerOptions from event.data before parsing

}

if (mode === OptionsReadMode.Fetch) {
const logger = this.application.logger;
for (let error of event.errors) {
Expand Down
30 changes: 22 additions & 8 deletions src/lib/utils/options/readers/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,45 @@ export class TSConfigReader extends OptionsComponent {
return;
}

let data = ts.readConfigFile(fileName, ts.sys.readFile).config;
const data = ts.readConfigFile(fileName, ts.sys.readFile).config;

if (data === undefined) {
event.addError('The tsconfig file %s does not contain valid JSON.', fileName);
return;
}

if (!_.isPlainObject(data)) {
event.addError('The tsconfig file %s does not contain a JSON object.', fileName);
return;
}

data = ts.parseJsonConfigFileContent(
let {options, fileNames} = ts.parseJsonConfigFileContent(
data,
ts.sys,
Path.resolve(Path.dirname(fileName)),
{},
Path.resolve(fileName));

event.inputFiles = data.fileNames;
event.inputFiles = fileNames;
options = _.clone(options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we care about cloning the options here? The config parse function should be generating a plain object that wouldn't be used anywhere else anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also point out where extends is now handled? I'm not sure I'm reading this snippet correctly, but it seems to move things around but I can't tell what/where something new was introduced.

Copy link
Author

@StrahilKazlachev StrahilKazlachev Mar 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extends is handled by ts.parseJsonConfigFileContent(). The rest of the changes here are for preserving the precedence of the options(cli, ..., tsconfig.compilerOptions) and clearing the unsupported options.


if (!data.typedocOptions) {
data.typedocOptions = Object.create(null);
}

for (const key of TypeScriptSource.IGNORED) {
delete options[key];
delete data.typedocOptions[key];
}

_.defaults(event.data, data.typedocOptions);

const ignored = TypeScriptSource.IGNORED;
let compilerOptions = _.clone(data.raw.compilerOptions);
for (const key of ignored) {
delete compilerOptions[key];
for (const key in options) {
if (!_.isUndefined(event.data[key])) {
delete options[key];
}
}

_.defaults(event.data, data.raw.typedocOptions, compilerOptions);
event.compilerOptions = options;
}
}