Skip to content

Commit cd739dc

Browse files
committed
fix(rule): fix throw error when expected contain regexp characters
1 parent cde6a86 commit cd739dc

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"prh"
3333
],
3434
"dependencies": {
35-
"prh": "^0.5.0",
35+
"prh": "^0.6.0",
3636
"structured-source": "^3.0.2",
3737
"textlint-rule-helper": "^1.1.3"
3838
},

src/prh-rule.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ Please set .textlinrc:
3434
let src = new StructuredSource(text);
3535
let makeChangeSet = config.makeChangeSet(null, text);
3636
makeChangeSet.forEach(function (changeSet) {
37-
// Avoid accidental match(ignore case, expected contain actual pattern)
38-
var expectedQuery = new RegExp('^' + changeSet.expected);
39-
// | ----[match------|
37+
// | ----[match]------
4038
var slicedText = text.slice(changeSet.index);
41-
if (expectedQuery.test(slicedText)) {
39+
// | ----[match------|
40+
var matchedText = slicedText.slice(0, changeSet.matches[0].length);
41+
var expected = matchedText.replace(changeSet.pattern, changeSet.expected);
42+
// Avoid accidental match(ignore case, expected contain actual pattern)
43+
if (slicedText.indexOf(expected) === 0) {
4244
return;
4345
}
4446
/*
@@ -48,11 +50,9 @@ Please set .textlinrc:
4850
adjust position => line -1, column + 1
4951
*/
5052
var position = src.indexToPosition(changeSet.index);
51-
// | ----[match]------|
52-
var matchedText = slicedText.slice(0, changeSet.matches[0].length);
53-
var expected = matchedText.replace(changeSet.pattern, changeSet.expected);
53+
5454
// line, column
55-
context.report(node, new context.RuleError(changeSet.matches[0] + " => " + expected, {
55+
context.report(node, new RuleError(changeSet.matches[0] + " => " + expected, {
5656
line: position.line - 1,
5757
column: position.column + 1
5858
}));

test/prh-rule-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("prh-rule-test", function () {
2424
assert(result.messages[0].column === 4);
2525
});
2626
});
27-
context("when match word and s/Web/Web/i pattern", function () {
27+
context("when match word but s/Web/Web/i pattern", function () {
2828
// fix ignore (the) case
2929
it("should not report", function () {
3030
var result = textlint.lintMarkdown("jQuery");

0 commit comments

Comments
 (0)