-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: quiet context.fix API deprecation warnings
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import process from 'node:process'; | ||
import { ruleName } from './defaults'; | ||
|
||
// internal warning code | ||
// @see: https://github.com/stylelint/stylelint/blob/3a903800248fcccd4968e8e0dc4a76a4d8b88ff4/lib/utils/emitDeprecationWarning.mjs#L3-L11 | ||
const STYLELINT_DEPRECATION_WARNING_PREFIX = 'stylelint:'; | ||
|
||
type Warning = Error & { | ||
code: string; | ||
}; | ||
|
||
/** | ||
* Quiet all stylelint related deprecation warnings like `context.fix` or `utils.report` API. | ||
*/ | ||
export default function unsafeQuietStylelintDeprecationWarning(): void { | ||
const original = process.emitWarning; | ||
process.emitWarning = function emitWarning(...args) { | ||
const [message, options] = args; | ||
|
||
if ( | ||
message && | ||
typeof message === 'string' && | ||
message?.includes(ruleName) && | ||
options && | ||
typeof options === 'object' && | ||
options?.type === 'DeprecationWarning' && | ||
options?.code?.startsWith(STYLELINT_DEPRECATION_WARNING_PREFIX) | ||
) { | ||
return; | ||
} | ||
|
||
original.apply(process, args); | ||
}; | ||
} |
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,49 @@ | ||
import process from 'node:process'; | ||
import unsafeQuietStylelintDeprecationWarning from '../src/unsafe-quiet-stylelint-deprecation-warning'; | ||
import { ruleName } from '../src/defaults'; | ||
|
||
describe('quietContextFixDeprecationWarning', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('quiets context.fix dewprecation warnings', () => { | ||
const emitWarningSpy = jest.spyOn(process, 'emitWarning'); | ||
|
||
unsafeQuietStylelintDeprecationWarning(); | ||
|
||
process.emitWarning(ruleName, { | ||
type: 'DeprecationWarning', | ||
code: 'stylelint:005', | ||
}); | ||
|
||
expect(emitWarningSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('quiets utils.report dewprecation warnings', () => { | ||
const emitWarningSpy = jest.spyOn(process, 'emitWarning'); | ||
|
||
unsafeQuietStylelintDeprecationWarning(); | ||
|
||
process.emitWarning(ruleName, { | ||
type: 'DeprecationWarning', | ||
code: 'stylelint:007', | ||
}); | ||
|
||
expect(emitWarningSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('re-emits unrelated warnings', () => { | ||
const emitWarningSpy = jest.spyOn(process, 'emitWarning'); | ||
|
||
unsafeQuietStylelintDeprecationWarning(); | ||
|
||
process.emitWarning('foo', { | ||
type: 'DeprecationWarning', | ||
code: 'bar', | ||
}); | ||
|
||
expect(emitWarningSpy).toHaveBeenCalled(); | ||
expect(emitWarningSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |