Skip to content

Commit 110b303

Browse files
committed
fix(rule): fix index of match word
1 parent 6abeaac commit 110b303

File tree

3 files changed

+44
-48
lines changed

3 files changed

+44
-48
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@
3636
"espower-babel": "^3.3.0",
3737
"mocha": "^2.3.0",
3838
"power-assert": "^1.0.0",
39-
"textlint": "^3.2.0",
40-
"textlint-tester": "^0.2.0"
39+
"textlint": "^5.0.3",
40+
"textlint-tester": "^0.4.1"
4141
},
4242
"dependencies": {
43+
"kuromojin": "^1.0.2",
4344
"object-assign": "^4.0.1",
45+
"sentence-splitter": "^1.2.0",
46+
"structured-source": "^3.0.2",
4447
"textlint-rule-helper": "^1.1.3"
4548
}
4649
}

src/max-ten.js

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,62 @@
22
"use strict";
33
import {RuleHelper} from "textlint-rule-helper"
44
import ObjectAssign from "object-assign"
5+
import {getTokenizer} from "kuromojin";
6+
import splitSentences from "sentence-splitter";
7+
import Source from "structured-source";
58
const defaultOptions = {max: 3};
6-
function countTen(text) {
7-
return text.split("、").length - 1;
8-
}
99
/**
1010
* @param {RuleContext} context
1111
* @param {object} options
1212
*/
1313
export default function (context, options = {}) {
1414
options = ObjectAssign({}, defaultOptions, options);
1515
const maxLen = options.max;
16-
const punctuation = /[]/;
1716
let helper = new RuleHelper(context);
1817
let {Syntax, RuleError, report, getSource} = context;
19-
let currentParagraphTexts = [];
2018
return {
21-
[Syntax.Paragraph](){
22-
currentParagraphTexts = []
23-
},
24-
[Syntax.Str](node){
25-
// ignore text from external factor
26-
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote])) {
19+
[Syntax.Paragraph](node){
20+
if (helper.isChildNode(node, [Syntax.BlockQuote])) {
2721
return;
2822
}
29-
currentParagraphTexts.push(node);
30-
},
31-
[Syntax.Paragraph + ":exit"](){
32-
let currentTenCount = 0;
23+
let sentences = splitSentences(getSource(node), {
24+
charRegExp: /[\?\!]/,
25+
newLineCharacters: "\n\n"
26+
});
3327
/*
3428
<p>
3529
<str><code><img><str>
3630
<str>
3731
</p>
3832
*/
39-
currentParagraphTexts.forEach(strNode => {
40-
let paddingLine = 0;
41-
let paddingColumn = 0;
42-
let text = getSource(strNode);
43-
let characters = text.split("");
44-
characters.forEach(char => {
45-
if (char === "、") {
46-
currentTenCount++;
47-
}
48-
if (char === "。") {
49-
// reset
50-
currentTenCount = 0;
51-
}
52-
// report
53-
if (currentTenCount >= maxLen) {
54-
var ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
55-
line: paddingLine,
56-
column: paddingColumn
57-
});
58-
report(strNode, ruleError);
59-
currentTenCount = 0;
60-
}
61-
// calc padding{line,column}
62-
if (char === "\n") {
63-
paddingLine++;
64-
paddingColumn = 0;
65-
} else {
66-
paddingColumn++;
67-
}
33+
return getTokenizer().then(tokenizer => {
34+
sentences.forEach(sentence => {
35+
let text = sentence.value;
36+
let source = new Source(text);
37+
let currentTenCount = 0;
38+
let tokens = tokenizer.tokenizeForSentence(text);
39+
let lastToken = null;
40+
tokens.forEach(token => {
41+
let surface = token.surface_form;
42+
if (surface === "、") {
43+
currentTenCount++;
44+
lastToken = token;
45+
}
46+
if (surface === "。") {
47+
// reset
48+
currentTenCount = 0;
49+
}
50+
// report
51+
if (currentTenCount >= maxLen) {
52+
let position = source.indexToPosition(lastToken.word_position - 1);
53+
let ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
54+
line: position.line-1,
55+
column:position.column
56+
});
57+
report(node, ruleError);
58+
currentTenCount = 0;
59+
}
60+
});
6861
});
6962
});
7063
}

test/max-ten-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import rule from "../src/max-ten"
22
function textIncludeTen(count) {
3-
return (new Array(count + 1)).join("テスト、") + "です";
3+
return (new Array(count + 1)).join("テスト文章において、") + "です";
44
}
55
var TextLintTester = require("textlint-tester");
66
var tester = new TextLintTester();
@@ -24,7 +24,7 @@ tester.run("max-ten", rule, {
2424
],
2525
invalid: [
2626
{
27-
text: `a、b、 c
27+
text: `これは、これは、これは
2828
、d`
2929
,
3030
errors: [

0 commit comments

Comments
 (0)