Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use padding for reporting error #152

Merged
merged 2 commits into from Oct 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion package.json
Expand Up @@ -55,7 +55,6 @@
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.7",
"@types/node-fetch": "^2.6.2",
"cross-env": "^7.0.3",
"lint-staged": "^13.0.3",
"mocha": "^10.1.0",
"prettier": "^2.7.1",
Expand All @@ -69,5 +68,13 @@
"packageManager": "yarn@1.22.15",
"engines": {
"node": ">=4"
},
"peerDependencies": {
"textlint": ">= 12.2.0"
},
"peerDependenciesMeta": {
"textlint": {
"optional": true
}
}
}
8 changes: 4 additions & 4 deletions src/no-dead-link.ts
Expand Up @@ -278,7 +278,7 @@ async function isAliveLocalFile(filePath: string): Promise<AliveFunctionReturn>
}

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

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

Expand Down Expand Up @@ -338,11 +338,11 @@ const reporter: TextlintRuleReporter<Options> = (context, options) => {
}
if (!ok) {
const lintMessage = `${uri} is dead. (${message})`;
report(node, new RuleError(lintMessage, { index }));
report(node, new RuleError(lintMessage, { padding: locator.range([index, index + uri.length]) }));
} else if (redirected) {
const lintMessage = `${uri} is redirected to ${redirectTo}. (${message})`;
const fix = redirectTo ? fixer.replaceTextRange([index, index + uri.length], redirectTo) : undefined;
report(node, new RuleError(lintMessage, { fix, index }));
report(node, new RuleError(lintMessage, { fix, padding: locator.range([index, index + uri.length]) }));
}
};

Expand Down
18 changes: 12 additions & 6 deletions test/no-dead-link.ts
Expand Up @@ -112,8 +112,7 @@ tester.run("no-dead-link", rule, {
errors: [
{
message: "https://httpstat.us/301 is redirected to https://httpstat.us/. (301 Moved Permanently)",
line: 1,
column: 18
range: [17, 40]
}
]
},
Expand All @@ -123,8 +122,7 @@ tester.run("no-dead-link", rule, {
errors: [
{
message: "https://httpstat.us/301 is redirected to https://httpstat.us/. (301 Moved Permanently)",
line: 1,
column: 25
range: [24, 47]
}
]
},
Expand Down Expand Up @@ -217,8 +215,16 @@ tester.run("no-dead-link", rule, {
errors: [
{
message: "https://httpstat.us/404 is dead. (404 Not Found)",
line: 3,
column: 6
loc: {
start: {
line: 3,
column: 6
},
end: {
line: 3,
column: 29
}
}
}
]
}
Expand Down
9 changes: 1 addition & 8 deletions yarn.lock
Expand Up @@ -1775,14 +1775,7 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cross-env@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
dependencies:
cross-spawn "^7.0.1"

cross-spawn@^7.0.1, cross-spawn@^7.0.3:
cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
Expand Down