Skip to content

Commit

Permalink
Use micromark-util-normalize-identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 11, 2021
1 parent b81b38a commit 27a96be
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 32 deletions.
24 changes: 9 additions & 15 deletions packages/remark-lint-no-undefined-references/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* 17:23-17:26: Found reference to undefined definition
*/

import {collapseWhiteSpace} from 'collapse-white-space'
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
import {location} from 'vfile-location'
import {lintRule} from 'unified-lint-rule'
import {generated} from 'unist-util-generated'
Expand All @@ -75,15 +75,17 @@ const remarkLintNoUndefinedReferences = lintRule(
const contents = String(file)
const loc = location(file)
const lineEnding = /(\r?\n|\r)[\t ]*(>[\t ]*)*/g
const allow = new Set((option.allow || []).map((d) => normalize(d)))
const allow = new Set(
(option.allow || []).map((d) => normalizeIdentifier(d))
)
const map = {}

visit(tree, (node) => {
if (
(node.type === 'definition' || node.type === 'footnoteDefinition') &&
!generated(node)
) {
map[normalize(node.identifier)] = true
map[normalizeIdentifier(node.identifier)] = true
}
})

Expand All @@ -96,8 +98,8 @@ const remarkLintNoUndefinedReferences = lintRule(
node.type === 'linkReference' ||
node.type === 'footnoteReference') &&
!generated(node) &&
!(normalize(node.identifier) in map) &&
!allow.has(normalize(node.identifier))
!(normalizeIdentifier(node.identifier) in map) &&
!allow.has(normalizeIdentifier(node.identifier))
) {
file.message('Found reference to undefined definition', node)
}
Expand Down Expand Up @@ -233,8 +235,8 @@ const remarkLintNoUndefinedReferences = lintRule(

if (
!generated({position: pos}) &&
!(normalize(id) in map) &&
!allow.has(normalize(id))
!(normalizeIdentifier(id) in map) &&
!allow.has(normalizeIdentifier(id))
) {
file.message('Found reference to undefined definition', pos)
}
Expand All @@ -244,11 +246,3 @@ const remarkLintNoUndefinedReferences = lintRule(
)

export default remarkLintNoUndefinedReferences

// The identifier is upcased to avoid naming collisions with fields inherited
// from `Object.prototype`.
// If `Object.create(null)` was used in place of `{}`, downcasing would work
// equally well.
function normalize(s) {
return collapseWhiteSpace(s.toUpperCase())
}
2 changes: 1 addition & 1 deletion packages/remark-lint-no-undefined-references/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"index.js"
],
"dependencies": {
"collapse-white-space": "^2.0.0",
"micromark-util-normalize-identifier": "^1.0.0",
"unified-lint-rule": "^1.0.0",
"unist-util-generated": "^2.0.0",
"unist-util-position": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import {lintRule} from 'unified-lint-rule'
import {visit} from 'unist-util-visit'
import {generated} from 'unist-util-generated'
import {collapseWhiteSpace} from 'collapse-white-space'
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'

const remarkLintNoUnneededFullReferenceImage = lintRule(
'remark-lint:no-unneeded-full-reference-image',
Expand All @@ -49,7 +49,7 @@ const remarkLintNoUnneededFullReferenceImage = lintRule(
if (
generated(node) ||
node.referenceType !== 'full' ||
normalize(node.alt) !== node.identifier
normalizeIdentifier(node.alt) !== node.identifier.toUpperCase()
) {
return
}
Expand All @@ -63,8 +63,3 @@ const remarkLintNoUnneededFullReferenceImage = lintRule(
)

export default remarkLintNoUnneededFullReferenceImage

// See: <https://github.com/remarkjs/remark/blob/cc7867b/packages/remark-parse/lib/util/normalize.js>
function normalize(value) {
return collapseWhiteSpace(value).toLowerCase()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"index.js"
],
"dependencies": {
"collapse-white-space": "^2.0.0",
"micromark-util-normalize-identifier": "^1.0.0",
"unified-lint-rule": "^1.0.0",
"unist-util-generated": "^2.0.0",
"unist-util-visit": "^4.0.0"
Expand Down
10 changes: 3 additions & 7 deletions packages/remark-lint-no-unneeded-full-reference-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import {lintRule} from 'unified-lint-rule'
import {visit} from 'unist-util-visit'
import {generated} from 'unist-util-generated'
import {collapseWhiteSpace} from 'collapse-white-space'
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'

const remarkLintNoUnneededFullReferenceLink = lintRule(
'remark-lint:no-unneeded-full-reference-link',
Expand All @@ -57,7 +57,8 @@ const remarkLintNoUnneededFullReferenceLink = lintRule(
node.referenceType !== 'full' ||
node.children.length !== 1 ||
node.children[0].type !== 'text' ||
normalize(node.children[0].value) !== node.identifier
normalizeIdentifier(node.children[0].value) !==
node.identifier.toUpperCase()
) {
return
}
Expand All @@ -71,8 +72,3 @@ const remarkLintNoUnneededFullReferenceLink = lintRule(
)

export default remarkLintNoUnneededFullReferenceLink

// See: <https://github.com/remarkjs/remark/blob/cc7867b/packages/remark-parse/lib/util/normalize.js>
function normalize(value) {
return collapseWhiteSpace(value).toLowerCase()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"index.js"
],
"dependencies": {
"collapse-white-space": "^2.0.0",
"micromark-util-normalize-identifier": "^1.0.0",
"unified-lint-rule": "^1.0.0",
"unist-util-generated": "^2.0.0",
"unist-util-visit": "^4.0.0"
Expand Down

0 comments on commit 27a96be

Please sign in to comment.