Skip to content

Add support to set a year or year range dynamically from the year range extracts from the previous header #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,43 @@ The rule works with both unix and windows line endings. For ESLint `--fix`, the
```
Possible values are `unix` for `\n` and `windows` for `\r\n` line endings.

### Dynamically manipulate the year in the template

When replacing the header, year can be dynamically manipulate and include using these options
```js

// eslintrc.js

"rules": {
"header/header": [
2,
"block",
[{ "pattern": " Copyright \\d{4}", "template": " Copyright {{year}}" }, "My Company"],
{
"templateOptions": {
"forceEndYear": false,
"endYear": 2022,
"endYearPersist": true,
"startYear": new Date().getFullYear(),
"startYearPersist": true,
"yearRange": true,
"yearRangeValidations": true
}
}
]
}
```

| Config | Value Type | Description |
|-|-|-|
| forceEndYear | Boolean | `yearRange` has to be `true`. Will add "-YYYY" if the `endYear` is not equal to `startYear`. But if start year and current year is same it will convert to "YYYY" format |
| endYear | Number | `yearRange` has to be `true`. Will ignore if the `endYearPersist` is `true`. |
| endYearPersist | Boolean | `yearRange` has to be `true`. Will extract end year from the previous header and replace in the new header. |
| startYear | Number| Will ignore if the `startYearPersist` is `true`. E.g., new Date().getFullYear() |
| startYearPersist | Boolean | Will extract start year from the previous header and replace in the new header |
| yearRange | Boolean | If `true` year format will be "YYYY-YYYY" and if `false` year format will be "YYYY" |
| yearRangeValidations | Boolean | If `true` end date will be removed if it lower than start date. `yearRange` has to be `true` |

## Examples

The following examples are all valid.
Expand Down
76 changes: 76 additions & 0 deletions lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,82 @@ module.exports = {
if (canFix && headerLines.length > 1) {
fixLines = [fixLines.join(eol)];
}

var yearTemplateLiteralPattern = /\{\{year\}\}/;
var yearTemplateLiteral = (fixLines.length > 0) ?
fixLines[0].match(yearTemplateLiteralPattern) : null;

if (yearTemplateLiteral) {

var yearRangePattern = /\b(\d{4})(?:-(\d{4}))?(?!\d)/;
var incorrectHeader = leadingComments[0].value;
var previousHeaderYear = incorrectHeader.match(yearRangePattern);
var settings = findSettings(options);

if (settings && settings.templateOptions) {

var year = "";
var startYearPattern = /^.{4}/;
var endYearPattern = /.{4}$/;

// Update start year based on the configs
if (previousHeaderYear && settings.templateOptions.startYearPersist) {

year += previousHeaderYear[1];
}

if (previousHeaderYear && !settings.templateOptions.startYearPersist &&
settings.templateOptions.startYear) {

year = year.replace(startYearPattern, settings.templateOptions.startYear);
}

// Update end year based on the configs
if (previousHeaderYear && previousHeaderYear[2] &&
settings.templateOptions.endYearPersist &&
settings.templateOptions.yearRange) {

year += "-" + previousHeaderYear[2];
}

if (previousHeaderYear && !settings.templateOptions.endYearPersist &&
settings.templateOptions.endYear &&
settings.templateOptions.yearRange) {

year = year.replace(endYearPattern, settings.templateOptions.endYear);
}

if (year !== "") {

var updatedYear = year.match(yearRangePattern);
var updatedYearHasEndYear = updatedYear[2];

// Add end year if end year is missing and the config is enabled
if (settings.templateOptions.forceEndYear &&
settings.templateOptions.yearRange &&
settings.templateOptions.endYear) {

if (!updatedYearHasEndYear) {
year += "-" + settings.templateOptions.endYear;
}
}

// Validate end year if the option is enabled
if (settings.templateOptions.yearRangeValidations) {

if (updatedYearHasEndYear) {

if (updatedYear[1] >= updatedYear[2]) {
year = year.replace(/.{5}$/, "");
}
}
}

fixLines = [fixLines[0].replace(yearTemplateLiteralPattern, year)];
}
}
}

context.report({
loc: node.loc,
message: "incorrect header",
Expand Down
Loading