From dbdf8a5084e2c2bf03387eb130708dbf976798c1 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Feb 2020 11:53:42 -0300 Subject: [PATCH 1/7] refactor: add details to exception messages --- src/_lib/protectedTokens/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/_lib/protectedTokens/index.js b/src/_lib/protectedTokens/index.js index 4987ff9bfd..cd10bf8049 100644 --- a/src/_lib/protectedTokens/index.js +++ b/src/_lib/protectedTokens/index.js @@ -9,22 +9,22 @@ export function isProtectedWeekYearToken(token) { return protectedWeekYearTokens.indexOf(token) !== -1 } -export function throwProtectedError(token) { +export function throwProtectedError(token, format, input) { if (token === 'YYYY') { throw new RangeError( - 'Use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr' + `Use \`yyyy\` instead of \`YYYY\` for formatting years using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'YY') { throw new RangeError( - 'Use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr' + `Use \`yy\` instead of \`YY\` for formatting years using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'D') { throw new RangeError( - 'Use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr' + `Use \`d\` instead of \`D\` for formatting days of the month using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'DD') { throw new RangeError( - 'Use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr' + `Use \`dd\` instead of \`DD\` for formatting days of the month using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` ) } } From 8103b3b7665fb5088bf2605e61f2a73a7c5fb091 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Feb 2020 12:11:08 -0300 Subject: [PATCH 2/7] refator: pass args to throwProtectedError --- src/format/index.js | 4 ++-- src/parse/index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/format/index.js b/src/format/index.js index 494b155089..fe20f1d5d9 100644 --- a/src/format/index.js +++ b/src/format/index.js @@ -438,13 +438,13 @@ export default function format(dirtyDate, dirtyFormatStr, dirtyOptions) { !options.useAdditionalWeekYearTokens && isProtectedWeekYearToken(substring) ) { - throwProtectedError(substring) + throwProtectedError(substring, dirtyFormatStr, dirtyDate) } if ( !options.useAdditionalDayOfYearTokens && isProtectedDayOfYearToken(substring) ) { - throwProtectedError(substring) + throwProtectedError(substring, dirtyFormatStr, dirtyDate) } return formatter(utcDate, substring, locale.localize, formatterOptions) } diff --git a/src/parse/index.js b/src/parse/index.js index 137ccfe81c..5bf43214d1 100644 --- a/src/parse/index.js +++ b/src/parse/index.js @@ -451,13 +451,13 @@ export default function parse( !options.useAdditionalWeekYearTokens && isProtectedWeekYearToken(token) ) { - throwProtectedError(token) + throwProtectedError(token, formatString, dirtyDateString) } if ( !options.useAdditionalDayOfYearTokens && isProtectedDayOfYearToken(token) ) { - throwProtectedError(token) + throwProtectedError(token, formatString, dirtyDateString) } var firstCharacter = token[0] From 40b91bf25fdfa38a9864573dafbec9750fd16706 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Feb 2020 12:12:23 -0300 Subject: [PATCH 3/7] test: update tests --- src/format/test.js | 8 ++++---- src/parse/test.js | 28 ++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/format/test.js b/src/format/test.js index d5655712d1..064f2aa30f 100644 --- a/src/format/test.js +++ b/src/format/test.js @@ -743,7 +743,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `d` instead of `D` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /Use `d` instead of `D` for formatting days of the month using `yyyy-MM-D` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -759,7 +759,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `dd` instead of `DD` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /Use `dd` instead of `DD` for formatting days of the month using `yyyy-MM-DD` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -775,7 +775,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yy` instead of `YY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /Use `yy` instead of `YY` for formatting years using `YY-MM-dd` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -791,7 +791,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yyyy` instead of `YYYY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /Use `yyyy` instead of `YYYY` for formatting years using `YYYY-MM-dd` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ ) }) diff --git a/src/parse/test.js b/src/parse/test.js index 68c897d2c6..01241d1600 100644 --- a/src/parse/test.js +++ b/src/parse/test.js @@ -1067,7 +1067,9 @@ describe('parse', function() { }) it('allows to specify which day is the first day of the week', function() { - var result = parse('7th', 'eo', referenceDate, { weekStartsOn: /* Fri */ 5 }) + var result = parse('7th', 'eo', referenceDate, { + weekStartsOn: /* Fri */ 5 + }) assert.deepEqual(result, new Date(1986, 3 /* Apr */, 10)) }) @@ -1147,7 +1149,9 @@ describe('parse', function() { }) it('allows to specify which day is the first day of the week', function() { - var result = parse('7th', 'co', referenceDate, { weekStartsOn: /* Fri */ 5 }) + var result = parse('7th', 'co', referenceDate, { + weekStartsOn: /* Fri */ 5 + }) assert.deepEqual(result, new Date(1986, 3 /* Apr */, 10)) }) @@ -2008,7 +2012,11 @@ describe('parse', function() { }) it('ISO week-numbering date', function() { - var result = parse('2016W474T153005', "RRRR'W'IIi'T'HHmmss", referenceDate) + var result = parse( + '2016W474T153005', + "RRRR'W'IIi'T'HHmmss", + referenceDate + ) assert.deepEqual(result, new Date(2016, 10 /* Nov */, 24, 15, 30, 5, 0)) }) @@ -2032,7 +2040,11 @@ describe('parse', function() { }) it('middle-endian', function() { - var result = parse('5 a.m. 07/02/2016', 'h aaaa MM/dd/yyyy', referenceDate) + var result = parse( + '5 a.m. 07/02/2016', + 'h aaaa MM/dd/yyyy', + referenceDate + ) assert.deepEqual(result, new Date(2016, 6 /* Jul */, 2, 5, 0, 0, 0)) }) @@ -2430,7 +2442,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `d` instead of `D` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /Use `d` instead of `D` for formatting days of the month using `yyyy D` to the input `2016 5`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2446,7 +2458,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `dd` instead of `DD` for formatting days of the month; see: https:\/\/git.io\/fxCyr/ + /Use `dd` instead of `DD` for formatting days of the month using `yyyy DD` to the input `2016 05`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2462,7 +2474,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yy` instead of `YY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /Use `yy` instead of `YY` for formatting years using `YY w` to the input `16 1`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2478,7 +2490,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yyyy` instead of `YYYY` for formatting years; see: https:\/\/git.io\/fxCyr/ + /Use `yyyy` instead of `YYYY` for formatting years using `YYYY w` to the input `2016 1`; see: https:\/\/git.io\/fxCyr/ ) }) From 144140ab22a4849de9f7b29976fb7c17bd66feb1 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Feb 2020 12:12:32 -0300 Subject: [PATCH 4/7] docs: update docs --- CHANGELOG.md | 1 + src/format/index.js | 8 ++++---- src/index.js.flow | 2 +- src/parse/index.js | 8 ++++---- typings.d.ts | 6 +++--- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9271eefe8c..8dcb4a71dc 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This change log follows the format documented in [Keep a CHANGELOG]. - [Added `weeks` to `Duration`](https://github.com/date-fns/date-fns/pull/1592). - [Added `weeks` support to `add` and `sub`](https://github.com/date-fns/date-fns/pull/1592). +- [Added details message in `throwProtectedError`](https://github.com/date-fns/date-fns/pull/1592). ## [2.9.0] - 2020-01-08 diff --git a/src/format/index.js b/src/format/index.js index fe20f1d5d9..3fb4c19815 100644 --- a/src/format/index.js +++ b/src/format/index.js @@ -319,10 +319,10 @@ var unescapedLatinCharacterRegExp = /[a-zA-Z]/ * @throws {RangeError} `options.locale` must contain `formatLong` property * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7 - * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr - * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr + * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr * @throws {RangeError} format string contains an unescaped latin alphabet character * * @example diff --git a/src/index.js.flow b/src/index.js.flow index 9c0198558b..624fd313e2 100644 --- a/src/index.js.flow +++ b/src/index.js.flow @@ -539,7 +539,7 @@ declare module.exports: { parse: ( dateString: string, formatString: string, - backupDate: Date | number, + referenceDate: Date | number, options?: { locale?: Locale, weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, diff --git a/src/parse/index.js b/src/parse/index.js index 5bf43214d1..93b1037d2e 100644 --- a/src/parse/index.js +++ b/src/parse/index.js @@ -336,10 +336,10 @@ var unescapedLatinCharacterRegExp = /[a-zA-Z]/ * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7 * @throws {RangeError} `options.locale` must contain `match` property - * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr - * @throws {RangeError} use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr - * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr + * @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr + * @throws {RangeError} use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: https://git.io/fxCyr * @throws {RangeError} format string contains an unescaped latin alphabet character * * @example diff --git a/typings.d.ts b/typings.d.ts index fd1940b97b..bdaf50d763 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -744,7 +744,7 @@ declare module 'date-fns' { function parse( dateString: string, formatString: string, - backupDate: Date | number, + referenceDate: Date | number, options?: { locale?: Locale weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 @@ -8273,7 +8273,7 @@ declare module 'date-fns/esm' { function parse( dateString: string, formatString: string, - backupDate: Date | number, + referenceDate: Date | number, options?: { locale?: Locale weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 @@ -18102,7 +18102,7 @@ interface dateFns { parse( dateString: string, formatString: string, - backupDate: Date | number, + referenceDate: Date | number, options?: { locale?: Locale weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 From 97b7dec6206cf6f9acf614230a50898d893af787 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Feb 2020 14:40:41 -0300 Subject: [PATCH 5/7] test: fix format test regex --- src/format/test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/format/test.js b/src/format/test.js index 064f2aa30f..a31a5277e9 100644 --- a/src/format/test.js +++ b/src/format/test.js @@ -743,7 +743,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `d` instead of `D` for formatting days of the month using `yyyy-MM-D` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ + /(Use `d` instead of `D` for formatting days of the month using `yyyy-MM-D` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -759,7 +759,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `dd` instead of `DD` for formatting days of the month using `yyyy-MM-DD` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ + /(Use `dd` instead of `DD` for formatting days of the month using `yyyy-MM-DD` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -775,7 +775,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yy` instead of `YY` for formatting years using `YY-MM-dd` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ + /(Use `yy` instead of `YY` for formatting days of the month using `YY-MM-dd` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -791,7 +791,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yyyy` instead of `YYYY` for formatting years using `YYYY-MM-dd` to the input `Fri Apr 04 1986 10:32:55 GMT-0300 \(Brasilia Standard Time\)`; see: https:\/\/git.io\/fxCyr/ + /(Use `yyyy` instead of `YYYY` for formatting days of the month using `YYYY-MM-dd` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g ) }) From e123f9bd12d2b088a8e836729347cd58c4faf2f2 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Feb 2020 22:19:23 -0300 Subject: [PATCH 6/7] refactor: improve messages --- src/_lib/protectedTokens/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_lib/protectedTokens/index.js b/src/_lib/protectedTokens/index.js index cd10bf8049..c62313f1b8 100644 --- a/src/_lib/protectedTokens/index.js +++ b/src/_lib/protectedTokens/index.js @@ -12,19 +12,19 @@ export function isProtectedWeekYearToken(token) { export function throwProtectedError(token, format, input) { if (token === 'YYYY') { throw new RangeError( - `Use \`yyyy\` instead of \`YYYY\` for formatting years using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` + `Use \`yyyy\` instead of \`YYYY\` (in \`${format}\`) for formatting years to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'YY') { throw new RangeError( - `Use \`yy\` instead of \`YY\` for formatting years using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` + `Use \`yy\` instead of \`YY\` (in \`${format}\`) for formatting years to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'D') { throw new RangeError( - `Use \`d\` instead of \`D\` for formatting days of the month using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` + `Use \`d\` instead of \`D\` (in \`${format}\`) for formatting days of the month to the input \`${input}\`; see: https://git.io/fxCyr` ) } else if (token === 'DD') { throw new RangeError( - `Use \`dd\` instead of \`DD\` for formatting days of the month using \`${format}\` to the input \`${input}\`; see: https://git.io/fxCyr` + `Use \`dd\` instead of \`DD\` (in \`${format}\`) for formatting days of the month to the input \`${input}\`; see: https://git.io/fxCyr` ) } } From f68ecfe0c7c096c19b57a5b332cc8797d82eafdf Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Feb 2020 22:19:32 -0300 Subject: [PATCH 7/7] test: update tests --- src/format/test.js | 8 ++++---- src/parse/test.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/format/test.js b/src/format/test.js index a31a5277e9..2c85c50f64 100644 --- a/src/format/test.js +++ b/src/format/test.js @@ -743,7 +743,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /(Use `d` instead of `D` for formatting days of the month using `yyyy-MM-D` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g + /(Use `d` instead of `D` \(in `yyyy-MM-D`\) for formatting days of the month to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -759,7 +759,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /(Use `dd` instead of `DD` for formatting days of the month using `yyyy-MM-DD` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g + /(Use `dd` instead of `DD` \(in `yyyy-MM-DD`\) for formatting days of the month to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -775,7 +775,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /(Use `yy` instead of `YY` for formatting days of the month using `YY-MM-dd` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g + /(Use `yy` instead of `YY` \(in `YY-MM-dd`\) for formatting years to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) @@ -791,7 +791,7 @@ describe('format', function() { assert.throws(block, RangeError) assert.throws( block, - /(Use `yyyy` instead of `YYYY` for formatting days of the month using `YYYY-MM-dd` to the input `Fri Apr 04 1986 10:32:55)|(`; see: https:\/\/git.io\/fxCyr)/g + /(Use `yyyy` instead of `YYYY` \(in `YYYY-MM-dd`\) for formatting years to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g ) }) diff --git a/src/parse/test.js b/src/parse/test.js index 01241d1600..8d07c43e55 100644 --- a/src/parse/test.js +++ b/src/parse/test.js @@ -2442,7 +2442,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `d` instead of `D` for formatting days of the month using `yyyy D` to the input `2016 5`; see: https:\/\/git.io\/fxCyr/ + /Use `d` instead of `D` \(in `yyyy D`\) for formatting days of the month to the input `2016 5`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2458,7 +2458,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `dd` instead of `DD` for formatting days of the month using `yyyy DD` to the input `2016 05`; see: https:\/\/git.io\/fxCyr/ + /Use `dd` instead of `DD` \(in `yyyy DD`\) for formatting days of the month to the input `2016 05`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2474,7 +2474,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yy` instead of `YY` for formatting years using `YY w` to the input `16 1`; see: https:\/\/git.io\/fxCyr/ + /Use `yy` instead of `YY` \(in `YY w`\) for formatting years to the input `16 1`; see: https:\/\/git.io\/fxCyr/ ) }) @@ -2490,7 +2490,7 @@ describe('parse', function() { assert.throws(block, RangeError) assert.throws( block, - /Use `yyyy` instead of `YYYY` for formatting years using `YYYY w` to the input `2016 1`; see: https:\/\/git.io\/fxCyr/ + /Use `yyyy` instead of `YYYY` \(in `YYYY w`\) for formatting years to the input `2016 1`; see: https:\/\/git.io\/fxCyr/ ) })