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

[new-fixer] file-header rule #3475

Merged
merged 8 commits into from
Nov 27, 2017
Merged

[new-fixer] file-header rule #3475

merged 8 commits into from
Nov 27, 2017

Conversation

jkillian
Copy link
Contributor

@jkillian jkillian commented Nov 10, 2017

PR checklist

  • New feature, bugfix, or enhancement
    • Includes tests

Overview of change:

Adds a fixer to file-header. The rule will now insert text from a new second option as the header of your file if that option is provided and --fix is enabled.

If the rule finds a header comment that's incorrect, it's replaced with the specified header. If the rule doesn't find a header comment at all, the specified header is inserted at the top of the document.

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

Is the newline handling legit?

CHANGELOG.md entry:

[new-fixer] file-header

const { pos, end, kind } = commentDetails;
const commentText = text.substring(pos, kind === ts.SyntaxKind.SingleLineCommentTrivia ? end : end - 2);
if (!headerFormat.test(commentText)) {
const isFirstCommentAHeader = ONLY_WHITESPACE.test(text.substring(offset, pos));
Copy link
Contributor

Choose a reason for hiding this comment

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

the substring could contain line breaks.The regexp probably needs the m flag to make this work as expected.

const isFirstCommentAHeader = ONLY_WHITESPACE.test(text.substring(offset, pos));
const fix = textToInsert !== undefined
? isFirstCommentAHeader
? Lint.Replacement.replaceFromTo(pos, end, this.createComment(sourceFile, textToInsert, 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is removing the existing comment really expected? It could contain JSDoc or something similar useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, it's a tricky call. If it's a malformed header, you'd want it to be removed, otherwise you probably wouldn't want it to be. I don't think there's any way to automatically determine which is the case, so we'll want to just pick the option that is most correct in most cases.

I think I agree with your thoughts that replacing the header might be unexpected more often than desired. (Plus it'll be simpler code w/o that feature.) I guess we could make the behavior configurable in the future if desired

const lineEnding = `${maybeCarriageReturn}\n`;
return [
"/*",
...commentText.split(lineEnding).map((line) => ` * ${line}`),
Copy link
Contributor

Choose a reason for hiding this comment

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

consider splitting at /\r?\n/g. That allows users to only use \n in their config while using \r\n in source files.

@jkillian
Copy link
Contributor Author

Updated to no longer replace the first comment it finds. I also modified the insertion code a little bit to handle insert newlines in (hopefully) the most expected manner.

@adidahiya adidahiya merged commit 497c3e1 into master Nov 27, 2017
@adidahiya adidahiya deleted the file-header-fixer branch November 27, 2017 18:22
@emanuelpinho
Copy link

emanuelpinho commented Nov 28, 2017

@adidahiya , are you expecting to include this in the next version? Do you already have a release date for that?

HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
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.

4 participants