Skip to content

Commit 2204b07

Browse files
authored
fix(*): incorrect error location and refactor (#304)
This pull request refactors the `textlint-rule-allowed-uris` module by removing the `theme.js` utility file and relocating its functionality directly into the main rule file. Additionally, it improves readability and simplifies error handling in related files. Below are the key changes grouped by theme: ### Refactoring and Code Simplification: - Removed the `theme.js` file entirely, and its `error` and `strikethrough` functions were re-implemented directly in `src/textlint-rule-allowed-uris.js` under a new "Helpers" section. (`[[1]](diffhunk://#diff-a2b1e5729a82f074ccb347987a8e0f012306a67763b353dde9875db8b069afa5L1-L13)`, `[[2]](diffhunk://#diff-7da72e1e0d5c49a43b94f9d928d3157918c0bdcc3121c5ed14e0bc459b3f26f9R22-R43)`) - Updated the `report` function in `src/textlint-rule-allowed-uris.js` to use the relocated `error` and `strikethrough` helpers, simplifying its structure by converting it from an arrow function to a named `async function`. (`[[1]](diffhunk://#diff-7da72e1e0d5c49a43b94f9d928d3157918c0bdcc3121c5ed14e0bc459b3f26f9L52-R73)`, `[[2]](diffhunk://#diff-7da72e1e0d5c49a43b94f9d928d3157918c0bdcc3121c5ed14e0bc459b3f26f9L67-R92)`) ### Dependency Cleanup: - Removed the `error` import from `src/utils/get-definition-node-uri-type/get-definition-node-uri-type.js`, as it is no longer needed due to the removal of `theme.js`. (`[src/utils/get-definition-node-uri-type/get-definition-node-uri-type.jsL11](diffhunk://#diff-7facb9d42bcc13857fefafc7c89f3bc71f7aea27042c5251c4d534b101628c8eL11)`) - Simplified error handling in `getMimeType` by removing the usage of the `error` helper and directly throwing plain error messages. (`[src/utils/get-definition-node-uri-type/get-definition-node-uri-type.jsL32-L34](diffhunk://#diff-7facb9d42bcc13857fefafc7c89f3bc71f7aea27042c5251c4d534b101628c8eL32-L34)`) ### Test Updates: - Modified the test cases in `tests/textlint-rule-allowed-uris.test.js` to replace the `createErrors` function with explicit error objects for improved clarity and flexibility. (`[tests/textlint-rule-allowed-uris.test.jsL168-R178](diffhunk://#diff-89b20226c99b6337f60cfce86fa424eded9dcce3e51d2231af14c9ca52614ae1L168-R178)`)
1 parent 007cc94 commit 2204b07

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

src/textlint-rule-allowed-uris.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// Import
88
// --------------------------------------------------------------------------------
99

10-
import { error, strikethrough } from './utils/theme.js';
1110
import { getUriTypesDefinition, getUriTypesHtml } from './utils/get-uri-types/index.js';
1211

1312
// --------------------------------------------------------------------------------
@@ -20,6 +19,28 @@ import { getUriTypesDefinition, getUriTypesHtml } from './utils/get-uri-types/in
2019
* @import { UriType, Options } from './utils/types.js';
2120
*/
2221

22+
// --------------------------------------------------------------------------------
23+
// Helpers
24+
// --------------------------------------------------------------------------------
25+
26+
/**
27+
* Console error theme.
28+
* @param {string} str
29+
* @return {string}
30+
*/
31+
function error(str) {
32+
return `\u001b[31m${str}\u001b[0m`;
33+
}
34+
35+
/**
36+
* Console strikethrough theme.
37+
* @param {string} str
38+
* @return {string}
39+
*/
40+
function strikethrough(str) {
41+
return `\u001b[9m${str}\u001b[0m`;
42+
}
43+
2344
// --------------------------------------------------------------------------------
2445
// Export
2546
// --------------------------------------------------------------------------------
@@ -49,7 +70,7 @@ export default function textlintRuleAllowedUris(context, rawOptions) {
4970
* @returns {Promise<void>}
5071
* @async
5172
*/
52-
const report = async (node, uriTypes) => {
73+
async function report(node, uriTypes) {
5374
uriTypes.forEach(({ uri, type }) => {
5475
Object.keys(options).forEach(key => {
5576
// The `some` method returns `true` if any element in the array satisfies the given condition.
@@ -64,14 +85,11 @@ export default function textlintRuleAllowedUris(context, rawOptions) {
6485
node,
6586
new context.RuleError(
6687
`${error(`${key}.${type}s`)}\n${error('-')} problem: '${strikethrough(uri)}'\n${error('-')} ${key} regular expressions: '${options[key][`${type}s`].join(' or ')}'`,
67-
{
68-
padding: context.locator.at(0),
69-
},
7088
),
7189
);
7290
});
7391
});
74-
};
92+
}
7593

7694
return {
7795
/** @param {TxtLinkNode} node */

src/utils/get-definition-node-uri-type/get-definition-node-uri-type.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import url from 'node:url';
1010
import mime from 'mime-types';
11-
import { error } from '../theme.js';
1211

1312
// --------------------------------------------------------------------------------
1413
// Helpers
@@ -29,9 +28,7 @@ const getMimeType = async uri => {
2928
// fetch failed. (Internet connection)
3029
if (err?.cause?.code === 'ENOTFOUND')
3130
throw new Error(
32-
error(
33-
'The linting process includes an HTTP request, so an internet connection is required',
34-
),
31+
'The linting process includes an HTTP request, so an internet connection is required',
3532
);
3633
// fetch failed. (i.e. Local URI links)
3734
return mime.lookup(url.parse(uri).pathname) || 'application/octet-stream'; // eslint-disable-line -- TODO: Remove this comment.

src/utils/theme.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/textlint-rule-allowed-uris.test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,17 @@ test('textlint-rule-allowed-uris', () => {
165165
// images: [],
166166
},
167167
},
168-
errors: createErrors([
169-
7, 9, 13, 15, 19, 21, 23, 25, 31, 37, 43, 47, 86, 101, 103, 105,
170-
]),
168+
errors: [
169+
{
170+
line: 7,
171+
range: [97, 119],
172+
},
173+
{
174+
line: 9,
175+
range: [121, 138],
176+
},
177+
...createErrors([13, 15, 19, 21, 23, 25, 31, 37, 43, 47, 86, 101, 103, 105]),
178+
],
171179
},
172180
{
173181
name: 'Pass Empty Array - 2',

0 commit comments

Comments
 (0)