Skip to content

Commit 0b0bf38

Browse files
committed
feat: use locator for improving error location
1 parent 96326ca commit 0b0bf38

File tree

4 files changed

+1084
-1512
lines changed

4 files changed

+1084
-1512
lines changed

package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@
4545
"trailingComma": "none"
4646
},
4747
"devDependencies": {
48-
"@textlint/types": "^12.1.1",
49-
"@types/node": "^17.0.34",
50-
"lint-staged": "^12.4.1",
51-
"prettier": "^2.6.2",
52-
"textlint-scripts": "^12.1.1",
53-
"textlint-tester": "^12.1.1",
54-
"ts-node": "^10.7.0",
55-
"typescript": "^4.6.4"
48+
"@textlint/types": "^13.3.2",
49+
"@types/node": "^18.15.10",
50+
"lint-staged": "^13.2.0",
51+
"prettier": "^2.8.7",
52+
"textlint-scripts": "^13.3.2",
53+
"textlint-tester": "^13.3.2",
54+
"ts-node": "^10.9.1",
55+
"typescript": "^5.0.2"
5656
},
5757
"publishConfig": {
5858
"access": "public"
5959
},
6060
"dependencies": {
61-
"@textlint/regexp-string-matcher": "^2.0.1",
62-
"textlint-rule-helper": "^2.2.1"
61+
"@textlint/regexp-string-matcher": "^2.0.2",
62+
"textlint-rule-helper": "^2.3.0"
6363
}
6464
}

src/textlint-rule-pattern.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const replace = (text: string, captures: string[]) => {
5656
});
5757
};
5858
const report: TextlintRuleReporter<Options> = (context, options) => {
59-
const { Syntax, report, RuleError, fixer, getSource } = context;
59+
const { Syntax, report, RuleError, fixer, getSource, locator } = context;
6060
const patterns = options?.patterns ?? [];
6161
const helper = new RuleHelper(context);
6262
const reportIfError = (node: TxtNode) => {
@@ -83,15 +83,15 @@ const report: TextlintRuleReporter<Options> = (context, options) => {
8383
report(
8484
node,
8585
new RuleError(pattern.message, {
86-
index: index,
86+
padding: locator.range([index, index + match.length]),
8787
fix: fixer.replaceTextRange([index, index + match.length], replaceText)
8888
})
8989
);
9090
} else {
9191
report(
9292
node,
9393
new RuleError(pattern.message, {
94-
index: index
94+
padding: locator.range([index, index + match.length])
9595
})
9696
);
9797
}

test/textlint-rule-pattern.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ tester.run("textlint-rule-pattern", rule, {
5151
errors: [
5252
{
5353
message: "No todo",
54-
index: 0
54+
range: [0, 5]
5555
}
5656
]
5757
},
@@ -109,15 +109,15 @@ Next is Code Block.
109109
errors: [
110110
{
111111
message: "Must to use YYYY-MM-DD instead of YYYY/MM/DD",
112-
index: 2
112+
range: [2, 12]
113113
},
114114
{
115115
message: "Must to use YYYY-MM-DD instead of YYYY/MM/DD",
116-
index: 15
116+
range: [15, 25]
117117
},
118118
{
119119
message: "Must to use YYYY-MM-DD instead of YYYY/MM/DD",
120-
index: 28
120+
range: [28, 38]
121121
}
122122
]
123123
}

0 commit comments

Comments
 (0)