Skip to content

Commit

Permalink
Improve the message of protected tokens error (#1641) (closes #1230)
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel <gabriel.belgamo@codeminer.com.br>
  • Loading branch information
belgamo and Gabriel committed Jul 17, 2020
1 parent 5ae81d3 commit 02f1ead
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -105,6 +105,7 @@ Kudos to [@oakhan3](https://github.com/oakhan3), [@Mukhammadali](https://github.

- [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

Expand Down
10 changes: 5 additions & 5 deletions src/_lib/protectedTokens/index.js
Expand Up @@ -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\` (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; 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; 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; 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`
)
}
}
12 changes: 6 additions & 6 deletions src/format/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions src/format/test.js
Expand Up @@ -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` \(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
)
})

Expand All @@ -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` \(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
)
})

Expand All @@ -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` \(in `YY-MM-dd`\) for formatting years to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g
)
})

Expand All @@ -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` \(in `YYYY-MM-dd`\) for formatting years to the input `Fri Apr 04 1986 10:32:55).*(`; see: https:\/\/git.io\/fxCyr)/g
)
})

Expand Down
12 changes: 6 additions & 6 deletions src/parse/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions src/parse/test.js
Expand Up @@ -2464,7 +2464,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` \(in `yyyy D`\) for formatting days of the month to the input `2016 5`; see: https:\/\/git.io\/fxCyr/
)
})

Expand All @@ -2480,7 +2480,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` \(in `yyyy DD`\) for formatting days of the month to the input `2016 05`; see: https:\/\/git.io\/fxCyr/
)
})

Expand All @@ -2496,7 +2496,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` \(in `YY w`\) for formatting years to the input `16 1`; see: https:\/\/git.io\/fxCyr/
)
})

Expand All @@ -2512,7 +2512,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` \(in `YYYY w`\) for formatting years to the input `2016 1`; see: https:\/\/git.io\/fxCyr/
)
})

Expand Down

0 comments on commit 02f1ead

Please sign in to comment.