Skip to content

Commit 8ecc647

Browse files
authored
feat: use padding for reporting error (#152)
Breaking Change: It depended on textlint 12.2.0>=
1 parent a923fce commit 8ecc647

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"@types/mocha": "^10.0.0",
5656
"@types/node": "^18.11.7",
5757
"@types/node-fetch": "^2.6.2",
58-
"cross-env": "^7.0.3",
5958
"lint-staged": "^13.0.3",
6059
"mocha": "^10.1.0",
6160
"prettier": "^2.7.1",
@@ -69,5 +68,13 @@
6968
"packageManager": "[email protected]",
7069
"engines": {
7170
"node": ">=4"
71+
},
72+
"peerDependencies": {
73+
"textlint": ">= 12.2.0"
74+
},
75+
"peerDependenciesMeta": {
76+
"textlint": {
77+
"optional": true
78+
}
7279
}
7380
}

src/no-dead-link.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ async function isAliveLocalFile(filePath: string): Promise<AliveFunctionReturn>
278278
}
279279

280280
const reporter: TextlintRuleReporter<Options> = (context, options) => {
281-
const { Syntax, getSource, report, RuleError, fixer, getFilePath } = context;
281+
const { Syntax, getSource, report, RuleError, fixer, getFilePath, locator } = context;
282282
const helper = new RuleHelper(context);
283283
const ruleOptions = { ...DEFAULT_OPTIONS, ...options };
284284
const isAliveURI = createCheckAliveURL(ruleOptions);
@@ -309,7 +309,7 @@ const reporter: TextlintRuleReporter<Options> = (context, options) => {
309309
const message =
310310
"Unable to resolve the relative URI. Please check if the base URI is correctly specified.";
311311

312-
report(node, new RuleError(message, { index }));
312+
report(node, new RuleError(message, { padding: locator.range([index, index + uri.length]) }));
313313
return;
314314
}
315315

@@ -338,11 +338,11 @@ const reporter: TextlintRuleReporter<Options> = (context, options) => {
338338
}
339339
if (!ok) {
340340
const lintMessage = `${uri} is dead. (${message})`;
341-
report(node, new RuleError(lintMessage, { index }));
341+
report(node, new RuleError(lintMessage, { padding: locator.range([index, index + uri.length]) }));
342342
} else if (redirected) {
343343
const lintMessage = `${uri} is redirected to ${redirectTo}. (${message})`;
344344
const fix = redirectTo ? fixer.replaceTextRange([index, index + uri.length], redirectTo) : undefined;
345-
report(node, new RuleError(lintMessage, { fix, index }));
345+
report(node, new RuleError(lintMessage, { fix, padding: locator.range([index, index + uri.length]) }));
346346
}
347347
};
348348

test/no-dead-link.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ tester.run("no-dead-link", rule, {
112112
errors: [
113113
{
114114
message: "https://httpstat.us/301 is redirected to https://httpstat.us/. (301 Moved Permanently)",
115-
line: 1,
116-
column: 18
115+
range: [17, 40]
117116
}
118117
]
119118
},
@@ -123,8 +122,7 @@ tester.run("no-dead-link", rule, {
123122
errors: [
124123
{
125124
message: "https://httpstat.us/301 is redirected to https://httpstat.us/. (301 Moved Permanently)",
126-
line: 1,
127-
column: 25
125+
range: [24, 47]
128126
}
129127
]
130128
},
@@ -217,8 +215,16 @@ tester.run("no-dead-link", rule, {
217215
errors: [
218216
{
219217
message: "https://httpstat.us/404 is dead. (404 Not Found)",
220-
line: 3,
221-
column: 6
218+
loc: {
219+
start: {
220+
line: 3,
221+
column: 6
222+
},
223+
end: {
224+
line: 3,
225+
column: 29
226+
}
227+
}
222228
}
223229
]
224230
}

yarn.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,14 +1775,7 @@ create-require@^1.1.0:
17751775
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
17761776
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
17771777

1778-
cross-env@^7.0.3:
1779-
version "7.0.3"
1780-
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
1781-
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
1782-
dependencies:
1783-
cross-spawn "^7.0.1"
1784-
1785-
cross-spawn@^7.0.1, cross-spawn@^7.0.3:
1778+
cross-spawn@^7.0.3:
17861779
version "7.0.3"
17871780
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
17881781
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==

0 commit comments

Comments
 (0)