Skip to content

Commit f63ed80

Browse files
committed
fix(rule): calc line and column
1 parent 13b984a commit f63ed80

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

src/max-ten.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ function countTen(text) {
1212
*/
1313
export default function (context, options = {}) {
1414
options = ObjectAssign({}, defaultOptions, options);
15-
var maxLen = options.max;
15+
const maxLen = options.max;
1616
const punctuation = /[]/;
1717
let helper = new RuleHelper(context);
1818
let {Syntax, RuleError, report, getSource} = context;
19-
var currentParagraphTexts = [];
19+
let currentParagraphTexts = [];
2020
return {
2121
[Syntax.Paragraph](){
2222
currentParagraphTexts = []
@@ -28,16 +28,37 @@ export default function (context, options = {}) {
2828
}
2929
currentParagraphTexts.push(node);
3030
},
31-
[Syntax.Paragraph + ":exit"](){
32-
currentParagraphTexts.forEach(node => {
33-
var currentParagraphText = getSource(node);
34-
var sentences = currentParagraphText.split(punctuation);
35-
sentences.forEach(sentence => {
36-
if (countTen(sentence) >= maxLen) {
37-
var ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`);
38-
report(node, ruleError);
39-
}
40-
});
31+
[Syntax.Paragraph + ":exit"](pNode){
32+
let currentTenCount = 0;
33+
let text = currentParagraphTexts.map(strNode => getSource(strNode)).join("\n");
34+
let characters = text.split("");// ["t","e","x","t"]
35+
36+
let paddingLine = 0;
37+
let paddingColumn = 0;
38+
characters.forEach(char => {
39+
if (char === "、") {
40+
currentTenCount++;
41+
}
42+
if (char === "。") {
43+
// reset
44+
currentTenCount = 0;
45+
}
46+
// report
47+
if (currentTenCount >= maxLen) {
48+
var ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
49+
line: paddingLine,
50+
column: paddingColumn
51+
});
52+
report(pNode, ruleError);
53+
currentTenCount = 0;
54+
}
55+
// calc padding{line,column}
56+
if (char === "\n") {
57+
paddingLine++;
58+
paddingColumn = 0;
59+
} else {
60+
paddingColumn++;
61+
}
4162
});
4263
}
4364
}

test/fixtures/error.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/pass.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/max-ten-test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ tester.run("max-ten", rule, {
2323

2424
],
2525
invalid: [
26+
{
27+
text: `a、b、 c
28+
、d`
29+
,
30+
errors: [
31+
{
32+
message: `一つの文で"、"を3つ以上使用しています`,
33+
line: 2,
34+
column: 1
35+
}
36+
]
37+
},
2638
{
2739
text: textIncludeTen(5),
2840
options: {
@@ -35,14 +47,15 @@ tester.run("max-ten", rule, {
3547
]
3648
},
3749
{
38-
text: `これは、長文で、
39-
行を分けたときにも、カウントされてるかをテスト、しています。`,
50+
text: `これは、長文、columnがちゃんと計算、されてるはずです。`,
4051
options: {
4152
"max": 3
4253
},
4354
errors: [
4455
{
45-
message: `一つの文で"、"を3つ以上使用しています`
56+
message: `一つの文で"、"を3つ以上使用しています`,
57+
line: 1,
58+
column: 21
4659
}
4760
]
4861
}

0 commit comments

Comments
 (0)