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

feat: make cli expect array for rules dir #3788

Merged

Conversation

ggarek
Copy link
Contributor

@ggarek ggarek commented Mar 22, 2018

PR checklist

Overview of change:

Is there anything you'd like reviewers to focus on?

It seems like the codebase is ready for both string and string[] for rulesDirectory, hence the change is just a cli option creation. (I have traced it back to Linter > getEnabledRules > arrayify)

⚠️ It seems, also, that there are no tests for this use case, at least i haven't found one.

✅ The current test suit passes.

CHANGELOG.md entry:

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @ggarek! 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.

@ggarek
Copy link
Contributor Author

ggarek commented Mar 22, 2018

It seems the reason why CI fails is

yarn add typescript@next

TS (typescript@2.8.0-dev.20180321) now reports more errors 😉

@@ -121,7 +121,7 @@ const options: Option[] = [
{
short: "r",
name: "rules-dir",
type: "string",
type: "array",
Copy link

@giladgray giladgray May 1, 2018

Choose a reason for hiding this comment

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

@ggarek isn't it string | string[] (per PR description)? can we support both types in the schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@giladgray
Not sure i've understood correctly, but

These are cli option definitions. The type property is handled by the cli code basically adding collect coercion to commander. (and we can not actually use string | string[] here, these are typescript types, but we have just value of type type: "string" | "boolean" | "array";)

It seems like the codebase is ready for both string and string[] for rulesDirectory, hence the change is just a cli option creation. (I have traced it back to Linter > getEnabledRules > arrayify)

Considering this, CLI should be ready to handle both string and string[] values.

@mayankku
Copy link

Any updates on this?

@giladgray
Copy link

@ggarek there is a conflict with master, we just renamed the file to tslintCli.ts in #3978.

to confirm, how would one pass an array for this CLI option? I couldn't find specifics on the commander docs. is it like tslint -r one -r two -r three?

@mayankku
Copy link

@giladgray this PR seems to be stuck. Is there any way to get this merged?

Re your question: you're right about passing repeated args. It seems a collect function is provided to Commander to push args into an array:

function collect(val: string, memo: string[]) {

@johnwiseheart
Copy link
Contributor

@ggarek can you please fix the merge conflict and then we can merge this in?

@ggarek
Copy link
Contributor Author

ggarek commented Sep 27, 2018

Repeated the patch on the latests master.

⚠️ I have change one line of code only, but the PR has many changes due to code formatting on pre-commit hook. It has happened probably, because there were no changes to the file after code formatting was introduced. Please, let me know if you want me to commit the patch in any other specific way.

Here is a check, i have made locally, to confirm if the cli works (both with rules directories defined in configuration file and passed via cli options)

the test project

➜ tree test-rule-dirs-array
test-rule-dirs-array
├── custom-rules
│   ├── 1
│   │   ├── oneTwoRule.d.ts
│   │   ├── oneTwoRule.js
│   │   └── oneTwoRule.ts
│   ├── 2
│   │   ├── threeFourRule.d.ts
│   │   ├── threeFourRule.js
│   │   └── threeFourRule.ts
│   └── tsconfig.json
├── index.ts
├── tsconfig.json
└── tslint.json

the index.ts

import t from 'typescript';

const a: number = 1;

tslint.json

{
    "extends": [],
    "x_rulesDirectory": ["./custom-rules/1", "./custom-rules/2"],
    "rules": {
        "one-two": true,
        "three-four": true
    }
}

test-case 1: pass two rule dirs

➜ ../bin/tslint -r ./custom-rules/1 -r ./custom-rules/2 index.ts

ERROR: index.ts[1, 1]: import statement forbidden
ERROR: index.ts[1, 8]: 'short identifier names are forbidden
ERROR: index.ts[3, 7]: 'short identifier names are forbidden

tests-case 2: pass only one rule dir

➜ ../bin/tslint -r ./custom-rules/1 index.ts

Could not find implementations for the following rules specified in the configuration:
    three-four
Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.
If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.


ERROR: index.ts[1, 1]: import statement forbidden

test-case 3: do not pass rule dirs

➜ ../bin/tslint index.ts

Could not find implementations for the following rules specified in the configuration:
    one-two
    three-four
Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.
If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.

No valid rules have been specified for TypeScript files

test-case 4: defined rule dirs in configuration (tslint.json is changed)

tslint.json

{
    "extends": [],
    "rulesDirectory": ["./custom-rules/1", "./custom-rules/2"],
    "rules": {
        "one-two": true,
        "three-four": true
    }
}

test case

➜ ../bin/tslint index.ts

ERROR: index.ts[1, 1]: import statement forbidden
ERROR: index.ts[1, 8]: 'short identifier names are forbidden
ERROR: index.ts[3, 7]: 'short identifier names are forbidden

@johnwiseheart @giladgray

I would be glad to have this fix merged, plz let me know if i have to do anything else to make it happen 🙏

@JoshuaKGoldberg
Copy link
Contributor

Hey @ggarek, sorry for the delay! You're right that this is a good, simple fix plus a bunch of auto-added formatting. We can ignore that - the problem is external to this PR. #4229

@JoshuaKGoldberg JoshuaKGoldberg merged commit 1e9ed49 into palantir:master Nov 5, 2018
@ggarek ggarek deleted the feature/cli-rules-dir-array branch January 21, 2019 23:08
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.

6 participants