-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): add warning, invariant utilities and dev-expression pl…
…ugin (#2901) * chore(project): add warning utility and dev-expression plugin * chore: address eslint violations * feat: add invariant and warn helper * chore: flip flag for useMedia warning * chore: add changeset * chore: add minified exception case for invariant * chore: update type signature for invariant * test: update test for warning helper * refactor(hooks): update useControllableState warning usage * test(warning): update test titles with flipped condition --------- Co-authored-by: Josh Black <[email protected]>
- Loading branch information
Showing
11 changed files
with
140 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': minor | ||
--- | ||
|
||
Add babel-plugin-dev-expression to transform warning calls in package bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {invariant} from '../invariant' | ||
|
||
test('throws an error when the condition is `false`', () => { | ||
expect(() => { | ||
invariant(false, 'test') | ||
}).toThrowError('test') | ||
}) | ||
|
||
test('does not throw an error when the condition is `true`', () => { | ||
expect(() => { | ||
invariant(true, 'test') | ||
}).not.toThrowError() | ||
}) | ||
|
||
test('formats arguments into error string', () => { | ||
expect(() => { | ||
invariant(false, 'test %s %s %s', 1, 2, 3) | ||
}).toThrowError('test 1 2 3') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {warning} from '../warning' | ||
|
||
test('emits a message to console.warn() when the condition is `true`', () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}) | ||
|
||
warning(true, 'test') | ||
|
||
expect(spy).toHaveBeenCalled() | ||
expect(spy).toHaveBeenCalledWith('Warning:', 'test') | ||
spy.mockRestore() | ||
}) | ||
|
||
test('does not emit a message to console.warn() when the condition is `false`', () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}) | ||
|
||
warning(false, 'test') | ||
|
||
expect(spy).not.toHaveBeenCalled() | ||
spy.mockRestore() | ||
}) | ||
|
||
test('formats arguments into warning string', () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}) | ||
|
||
warning(true, 'test %s %s %s', 1, 2, 3) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
expect(spy).toHaveBeenCalledWith('Warning:', 'test 1 2 3') | ||
spy.mockRestore() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function emptyFunction() {} | ||
|
||
// Inspired by invariant by fbjs | ||
// @see https://github.com/facebook/fbjs/blob/main/packages/fbjs/src/__forks__/invariant.js | ||
const invariant = __DEV__ | ||
? // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function invariant(condition: any, format?: string, ...args: Array<any>) { | ||
if (!condition) { | ||
let error | ||
|
||
if (format === undefined) { | ||
error = new Error( | ||
'Minified exception occurred; use the non-minified dev environment ' + | ||
'for the full error message and additional helpful warnings.', | ||
) | ||
} else { | ||
let index = 0 | ||
const message = format.replace(/%s/g, () => { | ||
return args[index++] | ||
}) | ||
|
||
error = new Error(message) | ||
error.name = 'Invariant Violation' | ||
} | ||
|
||
throw error | ||
} | ||
} | ||
: emptyFunction | ||
|
||
export {invariant} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function emptyFunction() {} | ||
|
||
const warn = __DEV__ | ||
? function warn(message: string) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Warning:', message) | ||
} | ||
: emptyFunction | ||
|
||
// Inspired by warning by fbjs | ||
// @see https://github.com/facebook/fbjs/blob/main/packages/fbjs/src/__forks__/warning.js | ||
const warning = __DEV__ | ||
? // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function warning(condition: any, format: string, ...args: Array<any>) { | ||
if (condition) { | ||
let index = 0 | ||
const message = format.replace(/%s/g, () => { | ||
return args[index++] | ||
}) | ||
warn(message) | ||
} | ||
} | ||
: emptyFunction | ||
|
||
export {warn, warning} |