remark-lint-no-undefined-references
Advanced tools
Comparing version 4.1.1 to 4.2.0
@@ -6,3 +6,11 @@ export default remarkLintNoUndefinedReferences | ||
export type Options = { | ||
allow?: string[] | undefined | ||
allow?: | ||
| ( | ||
| string | ||
| RegExp | ||
| { | ||
source: string | ||
} | ||
)[] | ||
| undefined | ||
} | ||
@@ -14,13 +22,11 @@ export type Range = Array<number> | ||
| Options | ||
| import('unified-lint-rule').Label | ||
| import('unified-lint-rule').Severity | ||
| [ | ||
( | ||
| boolean | ||
| import('unified-lint-rule').Label | ||
| import('unified-lint-rule').Severity | ||
), | ||
(Options | undefined)? | ||
] | ||
| undefined | ||
] | ||
| [ | ||
( | ||
| boolean | ||
| import('unified-lint-rule').Label | ||
| import('unified-lint-rule').Severity | ||
), | ||
Options | undefined | ||
], | ||
@@ -27,0 +33,0 @@ import('mdast').Root, |
73
index.js
@@ -11,5 +11,8 @@ /** | ||
* * `Object` with the following fields: | ||
* * `allow` (`Array<string>`, default: `[]`) | ||
* — text that you want to allowed between `[` and `]` even though it’s | ||
* undefined | ||
* * `allow` (`Array<string | RegExp | { source: string }>`, | ||
* default: `[]`) | ||
* — text or regex that you want to be allowed between `[` and `]` | ||
* even though it’s undefined; regex is provided via a `RegExp` object | ||
* or via a `{ source: string }` object where `source` is the source | ||
* text of a case-insensitive regex | ||
* | ||
@@ -60,3 +63,3 @@ * ## Recommendation | ||
* @example | ||
* {"name": "ok-allow.md", "setting": {"allow": ["...", "…"]}} | ||
* {"name": "ok-allow.md", "config": {"allow": ["...", "…"]}} | ||
* | ||
@@ -66,2 +69,11 @@ * > Eliding a portion of a quoted passage […] is acceptable. | ||
* @example | ||
* {"name": "ok-allow.md", "config": {"allow": ["a", {"source": "^b\\."}]}} | ||
* | ||
* [foo][b.c] | ||
* | ||
* [bar][a] | ||
* | ||
* Matching is case-insensitive: [bar][B.C] | ||
* | ||
* @example | ||
* {"name": "not-ok.md", "label": "input"} | ||
@@ -99,2 +111,15 @@ * | ||
* 17:23-17:26: Found reference to undefined definition | ||
* | ||
* @example | ||
* {"name": "not-ok.md", "label": "input", "config": {"allow": ["a", {"source": "^b\\."}]}} | ||
* | ||
* [foo][a.c] | ||
* | ||
* [bar][b] | ||
* | ||
* @example | ||
* {"name": "not-ok.md", "label": "output", "config": {"allow": ["a", {"source": "^b\\."}]}} | ||
* | ||
* 1:1-1:11: Found reference to undefined definition | ||
* 3:1-3:9: Found reference to undefined definition | ||
*/ | ||
@@ -108,3 +133,3 @@ | ||
* @typedef Options | ||
* @property {Array<string>} [allow] | ||
* @property {Array<string | RegExp | { source: string }>} [allow] | ||
* | ||
@@ -131,8 +156,24 @@ * @typedef {Array<number>} Range | ||
const lineEnding = /(\r?\n|\r)[\t ]*(>[\t ]*)*/g | ||
const allow = new Set( | ||
(option.allow || []).map((d) => normalizeIdentifier(d)) | ||
) | ||
/** @type {Record<string, boolean>} */ | ||
const map = Object.create(null) | ||
const allow = option.allow || [] | ||
/** @type {Array<RegExp>} */ | ||
const regexes = [] | ||
/** @type {Set<string>} */ | ||
const strings = new Set() | ||
let index = -1 | ||
while (++index < allow.length) { | ||
const value = allow[index] | ||
if (typeof value === 'string') { | ||
strings.add(normalizeIdentifier(value)) | ||
} else if (value instanceof RegExp) { | ||
regexes.push(value) | ||
} else { | ||
regexes.push(new RegExp(value.source, 'i')) | ||
} | ||
} | ||
visit(tree, (node) => { | ||
@@ -157,3 +198,3 @@ if ( | ||
!(normalizeIdentifier(node.identifier) in map) && | ||
!allow.has(normalizeIdentifier(node.identifier)) | ||
!isAllowed(node.identifier) | ||
) { | ||
@@ -315,3 +356,3 @@ file.message('Found reference to undefined definition', node) | ||
!(normalizeIdentifier(id) in map) && | ||
!allow.has(normalizeIdentifier(id)) | ||
!isAllowed(id) | ||
) { | ||
@@ -322,2 +363,14 @@ file.message('Found reference to undefined definition', pos) | ||
} | ||
/** | ||
* @param {string} id | ||
* @returns {boolean} | ||
*/ | ||
function isAllowed(id) { | ||
const normalized = normalizeIdentifier(id) | ||
return ( | ||
strings.has(normalized) || | ||
regexes.some((regex) => regex.test(normalized)) | ||
) | ||
} | ||
} | ||
@@ -324,0 +377,0 @@ ) |
{ | ||
"name": "remark-lint-no-undefined-references", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"description": "remark-lint rule to warn when references to undefined definitions are found", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -57,13 +57,13 @@ <!--This file is generated--> | ||
In Deno with [Skypack][]: | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import remarkLintNoUndefinedReferences from 'https://cdn.skypack.dev/remark-lint-no-undefined-references@4?dts' | ||
import remarkLintNoUndefinedReferences from 'https://esm.sh/remark-lint-no-undefined-references@4' | ||
``` | ||
In browsers with [Skypack][]: | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import remarkLintNoUndefinedReferences from 'https://cdn.skypack.dev/remark-lint-no-undefined-references@4?min' | ||
import remarkLintNoUndefinedReferences from 'https://esm.sh/remark-lint-no-undefined-references@4?bundle' | ||
</script> | ||
@@ -129,5 +129,8 @@ ``` | ||
* `Object` with the following fields: | ||
* `allow` (`Array<string>`, default: `[]`) | ||
— text that you want to allowed between `[` and `]` even though it’s | ||
undefined | ||
* `allow` (`Array<string | RegExp | { source: string }>`, | ||
default: `[]`) | ||
— text or regex that you want to be allowed between `[` and `]` | ||
even though it’s undefined; regex is provided via a `RegExp` object | ||
or via a `{ source: string }` object where `source` is the source | ||
text of a case-insensitive regex | ||
@@ -232,2 +235,39 @@ ## Recommendation | ||
##### `ok-allow.md` | ||
When configured with `{ allow: [ 'a', { source: '^b\\.' } ] }`. | ||
###### In | ||
```markdown | ||
[foo][b.c] | ||
[bar][a] | ||
Matching is case-insensitive: [bar][B.C] | ||
``` | ||
###### Out | ||
No messages. | ||
##### `not-ok.md` | ||
When configured with `{ allow: [ 'a', { source: '^b\\.' } ] }`. | ||
###### In | ||
```markdown | ||
[foo][a.c] | ||
[bar][b] | ||
``` | ||
###### Out | ||
```text | ||
1:1-1:11: Found reference to undefined definition | ||
3:1-3:9: Found reference to undefined definition | ||
``` | ||
## Compatibility | ||
@@ -288,3 +328,3 @@ | ||
[skypack]: https://www.skypack.dev | ||
[esmsh]: https://esm.sh | ||
@@ -291,0 +331,0 @@ [npm]: https://docs.npmjs.com/cli/install |
20327
365
340