-
Notifications
You must be signed in to change notification settings - Fork 885
Mark inputFilePath as optional in findConfiguration() #3195
Mark inputFilePath as optional in findConfiguration() #3195
Conversation
Thanks for your interest in palantir/tslint, @lumaxis! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
aad0683
to
ab70d62
Compare
src/configuration.ts
Outdated
@@ -90,7 +90,7 @@ const BUILT_IN_CONFIG = /^tslint:(.*)$/; | |||
* of the search for a configuration. | |||
* @returns Load status for a TSLint configuration object | |||
*/ | |||
export function findConfiguration(configFile: string | null, inputFilePath: string): IConfigurationLoadResult { | |||
export function findConfiguration(configFile: string | null, inputFilePath?: string): IConfigurationLoadResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to prevent call like findConfiguration(null, undefined);
this function needs overloads. just add these lines above:
export function findConfiguration(configFile: string | null, inputFilePath: string): IConfigurationLoadResult;
export function findConfiguration(configFile: string, inputFilePath?: string): IConfigurationLoadResult
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! For some reason I didn't even think of overloads here 🙂
src/configuration.ts
Outdated
@@ -112,14 +112,14 @@ export function findConfiguration(configFile: string | null, inputFilePath: stri | |||
* @returns An absolute path to a tslint.json file | |||
* or undefined if neither can be found. | |||
*/ | |||
export function findConfigurationPath(suppliedConfigFilePath: string | null, inputFilePath: string) { | |||
export function findConfigurationPath(suppliedConfigFilePath: string | null, inputFilePath?: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ajafff I tried adding that here too but then I run into a strange compiler error that I can't really make sense of:
src/configuration.ts(95,58): error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
error Command failed with exit code 2.
From what I understand, this should work but the TS compiler is ignoring the other overload? Not sure. Any ideas? Thanks!
src/configuration.ts
Outdated
if (suppliedConfigFilePath != null) { | ||
if (!fs.existsSync(suppliedConfigFilePath)) { | ||
throw new FatalError(`Could not find config file at: ${path.resolve(suppliedConfigFilePath)}`); | ||
} else { | ||
return path.resolve(suppliedConfigFilePath); | ||
} | ||
} else { | ||
} else if (inputFilePath) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inputFilePath !== undefined
to satisfy the linter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. Sorry I missed this, thanks!
ab70d62
to
56d646d
Compare
src/configuration.ts
Outdated
@@ -90,7 +90,8 @@ const BUILT_IN_CONFIG = /^tslint:(.*)$/; | |||
* of the search for a configuration. | |||
* @returns Load status for a TSLint configuration object | |||
*/ | |||
export function findConfiguration(configFile: string | null, inputFilePath: string): IConfigurationLoadResult { | |||
export function findConfiguration(configFile: string | null, inputFilePath: string): IConfigurationLoadResult; | |||
export function findConfiguration(configFile: string, inputFilePath?: string): IConfigurationLoadResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You actually need 2 overloads and one implementation. Right now there is only 1 overload.
export function findConfiguration(configFile: string | null, inputFilePath: string): IConfigurationLoadResult;
export function findConfiguration(configFile: string, inputFilePath?: string): IConfigurationLoadResult;
export function findConfiguration(configFile: string | null, inputFilePath?: string): IConfigurationLoadResult {
...
}
src/configuration.ts
Outdated
@@ -90,7 +90,8 @@ const BUILT_IN_CONFIG = /^tslint:(.*)$/; | |||
* of the search for a configuration. | |||
* @returns Load status for a TSLint configuration object | |||
*/ | |||
export function findConfiguration(configFile: string | null, inputFilePath: string): IConfigurationLoadResult { | |||
export function findConfiguration(configFile: string | null, inputFilePath: string): IConfigurationLoadResult; | |||
export function findConfiguration(configFile: string, inputFilePath?: string): IConfigurationLoadResult { | |||
const configPath = findConfigurationPath(configFile, inputFilePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you will get a compile error on this line, because the parameter types do not match any overload.
You could work around it with this dirty hack:
findConfigurationPath(configFile, inputFilePath!); // note the ! after inputFilePath
Otherwise you need an if..else and still need to use a nonNullAssertion
src/configuration.ts
Outdated
if (suppliedConfigFilePath != null) { | ||
if (!fs.existsSync(suppliedConfigFilePath)) { | ||
throw new FatalError(`Could not find config file at: ${path.resolve(suppliedConfigFilePath)}`); | ||
} else { | ||
return path.resolve(suppliedConfigFilePath); | ||
} | ||
} else { | ||
} else if (inputFilePath !== undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, you don't need this condition. If suppliedConfigPath === null
, then inputFilePath
cannot be undefined. You can simply assert that using the nonNullAssertion operator inputFilePath!
in the else branch
When `suppliedConfigFilePath` is passed to `findConfigurationPath()`, `inputFilePath` is completely ignored and should therefore be optional
56d646d
to
633f47a
Compare
Thanks @lumaxis Note that github does not send notifications when you force push changes to your branch. I only noticed by chance. |
When `suppliedConfigFilePath` is passed to `findConfigurationPath()`, `inputFilePath` is completely ignored and should therefore be optional
PR checklist
Overview of change:
When
suppliedConfigFilePath
is passed tofindConfigurationPath()
,inputFilePath
is completely ignored and should therefore be optional.Is there anything you'd like reviewers to focus on?
CHANGELOG.md entry:
[bugfix] Correctly mark
inputFilePath
as an optional parameter inConfiguration.findConfiguration()