Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
"publish-package": "npm publish",
"prepublishOnly": "npm run build",
"build": "npx babel src -d build",
"test": "concurrently \"npm:test:*\"",
"test": "concurrently \"npm:test:*\" && node --test src",
"test:tests": "npx mocha ./tests --inline-diffs true",
"test:utils": "npx mocha ./tests/utils --inline-diffs true",
"test:integration": "npm run textlint-pretty && npm run textlint-stylish && npm run textlint-json",
"coverage": "npx c8 --reporter=lcov npm run test",
"lint": "concurrently \"npm:lint:*\"",
Expand Down
4 changes: 2 additions & 2 deletions src/textlint-rule-allowed-uris.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// --------------------------------------------------------------------------------

const { error, highlight, strikethrough } = require('./utils/theme');
const getUriList = require('./utils/getUriList');
const getUriTypes = require('./utils/get-uri-types');

// --------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -47,7 +47,7 @@ const reporter = async ({ report, locator, RuleError }, options, node) => {
};

/* Report process */
(await getUriList(node)).uriTypes.forEach(({ uri, type }) => {
(await getUriTypes(node)).uriTypes.forEach(({ uri, type }) => {
Object.keys(regexes).forEach(key => {
// The `some` method returns `true` if any element in the array satisfies the given condition. In the case of an empty array, there are no elements to satisfy the condition, so the method returns `false`. Therefore, calling the `some` method on an empty array will always return `false`.
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const url = require('node:url');
const mime = require('mime-types');
const axios = require('axios');

const { error } = require('./theme');
const { error } = require('../theme');

// --------------------------------------------------------------------------------
// Helpers
Expand All @@ -20,9 +20,9 @@ const { error } = require('./theme');
/**
* Retrieves the MIME type of a given URI.
*
* @async
* @param {string} uri The URI to check. It can be a remote or local URI.
* @returns {Promise<string>} A promise that resolves to the MIME type of the URI. Defaults to `application/octet-stream` if the MIME type can't be determined.
* @returns {Promise<string>} Resolves to the MIME type of the URI. Defaults to `application/octet-stream` if the MIME type can't be determined.
* @async
*/
const getMimeType = async uri => {
try {
Expand All @@ -48,9 +48,9 @@ const getMimeType = async uri => {
/**
* Retrieves the type of a given URI.
*
* @async
* @param {string} uri The URI to check. It can be a remote or local URI.
* @returns {Promise<string>} A promise that resolves to `'comment'` for empty(` `) or hash-only(`#`) URIs, `'image'` if the URI's MIME type is an image, and `'link'` for other types of URIs.
* @returns {Promise<'comment' | 'image' | 'link'>} Resolves to `'comment'` for empty(` `) or hash-only(`#`) URIs, `'image'` if the URI's MIME type is an image, and `'link'` for other types of URIs.
* @async
*/
module.exports = async uri => {
if (['', '#'].includes(uri)) return 'comment';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @fileoverview Test for `get-definition-node-uri-type.js`.
*/

// --------------------------------------------------------------------------------
// Require
// --------------------------------------------------------------------------------

const { strictEqual } = require('node:assert');
const { describe, it } = require('node:test');

const testCases = require('./get-definition-node-uri-type.data');
const getDefinitionNodeUriType = require('./get-definition-node-uri-type');

// --------------------------------------------------------------------------------
// Test
// --------------------------------------------------------------------------------

describe('get-definition-node-uri-type.js', () => {
testCases.forEach(({ actual, expected }) => {
it(`${actual} => ${expected}`, async () => {
strictEqual(await getDefinitionNodeUriType(actual), expected);
});
});
});
3 changes: 3 additions & 0 deletions src/utils/get-definition-node-uri-type/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getDefinitionNodeUriType = require('./get-definition-node-uri-type');

module.exports = getDefinitionNodeUriType;
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@

const cheerio = require('cheerio');

const UriTypes = require('./uri-types');
const getDefinitionNodeUriType = require('./getDefinitionNodeUriType');
const UriTypes = require('../uri-types');
const getDefinitionNodeUriType = require('../get-definition-node-uri-type');

// --------------------------------------------------------------------------------
// Typedefs
// --------------------------------------------------------------------------------

/**
* @typedef {import('@textlint/ast-node-types').TxtLinkNode} TxtLinkNode
* @typedef {import('@textlint/ast-node-types').TxtImageNode} TxtImageNode
* @typedef {import('@textlint/ast-node-types').TxtDefinitionNode} TxtDefinitionNode
* @typedef {import('@textlint/ast-node-types').TxtHtmlNode} TxtHtmlNode
*/

// --------------------------------------------------------------------------------
// Helpers
Expand All @@ -20,27 +31,24 @@ const getDefinitionNodeUriType = require('./getDefinitionNodeUriType');
/**
* Retrieves URI from a given `Link` node and returns an instance of `UriTypes`.
*
* @param {object} node `Link` type node.
* @param {string} node.url The URI of the node.
* @param {TxtLinkNode} node `Link` type node.
*/
const getUriListLink = ({ url }) => new UriTypes().push({ uri: url, type: 'link' });
const getUriTypesLink = ({ url }) => new UriTypes().push({ uri: url, type: 'link' });

/**
* Retrieves URI from a given `Image` node and returns an instance of `UriTypes`.
*
* @param {object} node `Image` type node.
* @param {string} node.url The URI of the node.
* @param {TxtImageNode} node `Image` type node.
*/
const getUriListImage = ({ url }) => new UriTypes().push({ uri: url, type: 'image' });
const getUriTypesImage = ({ url }) => new UriTypes().push({ uri: url, type: 'image' });

/**
* Retrieves URI from a given `Definition` node and returns an instance of `UriTypes`.
*
* @param {object} node `Definition` type node.
* @param {string} node.url The URI of the node.
* @param {TxtDefinitionNode} node `Definition` type node.
* @async
*/
const getUriListDefinition = async ({ url }) => {
const getUriTypesDefinition = async ({ url }) => {
const type = await getDefinitionNodeUriType(url);

return ['link', 'image'].includes(type)
Expand All @@ -52,10 +60,9 @@ const getUriListDefinition = async ({ url }) => {
/**
* Parses the HTML content of the given node and retrieves all the `<a>` and `<img>` tag's URIs.
*
* @param {object} node `Html` type node.
* @param {string} node.value The raw HTML string.
* @param {TxtHtmlNode} node `Html` type node.
*/
const getUriListHtml = ({ value }) => {
const getUriTypesHtml = ({ value }) => {
const uriTypes = new UriTypes();
const $ = cheerio.load(value);

Expand Down Expand Up @@ -86,28 +93,23 @@ const getUriListHtml = ({ value }) => {
/**
* Retrieves the URI and creates an instance of `UriTypes` from a given `node`.
*
* @param {TxtLinkNode | TxtImageNode | TxtDefinitionNode | TxtHtmlNode} node The node from which to retrieve the URI.
* @throws {TypeError} Throws an `TypeError` if `node.type` is not one of `Link`, `Image`, `Definition`, or `Html`.
* @async
* @param {Object} node The node from which to retrieve the URI.
* @param {string} node.type The type of the node, which should be `Link`, `Image`, `Definition`, or `Html`.
* @throws Throws an `TypeError` error if `node.type` is not one of `Link`, `Image`, `Definition`, or `Html`.
*/
module.exports = async node => {
module.exports = async function getUriTypes(node) {
switch (node.type) {
case 'Link':
// @ts-ignore -- TODO
return getUriListLink(node);
return getUriTypesLink(node);
case 'Image':
// @ts-ignore -- TODO
return getUriListImage(node);
return getUriTypesImage(node);
case 'Definition':
// @ts-ignore -- TODO
return getUriListDefinition(node);
return getUriTypesDefinition(node);
case 'Html':
// @ts-ignore -- TODO
return getUriListHtml(node);
return getUriTypesHtml(node);
default:
throw new TypeError(
`'${node.type}' is an invalid 'node.type' parameter. It should be 'Link', 'Image', 'Definition', or 'Html'`,
'Invalid `node.type` parameter. It should be `Link`, `Image`, `Definition`, or `Html`',
);
}
};
30 changes: 30 additions & 0 deletions src/utils/get-uri-types/get-uri-types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @fileoverview Test for `get-uri-types.js`.
*/

// --------------------------------------------------------------------------------
// Require
// --------------------------------------------------------------------------------

const { deepStrictEqual, rejects } = require('node:assert');
const { describe, it } = require('node:test');

const testCases = require('./get-uri-types.data');
const getUriTypes = require('./get-uri-types');

// --------------------------------------------------------------------------------
// Test
// --------------------------------------------------------------------------------

describe('get-uri-types.js', () => {
testCases.forEach(({ actual, expected }) => {
it(`${actual.raw} => ${expected.map(({ uri, type }) => `${uri} ${type}`).join(' / ')}`, async () => {
if (['Link', 'Image', 'Definition', 'Html'].includes(actual.type))
deepStrictEqual((await getUriTypes(actual)).uriTypes, expected);
else
await rejects(async () => {
await getUriTypes(actual);
});
});
});
});
3 changes: 3 additions & 0 deletions src/utils/get-uri-types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getUriTypes = require('./get-uri-types');

module.exports = getUriTypes;
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
// --------------------------------------------------------------------------------

const { deepStrictEqual, throws } = require('node:assert');
const { describe, it } = require('node:test');

const UriTypes = require('../../src/utils/uri-types');
const UriTypes = require('./uri-types');

// --------------------------------------------------------------------------------
// Test
Expand Down
38 changes: 31 additions & 7 deletions tests/textlint-rule-allowed-uris.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
const fs = require('node:fs');
const path = require('node:path');
/**
* @fileoverview Test for `textlint-rule-allowed-uris.js`.
*/

// --------------------------------------------------------------------------------
// Require
// --------------------------------------------------------------------------------

const { readFileSync } = require('node:fs');
const { resolve } = require('node:path');
const { describe } = require('node:test');

const TextLintTester = require('textlint-tester').default;

const allowedUris = require('../src/textlint-rule-allowed-uris');
const testCases = require('./textlint-rule-allowed-uris.data');

// --------------------------------------------------------------------------------
// Helpers
// --------------------------------------------------------------------------------

const tester = new TextLintTester();
const testCasesMarkdown = fs.readFileSync(
path.resolve(__dirname, 'textlint-rule-allowed-uris.data.md'),
const testCasesMarkdown = readFileSync(
resolve(__dirname, 'textlint-rule-allowed-uris.data.md'),
'utf-8',
);

/**
* Tests for the `textlint-rule-allowed-uris.js`
*/
// --------------------------------------------------------------------------------
// Test
// --------------------------------------------------------------------------------

describe('textlint-rule-allowed-uris', () => {
testCases.forEach(({ options, lines }) => {
/* Initialization */
Expand Down Expand Up @@ -46,5 +62,13 @@ describe('textlint-rule-allowed-uris', () => {
invalid,
},
);

console.log(`
allowed links: ${options?.allowed?.links?.join(' or ')}
allowed images: ${options?.allowed?.images?.join(' or ')}
disallowed links: ${options?.disallowed?.links?.join(' or ')}
disallowed images: ${options?.disallowed?.links?.join(' or ')}
lines: ${lines.join(', ')}
`);
});
});
14 changes: 0 additions & 14 deletions tests/utils/getDefinitionNodeUriType.test.js

This file was deleted.

19 changes: 0 additions & 19 deletions tests/utils/getUriList.test.js

This file was deleted.