-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This new rule detects unused definitions and warns when they’re found. Closes GH-34.
- Loading branch information
Showing
6 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2016 Titus Wormer | ||
* @license MIT | ||
* @module no-unused-definitions | ||
* @fileoverview | ||
* Warn when unused definitions are found. | ||
* @example | ||
* <!-- Valid: --> | ||
* [foo][] | ||
* | ||
* [foo]: https://example.com | ||
* | ||
* <!-- Invalid: --> | ||
* [bar]: https://example.com | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/* eslint-env commonjs */ | ||
|
||
/* | ||
* Dependencies. | ||
*/ | ||
|
||
var position = require('mdast-util-position'); | ||
var visit = require('unist-util-visit'); | ||
|
||
/** | ||
* Warn when definitions with equal content are found. | ||
* | ||
* Matches case-insensitive. | ||
* | ||
* @param {Node} ast - Root node. | ||
* @param {File} file - Virtual file. | ||
* @param {*} preferred - Ignored. | ||
* @param {Function} done - Callback. | ||
*/ | ||
function noUnusedDefinitions(ast, file, preferred, done) { | ||
var map = {}; | ||
var identifier; | ||
|
||
/** | ||
* Check `node`. | ||
* | ||
* @param {Node} node - Node. | ||
*/ | ||
function find(node) { | ||
if (position.generated(node)) { | ||
return; | ||
} | ||
|
||
map[node.identifier.toUpperCase()] = { | ||
'node': node, | ||
'used': false | ||
}; | ||
} | ||
|
||
/** | ||
* Mark `node`. | ||
* | ||
* @param {Node} node - Node. | ||
*/ | ||
function mark(node) { | ||
var info = map[node.identifier.toUpperCase()]; | ||
|
||
if (position.generated(node) || !info) { | ||
return; | ||
} | ||
|
||
info.used = true; | ||
} | ||
|
||
visit(ast, 'definition', find); | ||
visit(ast, 'footnoteDefinition', find); | ||
|
||
visit(ast, 'imageReference', mark); | ||
visit(ast, 'linkReference', mark); | ||
visit(ast, 'footnoteReference', mark); | ||
|
||
for (identifier in map) { | ||
if (!map[identifier].used) { | ||
file.warn('Found unused definition', map[identifier].node); | ||
} | ||
} | ||
|
||
done(); | ||
} | ||
|
||
/* | ||
* Expose. | ||
*/ | ||
|
||
module.exports = noUnusedDefinitions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[bar]: https://example.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[foo][] | ||
|
||
[foo]: https://example.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters