Skip to content

Commit da86588

Browse files
committed
add: currentYear option and remove sinon dependency
1 parent 7402db3 commit da86588

File tree

5 files changed

+34
-72
lines changed

5 files changed

+34
-72
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ But, You can specify `2016-12-30` is `ja-JP` text by options
9292
- If true, when the year is missing in the date string (e.g. `4月23日(月)`), the current year will be automatically added for validation.
9393
- This is useful for documents that often omit the year in dates.
9494

95+
Example:
96+
9597
```json
9698
{
9799
"rules": {
@@ -102,6 +104,29 @@ But, You can specify `2016-12-30` is `ja-JP` text by options
102104
}
103105
```
104106

107+
If the text contains `4月23日(水)`, and the current year is 2025, it will be interpreted as `2025年4月23日(水)` for the weekday check.
108+
109+
- `currentYear`: number
110+
- Default: the current year (from system date)
111+
- If specified, this value will be used as the year when supplementing missing years in date strings (used only when `useCurrentYearIfMissing` is true).
112+
- This is useful for testing or for documents that should always use a specific year for validation.
113+
114+
Example (using both options):
115+
116+
```json
117+
{
118+
"rules": {
119+
"date-weekday-mismatch": {
120+
"useCurrentYearIfMissing": true,
121+
"currentYear": 2025
122+
}
123+
}
124+
}
125+
```
126+
127+
If the text contains `4月23日(水)`, it will always be interpreted as `2025年4月23日(水)` for the weekday check, regardless of the actual system year.
128+
129+
105130
language format following ISO 639-1.
106131

107132
e.g.) `en-US`, `en`, `ja` etc..

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
},
2828
"homepage": "https://github.com/textlint-rule/textlint-rule-date-weekday-mismatch",
2929
"devDependencies": {
30-
"sinon": "^20.0.0",
3130
"textlint-scripts": "^12.0.2"
3231
},
3332
"keywords": [

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const addYearToDateText = (dateText, year, lang) => {
8181
function reporter(context, config = {}) {
8282
const preferLang = config.lang;
8383
const useCurrentYearIfMissing = config.useCurrentYearIfMissing;
84+
const currentYearOption = config.currentYear;
8485
const {Syntax, RuleError, report, fixer, getSource} = context;
8586
if (typeof Intl === "undefined") {
8687
throw new Error("Not support your Node.js/browser. should be use latest version.");
@@ -92,7 +93,8 @@ function reporter(context, config = {}) {
9293
let chronoDates = chrono.parse(text);
9394
// Add current year if missing and option is enabled
9495
if (useCurrentYearIfMissing) {
95-
const currentYear = (new Date()).getFullYear();
96+
// Use currentYear option if provided, otherwise use system year
97+
const currentYear = currentYearOption ?? (new Date()).getFullYear();
9698
chronoDates.forEach(chronoDate => {
9799
// If year is not specified in the parsed result
98100
if (
@@ -104,7 +106,7 @@ function reporter(context, config = {}) {
104106
if (!lang) {
105107
return;
106108
}
107-
// Re-parse the text with the year added
109+
// Re-parse the text with the year added (using currentYear)
108110
const newText = addYearToDateText(chronoDate.text, currentYear, lang);
109111
const reparsed = chrono.parse(newText, undefined, {forwardDate: true});
110112
// If reparsed successfully, update knownValues with year/month/day

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
import TextLintTester from "textlint-tester";
2-
import sinon from "sinon";
32

43
const tester = new TextLintTester();
54
// rule
65
const rule = require("../src/textlint-rule-date-weekday-mismatch");
76

8-
// Mock: fix current date to 2025-04-23 to test `useCurrentYearIfMissing` option
9-
let clock;
10-
before(() => {
11-
clock = sinon.useFakeTimers(new Date(2025, 4, 23).getTime());
12-
});
13-
after(() => {
14-
clock.restore();
15-
});
16-
177
// ruleName, rule, { valid, invalid }
188
tester.run("rule", rule, {
199
valid: [
@@ -33,12 +23,12 @@ tester.run("rule", rule, {
3323
// useCurrentYearIfMissing option: valid
3424
{
3525
text: "4月23日(水)",
36-
options: { useCurrentYearIfMissing: true, lang: "ja" },
26+
options: { useCurrentYearIfMissing: true, currentYear: 2025, lang: "ja" },
3727
// 2025年4月23日は水曜日
3828
},
3929
{
4030
text: "4/23(Wed)",
41-
options: { useCurrentYearIfMissing: true, lang: "en" },
31+
options: { useCurrentYearIfMissing: true, currentYear: 2025, lang: "en" },
4232
// 2025-04-23 is Wednesday
4333
},
4434
],
@@ -148,7 +138,7 @@ tester.run("rule", rule, {
148138
{
149139
text: "4月23日(金)",
150140
output: "4月23日(水)",
151-
options: { useCurrentYearIfMissing: true, lang: "ja" },
141+
options: { useCurrentYearIfMissing: true, currentYear: 2025, lang: "ja" },
152142
errors: [
153143
{
154144
message: "4月23日(金) mismatch weekday.\n4月23日(金) => 4月23日(水)",
@@ -160,7 +150,7 @@ tester.run("rule", rule, {
160150
{
161151
text: "4/23(Fri)",
162152
output: "4/23(Wed)",
163-
options: { useCurrentYearIfMissing: true, lang: "en" },
153+
options: { useCurrentYearIfMissing: true, currentYear: 2025, lang: "en" },
164154
errors: [
165155
{
166156
message: "4/23(Fri) mismatch weekday.\n4/23(Fri) => 4/23(Wed)",

yarn.lock

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -993,29 +993,6 @@
993993
readdirp "^2.2.1"
994994
upath "^1.1.1"
995995

996-
"@sinonjs/commons@^3.0.1":
997-
version "3.0.1"
998-
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-3.0.1.tgz#1029357e44ca901a615585f6d27738dbc89084cd"
999-
integrity sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==
1000-
dependencies:
1001-
type-detect "4.0.8"
1002-
1003-
"@sinonjs/fake-timers@^13.0.5":
1004-
version "13.0.5"
1005-
resolved "https://registry.yarnpkg.com/@sinonjs/fake-timers/-/fake-timers-13.0.5.tgz#36b9dbc21ad5546486ea9173d6bea063eb1717d5"
1006-
integrity sha512-36/hTbH2uaWuGVERyC6da9YwGWnzUZXuPro/F2LfsdOsLnCojz/iSH8MxUt/FD2S5XBSVPhmArFUXcpCQ2Hkiw==
1007-
dependencies:
1008-
"@sinonjs/commons" "^3.0.1"
1009-
1010-
"@sinonjs/samsam@^8.0.1":
1011-
version "8.0.2"
1012-
resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-8.0.2.tgz#e4386bf668ff36c95949e55a38dc5f5892fc2689"
1013-
integrity sha512-v46t/fwnhejRSFTGqbpn9u+LQ9xJDse10gNnPgAcxgdoCDMXj/G2asWAC/8Qs+BAZDicX+MNZouXT1A7c83kVw==
1014-
dependencies:
1015-
"@sinonjs/commons" "^3.0.1"
1016-
lodash.get "^4.4.2"
1017-
type-detect "^4.1.0"
1018-
1019996
"@textlint/ast-node-types@^12.0.0":
1020997
version "12.0.0"
1021998
resolved "https://registry.yarnpkg.com/@textlint/ast-node-types/-/ast-node-types-12.0.0.tgz#23bd683f9fc04209ae28bff72954c8aa67c6b1ca"
@@ -1808,11 +1785,6 @@ diff@^4.0.2:
18081785
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
18091786
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
18101787

1811-
diff@^7.0.0:
1812-
version "7.0.0"
1813-
resolved "https://registry.yarnpkg.com/diff/-/diff-7.0.0.tgz#3fb34d387cd76d803f6eebea67b921dab0182a9a"
1814-
integrity sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==
1815-
18161788
ejs@^2.4.1:
18171789
version "2.7.1"
18181790
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.7.1.tgz#5b5ab57f718b79d4aca9254457afecd36fa80228"
@@ -2593,11 +2565,6 @@ lodash.debounce@^4.0.8:
25932565
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
25942566
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=
25952567

2596-
lodash.get@^4.4.2:
2597-
version "4.4.2"
2598-
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
2599-
integrity sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==
2600-
26012568
lodash.truncate@^4.4.2:
26022569
version "4.4.2"
26032570
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
@@ -3647,17 +3614,6 @@ signal-exit@^3.0.0:
36473614
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
36483615
integrity sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=
36493616

3650-
sinon@^20.0.0:
3651-
version "20.0.0"
3652-
resolved "https://registry.yarnpkg.com/sinon/-/sinon-20.0.0.tgz#4b653468735f7152ba694d05498c2b5d024ab006"
3653-
integrity sha512-+FXOAbdnj94AQIxH0w1v8gzNxkawVvNqE3jUzRLptR71Oykeu2RrQXXl/VQjKay+Qnh73fDt/oDfMo6xMeDQbQ==
3654-
dependencies:
3655-
"@sinonjs/commons" "^3.0.1"
3656-
"@sinonjs/fake-timers" "^13.0.5"
3657-
"@sinonjs/samsam" "^8.0.1"
3658-
diff "^7.0.0"
3659-
supports-color "^7.2.0"
3660-
36613617
slash@^2.0.0:
36623618
version "2.0.0"
36633619
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
@@ -3886,7 +3842,7 @@ supports-color@^5.3.0:
38863842
dependencies:
38873843
has-flag "^3.0.0"
38883844

3889-
supports-color@^7.1.0, supports-color@^7.2.0:
3845+
supports-color@^7.1.0:
38903846
version "7.2.0"
38913847
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.2.0.tgz#1b7dcdcb32b8138801b3e478ba6a51caa89648da"
38923848
integrity sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==
@@ -4034,16 +3990,6 @@ type-check@^0.4.0, type-check@~0.4.0:
40343990
dependencies:
40353991
prelude-ls "^1.2.1"
40363992

4037-
4038-
version "4.0.8"
4039-
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
4040-
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==
4041-
4042-
type-detect@^4.1.0:
4043-
version "4.1.0"
4044-
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.1.0.tgz#deb2453e8f08dcae7ae98c626b13dddb0155906c"
4045-
integrity sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==
4046-
40473993
unicode-canonical-property-names-ecmascript@^1.0.4:
40483994
version "1.0.4"
40493995
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"

0 commit comments

Comments
 (0)