-
Notifications
You must be signed in to change notification settings - Fork 885
Feat: ignore pattern for max-line-length #3099
Feat: ignore pattern for max-line-length #3099
Conversation
The schema used by vscode, webstorm etc. to validate your config comes from schemastore.org. It's updated by PR to their repository. |
src/rules/maxLineLengthRule.ts
Outdated
@@ -49,15 +56,18 @@ export class Rule extends Lint.Rules.AbstractRule { | |||
} | |||
|
|||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | |||
return this.applyWithFunction(sourceFile, walk, this.ruleArguments[0]); | |||
return this.applyWithFunction(sourceFile, walk, this.ruleArguments); |
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.
Parse the ruleArguments before passing it to applyWithFunction
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.
Do you mean something like this?
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const options = {
ignorePattern: this.ruleArguments[1] && new RegExp(this.ruleArguments[1]),
limit: parseInt(this.ruleArguments[0], 10),
};
return this.applyWithFunction(sourceFile, walk, options);
}
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.
Yeah, that's what I had in mind.
src/rules/maxLineLengthRule.ts
Outdated
optionsDescription: Lint.Utils.dedent`It can take up to 2 arguments, but only first one is required: | ||
|
||
* integer indicating the max length of lines. | ||
* string defining ignore pattern, being parsed by \`new RegExp()\`. |
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.
Consider to make this an object. That makes it easier to add new options in the future without requiring strict ordering of options.
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.
Do you mean object including the first number parameter this way?
"max-line-length": [true, {
limit: 120,
ignorePattern: "^import "
}],
Can it accept number or object (like number|object
) to avoid breaking change?
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.
It should accept number|object
. That's exactly what I meant.
Hey @ajafff, can you ensure I understood you correctly? |
@DanielKucal @ajafff Is this pull request still being looked at? I see there are some conflicts that need to be resolved before this can be merged. |
# Conflicts: # src/rules/maxLineLengthRule.ts # yarn.lock
would love to see this resolved |
Unfortunately I'm currently unable to make the request changes and resolve conflicts because of issues described in #3244... |
@DanielKucal Seems like #3244 was closed. How are things on your side? |
@sgiradoa-psl, am looking for some time to finish this |
src/rules/maxLineLengthRule.ts
Outdated
const lineRanges = getLineRanges(ctx.sourceFile); | ||
for (let i = 0; i < lines.length; i++) { | ||
if (ignorePattern && ignorePattern.test(lines[i])) { continue; } | ||
if (lineRanges[i].contentLength <= limit) { continue; } |
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.
consider moving this condition one line up to avoid regex matching if the line is not too long
src/rules/maxLineLengthRule.ts
Outdated
function walk(ctx: Lint.WalkContext<{limit: number; ignorePattern: RegExp | undefined}>) { | ||
const limit = ctx.options.limit; | ||
const ignorePattern = ctx.options.ignorePattern; | ||
const lines = (ctx.sourceFile && ctx.sourceFile.text.split("\n") || []); |
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.
please avoid this array by using ctx.sourceFile.text.substr(line.pos, line.contentLength)
in the loop below
src/rules/maxLineLengthRule.ts
Outdated
@@ -29,12 +28,41 @@ export class Rule extends Lint.Rules.AbstractRule { | |||
Limiting the length of a line of code improves code readability. | |||
It also makes comparing code side-by-side easier and improves compatibility with | |||
various editors, IDEs, and diff viewers.`, | |||
optionsDescription: "An integer indicating the max length of lines.", | |||
optionsDescription: Lint.Utils.dedent` | |||
It can take up to 2 arguments, but only the first one is required: |
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 description needs an update to the new options format
src/rules/maxLineLengthRule.ts
Outdated
return this.applyWithFunction(sourceFile, walk, this.ruleArguments[0] as number); | ||
const argument = this.ruleArguments[0]; | ||
const options = { | ||
ignorePattern: (typeof argument === "object") ? new RegExp(argument["ignore-pattern"]) : 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.
you also need to check if argument["ignore-pattern"]
is present because new RegExp(undefined)
matches everything
src/rules/maxLineLengthRule.ts
Outdated
const lines = (ctx.sourceFile && ctx.sourceFile.text.split("\n") || []); | ||
const lineRanges = getLineRanges(ctx.sourceFile); | ||
for (let i = 0; i < lines.length; i++) { | ||
if (ignorePattern && ignorePattern.test(lines[i])) { continue; } |
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.
I guess the linter complains about ignorePattern &&
please amend to ignorePattern !== undefined &&
Hey @ajafff, thanks for your comments, I've made the request changes. The Circle CI test seems to fail due to not found binary for |
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.
LGTM, I only found two style nits.
I just fixed the CI failures on master. This should be good to go after merging master into your branch.
src/rules/maxLineLengthRule.ts
Outdated
return super.isEnabled() && this.ruleArguments[0] as number > 0; | ||
const argument: any = this.ruleArguments[0]; | ||
const numberGreaterThan0: boolean = (Number(argument) > 0); | ||
const limitGreaterTHan0: boolean = (argument instanceof Object && argument.limit > 0); |
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.
s/THan/Than/
src/rules/maxLineLengthRule.ts
Outdated
@@ -45,19 +79,31 @@ export class Rule extends Lint.Rules.AbstractRule { | |||
} | |||
|
|||
public isEnabled(): boolean { | |||
return super.isEnabled() && this.ruleArguments[0] as number > 0; | |||
const argument: any = this.ruleArguments[0]; |
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 omit the : any
and : boolean
type annotations.
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.
Thought linter is complaining about infererred types
@DanielKucal almost done. There are some lint errors that need to be fixed. Use
|
…on, resolve errors about unsafe types
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.
still LGTM
Thanks @DanielKucal |
Can someone tell me when or how I can use this? I have the latest version of tslint and is not there. 😞 |
@sgiradoa-psl this change will be part of the next release |
@ajafff Will this be available anytime soon? I don't see this in |
@ajafff It might be useful to add this issue to the milestone in which you plan to release this feature. |
@MartinJohns it'll be in the next release, but we don't have strict release schedules right now. I know it's been a long time since the last release. I'm going through PRs now and will try to ship a minor release this week. |
The release schedule is not what I was pointing at, but just generally showing in what version the issue will ship. You could utilize the milestones feature and add the issues there, then it would quickly be visible what feature will ship with that version - just without a due date. For example: https://github.com/palantir/tslint/milestone/43?closed=1 - and I can see all issues that will ship with version 5.9. |
Sure, the milestone link would be nice to have, and we'll try to do it for some PRs / issues, but since it requires manual work it might not happen all the time. It should be easy enough for users to go to the project's root README and see the latest version on NPM and infer what the next release will be. IMO the best thing is to click through to the commit where a PR was merged: then check its tags to see where it was released: |
for people arriving here via google, two things:
{
"rules": {
"max-line-length": [true, {"limit": 120, "ignore-pattern": "your regex here"}],
}
}
{
"rules": {
"max-line-length": [true, {"limit": 120, "ignore-pattern": "^import [^,]+ from"}],
}
} |
Thanks @ekilah, I've updated first post to avoid confusing. |
PR checklist
Overview of change:
We can pass a regexp (as a string) to
max-line-length
rule to ignore some lines, e.g. long imports. Example usage:will ignore the following lines:
CHANGELOG.md entry:
[new-rule-option], [enhancement] allow to define ignore pattern for
max-line-length
rule