-
Notifications
You must be signed in to change notification settings - Fork 885
Add exclude section to configuration files. #2409
Conversation
This allows users to define default cli options in tslint.json. e.g. if you want to exclude the "bin" directory from linting, you could add this to your tslint.json: "cliOptions": { "exclude": "bin" }
Thanks for your interest in palantir/tslint, @mmkal! 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. |
Cool, overall this seems reasonable given all the user feedback around CLI options (mainly summed up in #2227). It does represent a change of philosophy in what tslint.json does. Previously, the configuration file could only specify how to lint, but now it has the ability to specify what to lint as well. See #73 (comment) and #629 (comment). What do you think @jkillian @nchen63 @ajafff @andy-hanson @andy-ms @mgechev @ChrisMBarr? Any objections? |
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.
Now for PR feedback:
- I don't think this exactly solves Feature: ".tslintignore" file to exclude globs #73 because
tslint
doesn't look for additional tslint.json files after it has found a relevant one it can use to lint. I believe the request for a.tslintignore
file was made so that users could have multiple such ignore files across their codebase and a single tslint.json (I'm not 100% sure though, you should go back to that thread and confirm). - we should name the top-level config key
"linterOptions"
, like this private API we use for testing. - there are many more CLI options, not just
exclude
. we need tests for those too. in particular we need to test things like path resolution rulesDirectory
is currently handled specially as a top-level tslint.json key. It should probably move intolinterOptions
to be consistent (yeah it's a breaking change but we're on the 5.0 track now so we can do that).
I'm ok with specifying what to lint and making all CLI options available in tslint.json. There are some questions that need to be resolved:
|
not all options are configurable via the CLI. for example,
yeah, I would accept that as a PR if it had the same semantics as
hmm, not sure about this one. In general, we don't expect base/intermediate configurations (those you are extending) to use
I think they should override the config file's options (this is how most CLIs work) |
I didn't go the
Sure, will push an update soon.
I'm unsure what you mean by this. In terms of testing the code I'm adding, I'm almost exclusively relying on existing functionality. In terms of implementation, I'm loading the configuration object using I didn't find any existing tests of
Sure - should I deprecate the existing
I wasn't sure either. But I had a vision of working on a team project and extending an existing
Yeah, I was basing this on how |
Haven't reviewed the actual code changes, but I'm fine w/ this in principle if it makes things easier for users. There are some questions about how this affects integration with other tools though: In the past it was up to the integrating tool to decide what files to lint. If you were using an IDE, your IDE would have to tell TSLint which files to lint. If you're using webpack, this would be controlled by your webpack rule/loader In this PR, presumably the proposed |
I agree, this is better than
Yeah, never mind the path resolution thing; I just think there should just be a test that verifies that CLI options can override
Yeah, let's deprecate it with a
Cool, this makes sense to implement now and wait for feedback if the behavior should be changed later |
Yes, I would actually expect IDEs and webpack loaders to read these config options (eventually). With |
@mmkal ah, actually we do have CLI tests, they're in test/executable/executableTests.ts |
Good point and agreed, |
@adidahiya just to give an update on this... I haven't had a lot of time to look at this since we last spoke, but I managed to look at it a little over the weekend. Moving cliOptions to linterOptions is a little more complicated than I'd originally thought. There are a couple of design decisions to consider, like
Camel casing is more consistent with the rest of tslint.json, but it increases the complexity because the cli options are hyphenated. The deprecation of rulesDir and linterOptions.typeCheck is also a little unclear. Presumably I need to support the merging of rules directories from parent configs? If so, how do I reconcile this with the decision above, to overwrite rather than replace properties? Different behavior for different properties? And since you've suggested I deprecate rulesDir, this would mean changing it in a lot of different files since it wouldn't be good to have tslint itself using a deprecated feature. Another problem I've seen is that I can't actually seem to make the executableTests run. npm test and npm run verify seem to ignore them. Taking all this into account, my solution is no longer looking like a cheap win. As I don't know the codebase well, I would rather not be making big sweeping changes. So I can make three suggestions for what I can do next:
I'd be ok with any one of those three - or any other suggestion you might have. As long as they allow me to only make small code changes - otherwise I won't realistically be able to get them done. |
@mmkal sorry for the delay. Let's just go with the easiest approach, (3), limiting the scope of the work in this PR. |
Looking at the project file in the root wasn't sufficient; we need to check if there's an exclude value every time we read a project file. Also make exclude always an array, not a string.
@adidahiya updated - sorry it took so long, I ended up finding a workaround for the project I needed this for, and I didn't have a lot of time to look at this outside of work. You were right about using __filename not making sense. I moved the check for whether a file is excluded to just before it's linted, since the config for that file is being loaded there already. |
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.
Works for me
whoops, commented from my work account. Your changes look good to me @adidahiya, thanks! |
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.
Looks good.
Solving the relative vs absolute path issue when matching the glob pattern can be done in a followup.
src/configuration.ts
Outdated
typeCheck?: boolean; | ||
}; | ||
linterOptions?: Partial<{ | ||
typeCheck: boolean; |
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 can remove the typeCheck
. It's no longer used by the tests
return false; | ||
} | ||
const fullPath = path.resolve(filepath); | ||
return configFile.linterOptions.exclude.some((pattern) => new Minimatch(pattern).match(fullPath)); |
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.
The excluded paths should probably be resolved relative to the location of tslint.json
.
If /foo/tslint.json
excludes baz.ts
we should only exclude /foo/baz.ts
and still lint /foo/bar/baz.ts
.
That will only work when filepath
is an absolute path.
}; | ||
|
||
const actualConfig = extendConfigurationFile(baseConfig, extendingConfig); | ||
assertConfigEquals(actualConfig, expectedConfig); |
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.
out of curiosity: what happens if extendingConfig
contains an empty object as "linterOptions"
? Would that override the whole object including "exclude"
?
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.
No, only if exclude
is specified. I added a test for this below.
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 added a test for an extending config with empty linterOptions
, and got rid of typeCheck
.
}; | ||
|
||
const actualConfig = extendConfigurationFile(baseConfig, extendingConfig); | ||
assertConfigEquals(actualConfig, expectedConfig); |
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.
No, only if exclude
is specified. I added a test for this below.
Will this be in 5.8? |
@KSuttle-cng Yes, this will be part of the next release |
Cool, any timeline for this? I've been trying to get the CLI to respect the |
There's no release schedule. Maybe I get around to it during the next week. |
Awesome. Thanks @ajafff. |
Per discussion, we want to be able to keep the samples simple so don't want to force use of const there. Currently, putting `tslint:disable` in all files appears to be the best way to accomplish this. Hopefully once palantir/tslint#2409 lands in the next tslint release we can move over to juts excluding our samples folder from linting.
This allows users to define default cli options in tslint.json.
PR checklist
Overview of change:
Adds
cliOptions
object toIConfigurationFile
interface insrc/configuration.ts
.Modifies
src/cli.ts
to read from the configuration file (passed in via cli or defaulttslint.json
) and useconfiguration.cliOptions
as default values for the command line options.This would, for example, allow exluding files from linting (the topic of the issue) like this:
suggested tags: [enhancement]