diff --git a/index.js b/index.js index b9349b7..4a6e808 100644 --- a/index.js +++ b/index.js @@ -29,9 +29,6 @@ module.exports = attacher; var exists = fs.existsSync; var parse = url.parse; -/* Constants. */ -var NS = 'remark-validate-links'; - /** * Get the `pathname` of `uri`, if applicable. * @@ -89,17 +86,17 @@ function getClosest(pathname, references) { } /** - * Utilitity to warn `warning` for each node in `nodes`, + * Utilitity to warn `reason` for each node in `nodes`, * on `file`. * * @param {File} file - Virtual file. * @param {Array.} nodes - Offending nodes. - * @param {string} message - Message. + * @param {string} reason - Message. */ -function warnAll(file, nodes, message) { +function warnAll(file, nodes, reason) { nodes.forEach(function (node) { - var warning = file.warn(message, node); - warning.source = 'remark-validate-links'; + var message = file.message(reason, node); + message.source = message.ruleId = 'remark-validate-links'; }); } @@ -117,8 +114,8 @@ function warnAll(file, nodes, message) { */ function gatherReferences(file, tree, project) { var cache = {}; - var filePath = file.filePath(); - var directory = file.directory; + var filePath = file.path; + var dirname = file.dirname; var getDefinition; var prefix = ''; @@ -158,7 +155,7 @@ function gatherReferences(file, tree, project) { link = filePath + uri.hash; uri = parse(link); } else { - link = urljoin(directory || './', link); + link = urljoin(dirname, link); if (uri.hash) { link += uri.hash; } @@ -238,8 +235,8 @@ function gatherReferences(file, tree, project) { * @param {File} file - Set of virtual files. */ function validate(exposed, file) { - var references = file.namespace(NS).references; - var filePath = file.filePath(); + var references = file.data.remarkValidateLinksReferences; + var filePath = file.path; var reference; var nodes; var real; @@ -300,7 +297,7 @@ function completer(set, done) { var exposed = {}; set.valueOf().forEach(function (file) { - var landmarks = file.namespace(NS).landmarks; + var landmarks = file.data.remarkValidateLinksLandmarks; if (landmarks) { exposed = xtend(exposed, landmarks); @@ -309,7 +306,7 @@ function completer(set, done) { set.valueOf().forEach(function (file) { /* istanbul ignore else - stdin */ - if (file.filePath()) { + if (file.path) { validate(exposed, file); } }); @@ -337,8 +334,8 @@ function transformerFactory(project, fileSet) { * @param {File} file - Virtual file. */ function transformer(ast, file) { - var filePath = file.filePath(); - var space = file.namespace(NS); + var filePath = file.path; + var space = file.data; var links = []; var landmarks = {}; var references; @@ -379,8 +376,8 @@ function transformerFactory(project, fileSet) { } }); - space.references = references; - space.landmarks = landmarks; + space.remarkValidateLinksReferences = references; + space.remarkValidateLinksLandmarks = landmarks; } return transformer; @@ -408,7 +405,8 @@ function attacher(remark, options, fileSet) { * given. */ if (!repo) { try { - pack = require(path.resolve(process.cwd(), 'package.json')); + pack = fileSet.files[0].cwd; + pack = require(path.resolve(pack, 'package.json')); } catch (err) { pack = {}; } diff --git a/package.json b/package.json index f70b264..f7dcdbe 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "github-url-to-object": "^2.1.0", "mdast-util-definitions": "^1.0.0", "propose": "0.0.5", - "remark-slug": "^4.1.0", + "remark-slug": "^4.2.1", "unist-util-visit": "^1.0.0", "urljoin": "^0.1.5", "xtend": "^4.0.1" @@ -34,9 +34,9 @@ ], "devDependencies": { "execa": "^0.4.0", - "nyc": "^7.0.0", - "remark": "^5.0.0", - "remark-cli": "^1.0.0", + "nyc": "^8.1.0", + "remark": "^6.0.0", + "remark-cli": "^2.0.0", "remark-comment-config": "^4.0.0", "remark-github": "^5.0.0-alpha.1", "remark-lint": "^4.0.0", @@ -68,7 +68,7 @@ "functions": 100, "branches": 100 }, - "remarkConfig": { + "remarkConfig-": { "output": true, "plugins": [ "comment-config", diff --git a/test/index.js b/test/index.js index 389073c..852b5a5 100644 --- a/test/index.js +++ b/test/index.js @@ -26,6 +26,8 @@ test.onFinish(function () { var bin = path.join('..', '..', 'node_modules', '.bin', 'remark'); +var source = ' remark-validate-links remark-validate-links'; + /* Tests. */ test('remark-validate-links', function (t) { t.plan(8); @@ -45,7 +47,7 @@ test('remark-validate-links', function (t) { '--no-config', '--no-ignore', '--use', - '.', + '../../index', 'definitions.md', 'FOOOO' ]).catch(function (err) { @@ -57,7 +59,7 @@ test('remark-validate-links', function (t) { ' 1:1 error No such file or directory', '', 'definitions.md', - ' 5:12-5:21 warning Link to unknown heading: `world`', + ' 5:12-5:21 warning Link to unknown heading: `world`' + source, '', '2 messages (✖ 1 error, ⚠ 1 warning)', '' @@ -74,22 +76,22 @@ test('remark-validate-links', function (t) { '--no-config', '--no-ignore', '--use', - '.', + '../../index', 'example.md' ]).then(function (stderr) { st.equal( strip(stderr), [ 'example.md', - ' 5:37-5:51 warning Link to unknown heading: `world`', - ' 23:10-23:37 warning Link to unknown file: `examples/world.md`', - ' 25:10-25:35 warning Link to unknown file: `examples/world.md`', - ' 37:10-37:42 warning Link to unknown heading in `examples/example.md`: `world`', - ' 39:10-39:40 warning Link to unknown heading in `examples/example.md`: `world`', - ' 45:10-45:40 warning Link to unknown file: `examples/world.md`', - ' 45:10-45:40 warning Link to unknown heading in `examples/world.md`: `hello`', - ' 47:10-47:38 warning Link to unknown file: `examples/world.md`', - ' 47:10-47:38 warning Link to unknown heading in `examples/world.md`: `hello`', + ' 5:37-5:51 warning Link to unknown heading: `world` ' + source, + ' 23:10-23:37 warning Link to unknown file: `examples/world.md` ' + source, + ' 25:10-25:35 warning Link to unknown file: `examples/world.md` ' + source, + ' 37:10-37:42 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 39:10-39:40 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 45:10-45:40 warning Link to unknown file: `examples/world.md` ' + source, + ' 45:10-45:40 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 47:10-47:38 warning Link to unknown file: `examples/world.md` ' + source, + ' 47:10-47:38 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, '', '⚠ 9 warnings' ].join('\n'), @@ -105,7 +107,7 @@ test('remark-validate-links', function (t) { '--no-config', '--no-ignore', '--use', - '.', + '../../index', 'example.md', 'examples/example.md' ]).then(function (stderr) { @@ -113,27 +115,22 @@ test('remark-validate-links', function (t) { strip(stderr), [ 'example.md', - ' 5:37-5:51 warning Link to unknown heading: `world`', - ' 23:10-23:37 warning Link to unknown file: `examples/world.md`', - ' 25:10-25:35 warning Link to unknown file: `examples/world.md`', - ' 37:10-37:42 warning Link to unknown heading in `examples/example.md`: `world`', - ' 39:10-39:40 warning Link to unknown heading in `examples/example.md`: `world`', - ' 45:10-45:40 warning Link to unknown file: `examples/world.md`', - ' 45:10-45:40 warning Link to unknown heading in `examples/world.md`: `hello`', - ' 47:10-47:38 warning Link to unknown file: `examples/world.md`', - ' 47:10-47:38 warning Link to unknown heading in `examples/world.md`: `hello`', + ' 5:37-5:51 warning Link to unknown heading: `world` ' + source, + ' 23:10-23:37 warning Link to unknown file: `examples/world.md` ' + source, + ' 25:10-25:35 warning Link to unknown file: `examples/world.md` ' + source, + ' 37:10-37:42 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 39:10-39:40 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 45:10-45:40 warning Link to unknown file: `examples/world.md` ' + source, + ' 45:10-45:40 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 47:10-47:38 warning Link to unknown file: `examples/world.md` ' + source, + ' 47:10-47:38 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, '', 'examples/example.md', - ' 5:37-5:51 warning Link to unknown heading: ' + - '`world`', - ' 19:10-19:29 warning Link to unknown file: ' + - '`world.md`', - ' 29:10-29:34 warning Link to unknown heading in ' + - '`example.md`: `world`', - ' 35:10-35:32 warning Link to unknown file: ' + - '`world.md`', - ' 35:10-35:32 warning Link to unknown heading in ' + - '`world.md`: `hello`', + ' 5:37-5:51 warning Link to unknown heading: `world` ' + source, + ' 19:10-19:29 warning Link to unknown file: `world.md` ' + source, + ' 29:10-29:34 warning Link to unknown heading in `example.md`: `world` ' + source, + ' 35:10-35:32 warning Link to unknown file: `world.md` ' + source, + ' 35:10-35:32 warning Link to unknown heading in `world.md`: `hello` ' + source, '', '⚠ 14 warnings' ].join('\n'), @@ -149,14 +146,14 @@ test('remark-validate-links', function (t) { '--no-config', '--no-ignore', '--use', - '.', + '../../index', 'definitions.md' ]).then(function (stderr) { st.equal( strip(stderr), [ 'definitions.md', - ' 5:12-5:21 warning Link to unknown heading: `world`', + ' 5:12-5:21 warning Link to unknown heading: `world`' + source, '', '⚠ 1 warning' ].join('\n'), @@ -172,7 +169,7 @@ test('remark-validate-links', function (t) { '--no-config', '--no-ignore', '--use', - '.=repository:"wooorm/test"', + '../../index=repository:"wooorm/test"', 'example.md', 'examples/example.md' ]).then(function (stderr) { @@ -180,68 +177,38 @@ test('remark-validate-links', function (t) { strip(stderr), [ 'example.md', - ' 5:37-5:51 warning Link to unknown heading: ' + - '`world`', - ' 19:34-19:102 warning Link to unknown file: ' + - '`examples/world.md`', - ' 21:12-21:81 warning Link to unknown file: ' + - '`examples/world.md`', - ' 23:10-23:37 warning Link to unknown file: ' + - '`examples/world.md`', - ' 25:10-25:35 warning Link to unknown file: ' + - '`examples/world.md`', - ' 37:10-37:42 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 39:10-39:40 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 41:10-41:83 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 43:10-43:84 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 45:10-45:40 warning Link to unknown file: ' + - '`examples/world.md`', - ' 45:10-45:40 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 47:10-47:38 warning Link to unknown file: ' + - '`examples/world.md`', - ' 47:10-47:38 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 49:10-49:81 warning Link to unknown file: ' + - '`examples/world.md`', - ' 49:10-49:81 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 51:10-51:82 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 51:10-51:82 warning Link to unknown file: ' + - '`examples/world.md`', + ' 5:37-5:51 warning Link to unknown heading: `world` ' + source, + ' 19:34-19:102 warning Link to unknown file: `examples/world.md` ' + source, + ' 21:12-21:81 warning Link to unknown file: `examples/world.md` ' + source, + ' 23:10-23:37 warning Link to unknown file: `examples/world.md` ' + source, + ' 25:10-25:35 warning Link to unknown file: `examples/world.md` ' + source, + ' 37:10-37:42 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 39:10-39:40 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 41:10-41:83 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 43:10-43:84 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 45:10-45:40 warning Link to unknown file: `examples/world.md` ' + source, + ' 45:10-45:40 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 47:10-47:38 warning Link to unknown file: `examples/world.md` ' + source, + ' 47:10-47:38 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 49:10-49:81 warning Link to unknown file: `examples/world.md` ' + source, + ' 49:10-49:81 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 51:10-51:82 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 51:10-51:82 warning Link to unknown file: `examples/world.md` ' + source, '', 'examples/example.md', - ' 5:37-5:51 warning Link to unknown heading: ' + - '`world`', - ' 15:34-15:93 warning Link to unknown file: ' + - '`world.md`', - ' 17:12-17:72 warning Link to unknown file: ' + - '`world.md`', - ' 19:10-19:29 warning Link to unknown file: ' + - '`world.md`', - ' 29:10-29:34 warning Link to unknown heading in ' + - '`example.md`: `world`', - ' 31:10-31:74 warning Link to unknown heading in ' + - '`example.md`: `world`', - ' 33:10-33:75 warning Link to unknown heading in ' + - '`example.md`: `world`', - ' 35:10-35:32 warning Link to unknown file: ' + - '`world.md`', - ' 35:10-35:32 warning Link to unknown heading in ' + - '`world.md`: `hello`', - ' 37:10-37:72 warning Link to unknown file: ' + - '`world.md`', - ' 37:10-37:72 warning Link to unknown heading in ' + - '`world.md`: `hello`', - ' 39:10-39:73 warning Link to unknown heading in ' + - '`world.md`: `hello`', - ' 39:10-39:73 warning Link to unknown file: ' + - '`world.md`', + ' 5:37-5:51 warning Link to unknown heading: `world` ' + source, + ' 15:34-15:93 warning Link to unknown file: `world.md` ' + source, + ' 17:12-17:72 warning Link to unknown file: `world.md` ' + source, + ' 19:10-19:29 warning Link to unknown file: `world.md` ' + source, + ' 29:10-29:34 warning Link to unknown heading in `example.md`: `world` ' + source, + ' 31:10-31:74 warning Link to unknown heading in `example.md`: `world` ' + source, + ' 33:10-33:75 warning Link to unknown heading in `example.md`: `world` ' + source, + ' 35:10-35:32 warning Link to unknown file: `world.md` ' + source, + ' 35:10-35:32 warning Link to unknown heading in `world.md`: `hello` ' + source, + ' 37:10-37:72 warning Link to unknown file: `world.md` ' + source, + ' 37:10-37:72 warning Link to unknown heading in `world.md`: `hello` ' + source, + ' 39:10-39:73 warning Link to unknown heading in `world.md`: `hello` ' + source, + ' 39:10-39:73 warning Link to unknown file: `world.md` ' + source, '', '⚠ 30 warnings' ].join('\n'), @@ -275,68 +242,38 @@ test('remark-validate-links', function (t) { strip(stderr), [ 'example.md', - ' 5:37-5:51 warning Link to unknown heading: ' + - '`world`', - ' 19:34-19:102 warning Link to unknown file: ' + - '`examples/world.md`', - ' 21:12-21:81 warning Link to unknown file: ' + - '`examples/world.md`', - ' 23:10-23:37 warning Link to unknown file: ' + - '`examples/world.md`', - ' 25:10-25:35 warning Link to unknown file: ' + - '`examples/world.md`', - ' 37:10-37:42 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 39:10-39:40 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 41:10-41:83 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 43:10-43:84 warning Link to unknown heading in ' + - '`examples/example.md`: `world`', - ' 45:10-45:40 warning Link to unknown file: ' + - '`examples/world.md`', - ' 45:10-45:40 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 47:10-47:38 warning Link to unknown file: ' + - '`examples/world.md`', - ' 47:10-47:38 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 49:10-49:81 warning Link to unknown file: ' + - '`examples/world.md`', - ' 49:10-49:81 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 51:10-51:82 warning Link to unknown heading in ' + - '`examples/world.md`: `hello`', - ' 51:10-51:82 warning Link to unknown file: ' + - '`examples/world.md`', + ' 5:37-5:51 warning Link to unknown heading: `world` ' + source, + ' 19:34-19:102 warning Link to unknown file: `examples/world.md` ' + source, + ' 21:12-21:81 warning Link to unknown file: `examples/world.md` ' + source, + ' 23:10-23:37 warning Link to unknown file: `examples/world.md` ' + source, + ' 25:10-25:35 warning Link to unknown file: `examples/world.md` ' + source, + ' 37:10-37:42 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 39:10-39:40 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 41:10-41:83 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 43:10-43:84 warning Link to unknown heading in `examples/example.md`: `world`' + source, + ' 45:10-45:40 warning Link to unknown file: `examples/world.md` ' + source, + ' 45:10-45:40 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 47:10-47:38 warning Link to unknown file: `examples/world.md` ' + source, + ' 47:10-47:38 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 49:10-49:81 warning Link to unknown file: `examples/world.md` ' + source, + ' 49:10-49:81 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 51:10-51:82 warning Link to unknown heading in `examples/world.md`: `hello` ' + source, + ' 51:10-51:82 warning Link to unknown file: `examples/world.md` ' + source, '', 'examples/example.md', - ' 5:37-5:51 warning Link to unknown heading: ' + - '`world`', - ' 15:34-15:93 warning Link to unknown file: ' + - '`world.md`', - ' 17:12-17:72 warning Link to unknown file: ' + - '`world.md`', - ' 19:10-19:29 warning Link to unknown file: ' + - '`world.md`', - ' 29:10-29:34 warning Link to unknown heading in ' + - '`example.md`: `world`', - ' 31:10-31:74 warning Link to unknown heading in ' + - '`example.md`: `world`', - ' 33:10-33:75 warning Link to unknown heading in ' + - '`example.md`: `world`', - ' 35:10-35:32 warning Link to unknown file: ' + - '`world.md`', - ' 35:10-35:32 warning Link to unknown heading in ' + - '`world.md`: `hello`', - ' 37:10-37:72 warning Link to unknown file: ' + - '`world.md`', - ' 37:10-37:72 warning Link to unknown heading in ' + - '`world.md`: `hello`', - ' 39:10-39:73 warning Link to unknown heading in ' + - '`world.md`: `hello`', - ' 39:10-39:73 warning Link to unknown file: ' + - '`world.md`', + ' 5:37-5:51 warning Link to unknown heading: `world` ' + source, + ' 15:34-15:93 warning Link to unknown file: `world.md` ' + source, + ' 17:12-17:72 warning Link to unknown file: `world.md` ' + source, + ' 19:10-19:29 warning Link to unknown file: `world.md` ' + source, + ' 29:10-29:34 warning Link to unknown heading in `example.md`: `world` ' + source, + ' 31:10-31:74 warning Link to unknown heading in `example.md`: `world` ' + source, + ' 33:10-33:75 warning Link to unknown heading in `example.md`: `world` ' + source, + ' 35:10-35:32 warning Link to unknown file: `world.md` ' + source, + ' 35:10-35:32 warning Link to unknown heading in `world.md`: `hello` ' + source, + ' 37:10-37:72 warning Link to unknown file: `world.md` ' + source, + ' 37:10-37:72 warning Link to unknown heading in `world.md`: `hello` ' + source, + ' 39:10-39:73 warning Link to unknown heading in `world.md`: `hello` ' + source, + ' 39:10-39:73 warning Link to unknown file: `world.md` ' + source, '', '⚠ 30 warnings' ].join('\n'), @@ -352,17 +289,15 @@ test('remark-validate-links', function (t) { '--no-config', '--no-ignore', '--use', - '.', + '../../index', 'suggestions.md' ]).then(function (stderr) { st.equal( strip(stderr), [ 'suggestions.md', - ' 3:22-3:37 warning Link to unknown heading: ' + - '`helloo`. Did you mean `hello`', - ' 7:17-7:40 warning Link to unknown heading in ' + - '`example.md`: `fiiiles`. Did you mean `files`', + ' 3:22-3:37 warning Link to unknown heading: `helloo`. Did you mean `hello` ' + source, + ' 7:17-7:40 warning Link to unknown heading in `example.md`: `fiiiles`. Did you mean `files`' + source, '', '⚠ 2 warnings' ].join('\n'),