Stuk / eslint-plugin-header

ESLint plugin to ensure that files begin with given comment
MIT License
71 stars 36 forks source link

Support capture groups in template #33

Open just-at-uber opened 3 years ago

just-at-uber commented 3 years ago

Right now this library can detect if a given header is in violation of the rules specified in a pattern. If the pattern is partially matched, it would be nice to be able to reference capture groups in the pattern and apply those capture groups into the template (if matched), otherwise leave blank.

For example: Say my header & configuration looked like the following:

Actual behavior

header:

// Copyright 2017-2020 Uber Technologies Inc.

rule:

const currentYear = (new Date()).getFullYear(); // = '2020'
...
{
    pattern: ` Copyright (20\\d{2}\\-)?${currentYear} Uber Technologies Inc\.`,
    template: ` Copyright ${currentYear} Uber Technologies Inc.`,
}

and time ticked over to 2021

header (fixed):

// Copyright 2021 Uber Technologies Inc.

Desired behavior

header:

// Copyright 2017-2020 Uber Technologies Inc.

rule:

const currentYear = (new Date()).getFullYear(); // = '2020'
...
{
    pattern: ` Copyright (?<firstYear>20\\d{2}\\-)?${currentYear} Uber Technologies Inc\.`,
    template: ` Copyright (\\k<firstYear>)${currentYear} Uber Technologies Inc.`,
}

When the year ticks over to 2021, it would be nice if it updated current year only, and left first year as-is.

header (fixed):

// Copyright 2017-2021 Uber Technologies Inc.
just-at-uber commented 3 years ago

Another option instead of supporting capture groups is to give simple const primitives to us, such as:

const CURRENT_FULL_YEAR = 2021;
const START_FULL_YEAR = 2017; // can be passed in and configured by user.

then rule becomes:

{
    startFullYear: '2017',
    pattern: ` Copyright ({START_FULL_YEAR}\\-)?{CURRENT_FULL_YEAR} Uber Technologies Inc\.`,
    template: ` Copyright ({START_FULL_YEAR}\\-)?{CURRENT_FULL_YEAR} Uber Technologies Inc.`,
}

then header can support (when year ends):

// Copyright 2017-2021 Uber Technologies Inc.
// Copyright 2021 Uber Technologies Inc.
jeradrutnam commented 1 year ago

Hi @just-at-uber ,

I have extended the plugin to support an requeriment of mine, which is similar to yours. If the PR https://github.com/Stuk/eslint-plugin-header/pull/53 gets merged, you can use the newly added option to fulfill your requirment too.

Cheers!

jonathanbaker7 commented 1 year ago

Hi. Just FYI, @just-at-uber has left Uber and isn't responding to emails anymore. This is the OSPO, where things got redirected.

jeradrutnam commented 1 year ago

Hi @jonathanbaker7, Noted. Thanks for informing. :)