eslint-plugin-header icon indicating copy to clipboard operation
eslint-plugin-header copied to clipboard

Support capture groups in template

Open just-at-uber opened this issue 5 years ago • 4 comments

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 avatar Nov 24 '20 22:11 just-at-uber

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.

just-at-uber avatar Nov 25 '20 20:11 just-at-uber

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!

jeradrutnam avatar May 17 '23 05:05 jeradrutnam

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

jonathanbaker7 avatar May 17 '23 13:05 jonathanbaker7

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

jeradrutnam avatar Jun 02 '23 07:06 jeradrutnam