Skip to content

Support capture groups in template #33

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
just-at-uber opened this issue Nov 24, 2020 · 4 comments
Open

Support capture groups in template #33

just-at-uber opened this issue Nov 24, 2020 · 4 comments

Comments

@just-at-uber
Copy link

just-at-uber commented Nov 24, 2020

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
Copy link
Author

just-at-uber commented Nov 25, 2020

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
Copy link

Hi @just-at-uber ,

I have extended the plugin to support an requeriment of mine, which is similar to yours. If the PR #53 gets merged, you can use the newly added option to fulfill your requirment too.

Cheers!

@jonathanbaker7
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants