Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Mark inputFilePath as optional in findConfiguration() #3195

Merged

Conversation

lumaxis
Copy link
Contributor

@lumaxis lumaxis commented Sep 2, 2017

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

Overview of change:

When suppliedConfigFilePath is passed to findConfigurationPath(), 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 in Configuration.findConfiguration()

@palantirtech
Copy link
Member

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.

@lumaxis lumaxis force-pushed the findConfiguration-optional-parameter branch from aad0683 to ab70d62 Compare September 2, 2017 00:59
@@ -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 {
Copy link
Contributor

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

Copy link
Contributor Author

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 🙂

@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

Copy link
Contributor Author

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!

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) {
Copy link
Contributor

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

Copy link
Contributor Author

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!

@lumaxis lumaxis force-pushed the findConfiguration-optional-parameter branch from ab70d62 to 56d646d Compare September 3, 2017 21:34
@@ -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 {
Copy link
Contributor

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 {
   ...
}

@@ -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);
Copy link
Contributor

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

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) {
Copy link
Contributor

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
@lumaxis lumaxis force-pushed the findConfiguration-optional-parameter branch from 56d646d to 633f47a Compare September 4, 2017 21:11
@ajafff ajafff merged commit 1da80df into palantir:master Sep 12, 2017
@ajafff
Copy link
Contributor

ajafff commented Sep 12, 2017

Thanks @lumaxis

Note that github does not send notifications when you force push changes to your branch. I only noticed by chance.

HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
When `suppliedConfigFilePath` is passed to `findConfigurationPath()`, `inputFilePath` is completely ignored and should therefore be optional
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants