Skip to content

Commit c1e96a4

Browse files
committed
Fix position to report in a certain cases
1 parent 2b86a63 commit c1e96a4

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/max-ten.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ function isSandwichedMeishi({
1919
}
2020
return before.pos === "名詞" && after.pos === "名詞";
2121
}
22+
/**
23+
* add two positions.
24+
* note: line starts with 1, column starts with 0.
25+
* @param {Position} base
26+
* @param {Position} relative
27+
* @return {Position}
28+
*/
29+
function addPositions(base, relative) {
30+
return {
31+
line: base.line + relative.line - 1, // line 1 + line 1 should be line 1
32+
column: relative.line == 1 ? base.column + relative.column // when the same line
33+
: relative.column // when another line
34+
};
35+
}
2236
/**
2337
* @param {RuleContext} context
2438
* @param {object} options
@@ -78,10 +92,11 @@ export default function (context, options = {}) {
7892
}
7993
// report
8094
if (currentTenCount >= maxLen) {
81-
let position = source.indexToPosition(lastToken.word_position - 1);
95+
let positionInSentence = source.indexToPosition(lastToken.word_position - 1);
96+
let positionInNode = addPositions(sentence.loc.start, positionInSentence);
8297
let ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
83-
line: position.line - 1,
84-
column: position.column
98+
line: positionInNode.line - 1,
99+
column: positionInNode.column
85100
});
86101
report(node, ruleError);
87102
currentTenCount = 0;

test/max-ten-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ tester.run("max-ten", rule, {
7474
column: 26
7575
}
7676
]
77+
},
78+
{
79+
text: `複数のセンテンスがある場合。これでも、columnが、ちゃんと計算、されているはずです。`,
80+
options: {
81+
"max": 3
82+
},
83+
errors: [
84+
{
85+
message: `一つの文で"、"を3つ以上使用しています`,
86+
line: 1,
87+
column: 34
88+
}
89+
]
90+
},
91+
{
92+
text: `複数のセンテンスがあって、改行されている場合でも\n大丈夫です。これでも、lineとcolumnが、ちゃんと計算、されているはずです。`,
93+
options: {
94+
"max": 3
95+
},
96+
errors: [
97+
{
98+
message: `一つの文で"、"を3つ以上使用しています`,
99+
line: 2,
100+
column: 31
101+
}
102+
]
77103
}
78104
]
79105
});

0 commit comments

Comments
 (0)