Skip to content

Support capture groups in template #33

@just-at-uber

Description

@just-at-uber

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions