Skip to content
Merged
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
39 changes: 14 additions & 25 deletions src/legacy/ui/public/vis/editors/config/editor_config_providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class EditorConfigProviderRegistry {
current: EditorParamConfig,
merged: EditorParamConfig,
paramName: string
): { fixedValue?: any; base?: number } {
): { fixedValue: unknown } | { base: number } | {} {
Copy link
Contributor Author

@timroes timroes Jul 3, 2019

Choose a reason for hiding this comment

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

ℹ️ We know this method is returning either of those, so we use a union type here instead, which makes more sense.

if (
this.isFixedParam(current) &&
this.isFixedParam(merged) &&
Expand Down Expand Up @@ -128,37 +128,26 @@ class EditorConfigProviderRegistry {
current: TimeIntervalParam,
merged: EditorParamConfig,
paramName: string
): { timeBase?: string; default?: string } {
): { timeBase: string; default: string } {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ℹ️ The problem here was, that the method could from it's types return an object without default, that would then legibly fail (in newer TS versions) later on, because TS detects correctly, that a TimeIntervalParam needs this, so this merge method must return it. In fact it also does, if we remove the last if check for isTimeBaseParam(current) which is anyway redundant, since we know that current is a TimeIntervalParam (it's the type of the parameter).

if (current.default !== current.timeBase) {
throw new Error(`Tried to provide differing default and timeBase values for ${paramName}.`);
}

if (this.isTimeBaseParam(current) && this.isTimeBaseParam(merged)) {
if (this.isTimeBaseParam(merged)) {
// In case both had where interval values, just use the least common multiple between both intervals
try {
const timeBase = leastCommonInterval(current.timeBase, merged.timeBase);
return {
default: timeBase,
timeBase,
};
} catch (e) {
throw e;
}
}

if (this.isTimeBaseParam(current)) {
try {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ℹ️ I removed the redundant try-catch blocks, that were just rethrowing the caught error.

parseEsInterval(current.timeBase);
return {
default: current.timeBase,
timeBase: current.timeBase,
};
} catch (e) {
throw e;
}
const timeBase = leastCommonInterval(current.timeBase, merged.timeBase);
return {
default: timeBase,
timeBase,
};
}

return {};
// This code is simply here to throw an error in case the `timeBase` is not a valid ES interval
parseEsInterval(current.timeBase);
return {
default: current.timeBase,
timeBase: current.timeBase,
};
}

private mergeConfigs(configs: EditorConfig[]): EditorConfig {
Expand Down