Skip to content

Commit

Permalink
fix: not have year like 11月10日(月), should not report error
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Dec 30, 2016
1 parent 3c1ca3d commit 334e976
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/textlint-rule-date-weekday-mismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const supportedLang = [
const textIncludesNumber = (chronoDate) => {
return /[0-9-]/.test(chronoDate.text);
};
const yearMonthDayShouldKnownValues = (chronoDate) => {
if (!chronoDate.start) {
return false;
}
// year-month-day should known value
// if have not anyone, not report as error
const kV = chronoDate.start.knownValues;
return kV.year !== undefined && kV.month !== undefined && kV.day !== undefined;
};
/**
* detect lang and return language string
* @param {string[]} tags
Expand Down Expand Up @@ -56,10 +65,11 @@ function reporter(context, config = {}) {
return {
[Syntax.Str](node){
const text = getSource(node);
const chronoDate = chrono.parse(text);
const chronoDates = chrono.parse(text);
// ignore "今日" text

chronoDate.filter(textIncludesNumber).forEach(chronoDate => {
// ignore not valid data
const filteredChronoDates = chronoDates.filter(textIncludesNumber).filter(yearMonthDayShouldKnownValues);
filteredChronoDates.forEach(chronoDate => {
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
if (!lang) {
// not found lang
Expand All @@ -71,7 +81,7 @@ function reporter(context, config = {}) {
try {
$moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
} catch (error) {
console.warn("Maybe textlint-rule-date-weekday-mismatch options was wrong language.", lang);
report(node, new RuleError(`Maybe textlint-rule-date-weekday-mismatch options was wrong language. lang: ${lang}`));
// parse error is ignore
return;
}
Expand Down
5 changes: 5 additions & 0 deletions test/textlint-rule-date-weekday-mismatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ tester.run("rule", rule, {
"2016年12月29日(木曜日)",
"2016-12-29(Thursday)",
"2016-12-29(Tsu)",
// real example
`今回で192回目の更新ですが、このままのペースだと11月10日(月)に200回目となるので、
それを記念して勉強会的なイベントをやろうかと思ってます。
詳細は(まだ詳細は決まってない)以下に書いてありますが、2014年11月09日(日)を予定しています。`,
// invalid date should be ignored
"11月 25日 (火曜日) ",
// ignore relative word
Expand Down

0 comments on commit 334e976

Please sign in to comment.