Skip to content

Commit

Permalink
feat(options): ignoreNextTo now supports an array of strings to ign…
Browse files Browse the repository at this point in the history
…ore (#178)
  • Loading branch information
wickedest authored Oct 12, 2021
1 parent ae70458 commit 5c52b47
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Example configuration:
{ no: "End-Of-Life", yes: "End-of-Life" },
{ no: "End-of-life", yes: "End-of-Life" },
{ no: 'gatsby', yes: "Gatsby", ignoreNextTo: "-" },
{ no: 'sales', yes: "Sales", ignoreNextTo: [ "-", "\'", "'" ] },
{ no: "Github", yes: "GitHub" },
{ no: "Javascript", yes: "JavaScript" },
{ no: "Node.JS", yes: "Node.js" },
Expand Down Expand Up @@ -38,10 +39,12 @@ Specifies what users will be told to use instead of the matched `no` value (if p

### ignoreNextTo

`string`, _optional_
`string` | `string []`, _optional_

Makes a prohibited string allowable if it appears next to that string. It is interpreted as a literal sequence of character(s) that appear immediately before or after the `yes` text. For example, in the configuration above, users will be told to use "Gatsby" instead of "gatsby". However, "gatsby-plugin" and "node-gatsby" will not be flagged because `'-'` is included in `ignoreNextTo` for that rule.

As an array of strings, the items are combined into a [regex OR condition](https://www.ocpsoft.org/tutorials/regular-expressions/or-in-regex/) to match a number of possible sequences of characters that might appear immediately before or after the `yes` text. In the configuration above, the linter will instruct the user to use "Sales" instead of "sales", but it will ignore "pre-sales", `'sales'`, and `"sales"` (within quotes).

### replaceCaptureGroups

`boolean`, _optional_
Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ function testProhibited (val, content) {
}

let regexpString = '(?<!\\.|@[a-zA-Z0-9/-]*)'

const ignoreNextTo = val.ignoreNextTo ? escapeStringRegexp(val.ignoreNextTo) : ''
let ignoreNextTo
if (val.ignoreNextTo) {
if (Array.isArray(val.ignoreNextTo)) {
const parts = val.ignoreNextTo.map(a => escapeStringRegexp(a)).join('|')
ignoreNextTo = `(?:${parts})`
} else {
ignoreNextTo = escapeStringRegexp(val.ignoreNextTo)
}
} else {
ignoreNextTo = ''
}
const replaceCaptureGroups = !!val.replaceCaptureGroups

// If it starts with a letter, make sure it is a word break.
Expand Down
92 changes: 92 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,98 @@ test('remark-lint-prohibited-strings', (t) => {
)
}

{
const value = 'The gatsby-specific way to do this is as follows:'
const ignoreNextTo = ['-']
t.deepEqual(
processorWithOptions([{ yes: 'Gatsby', no: 'gatsby', ignoreNextTo }])
.processSync(new VFile({ path, value }))
.messages.map(String),
[],
'should respect single occurence of one ignoreNextTo array item'
)
}

{
const value = 'word-gatsby gatsby-word word-gatsby-word'
const ignoreNextTo = ['-']
t.deepEqual(
processorWithOptions([{ yes: 'Gatsby', no: 'gatsby', ignoreNextTo }])
.processSync(new VFile({ path, value }))
.messages.map(String),
[],
'should respect multiple occurrences of a one ignoreNextTo array item'
)
}

{
const value = 'we are pro-rfc, see rfc-3986, rfc@3986'
const ignoreNextTo = ['-', '@']
t.deepEqual(
processorWithOptions([{ no: 'rfc', yes: 'RFC', ignoreNextTo }])
.processSync(new VFile({ path, value }))
.messages.map(String),
[],
'should respect multiple occurrences of multiple ignoreNextTo array items'
)
}

{
const value = 'use rfc.3986, "rfc", /rfc and rfc='
const ignoreNextTo = ['.', '"', '/']
t.deepEqual(
processorWithOptions([{ no: 'rfc', yes: 'RFC', ignoreNextTo }])
.processSync(new VFile({ path, value }))
.messages.map(String),
['fhqwhgads.md:1:31-1:34: Use "RFC" instead of "rfc"'],
'should respect multiple occurrences of multiple ignoreNextTo array items including regex'
)
}

{
const value = 'our pre-sales, post-sales, and sales-figures'
const ignoreNextTo = ['pre-', 'post-', '-figures']
t.deepEqual(
processorWithOptions([{ no: 'sales', yes: 'Sales', ignoreNextTo }])
.processSync(new VFile({ path, value }))
.messages.map(String),
[],
'should respect multiple occurrences of multiple ignoreNextTo array words'
)
}

{
const value = 'see rfc-3986'
const ignoreNextTo = ['"', '@', '#']
t.deepEqual(
processorWithOptions([{ no: 'rfc', yes: 'RFC', ignoreNextTo }])
.processSync(new VFile({ path, value }))
.messages.map(String),
['fhqwhgads.md:1:5-1:8: Use "RFC" instead of "rfc"'],
'should flag text that is not in ignoreNextTo items'
)
}

{
const value = 'see rfc 3986, rfc, rfc1234 but not rfc-folder or me@rfc.io'
const options = [{
yes: 'RFC-$1',
no: '[Rr][Ff][Cc]\\s*(\\d+)',
ignoreNextTo: ['-', '@'],
replaceCaptureGroups: true
}]
t.deepEqual(
processorWithOptions(options)
.processSync(new VFile({ path, value }))
.messages.map(String),
[
'fhqwhgads.md:1:5-1:13: Use "RFC-3986" instead of "rfc 3986"',
'fhqwhgads.md:1:20-1:27: Use "RFC-1234" instead of "rfc1234"'
],
'should flag text that is not in ignoreNextTo items and use capture groups'
)
}

{
const value = 'You got Sblounchsked!'
t.deepEqual(
Expand Down

0 comments on commit 5c52b47

Please sign in to comment.