Skip to content

Commit 6e7a33e

Browse files
committed
fix: correct range case
1 parent 71343ac commit 6e7a33e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/textlint-rule-date-weekday-mismatch.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function reporter(context, config = {}) {
5757
const text = getSource(node);
5858
const chronoDate = chrono.parse(text);
5959
// ignore "今日" text
60+
6061
chronoDate.filter(textIncludesNumber).forEach(chronoDate => {
6162
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
6263
if (!lang) {
@@ -69,27 +70,30 @@ function reporter(context, config = {}) {
6970
try {
7071
$moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
7172
} catch (error) {
73+
console.warn("Maybe textlint-rule-date-weekday-mismatch options was wrong language.", lang);
7274
// parse error is ignore
7375
return;
7476
}
7577
if (!$moment.isValid()) {
7678
return;
7779
}
78-
const slicedText = text.slice(chronoDate.index);
80+
// get (weekday)
81+
const startOfPairSymbol = chronoDate.text.length + chronoDate.index;
82+
const slicedText = text.slice(startOfPairSymbol);
7983
// (match) or (match)
80-
const match = slicedText.match(/\s*?([(])([^()]+)([)])/);
84+
const match = slicedText.match(/^(\s*?[(])([^()]+)([)])/);
8185
if (!match) {
8286
return;
8387
}
8488
const actualDateText = match[0];
8589
const actualTextAll = `${chronoDate.text}${actualDateText}`;
86-
const pairStartSymbol = match[1];// (
90+
const pairStartSymbol = match[1];// ( and padding-left
8791
const pairEndSymbol = match[3]; // )
8892
const maybeWeekdayText = match[2].trim(); // weekday
8993
// 2016年12月30日 (金曜日)
9094
// ^ ^ ^
9195
// chronoDate.index match.index pairStartSymbol.length
92-
const paddingIndex = chronoDate.index + match.index + pairStartSymbol.length;
96+
const paddingIndex = startOfPairSymbol + match.index + pairStartSymbol.length;
9397
// format http://momentjs.com/docs/#/parsing/string-format/
9498
const weekdayPatterns = [
9599
// date-format , symbols

test/textlint-rule-date-weekday-mismatch-test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,24 @@ tester.run("rule", rule, {
2828
]
2929
},
3030
{
31-
text: "2017年1月1日(火)",
32-
output: "2017年1月1日(日)",
31+
text: "2017年1月1日 (火)",
32+
output: "2017年1月1日 (日)",
33+
errors: [
34+
{
35+
message: "2017年1月1日 (火) mismatch weekday.\n2017年1月1日 (火) => 2017年1月1日 (日)",
36+
line: 1,
37+
column: 12
38+
}
39+
]
40+
},
41+
{
42+
text: "2016年12月30日〜2017年1月1日(火)",
43+
output: "2016年12月30日〜2017年1月1日(日)",
3344
errors: [
3445
{
3546
message: "2017年1月1日(火) mismatch weekday.\n2017年1月1日(火) => 2017年1月1日(日)",
3647
line: 1,
37-
column: 11
48+
column: 23
3849
}
3950
]
4051
},

0 commit comments

Comments
 (0)