Skip to content

Commit

Permalink
chore: quiet context.fix API deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyOGo committed Jan 7, 2025
1 parent 2e2951b commit 1c70f51
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"eslint-config-airbnb-typescript": "^12.0.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.7.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-tsdoc": "^0.2.7",
"husky": "^6.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import defaults, {
IgnoreValue,
RegExpString,
} from './defaults';
import unsafeQuietStylelintDeprecationWarning from './unsafe-quiet-stylelint-deprecation-warning';

unsafeQuietStylelintDeprecationWarning();

const { utils } = stylelint;
const meta: RuleMeta = {
Expand Down
34 changes: 34 additions & 0 deletions src/unsafe-quiet-stylelint-deprecation-warning.ts
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);
};
}
4 changes: 4 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"plugins": ["jest"],
"env": {
"jest/globals": true
},
"globals": {
"testRule": "readonly",
"testCustomAutoFixMessage": "readonly"
Expand Down
49 changes: 49 additions & 0 deletions test/unsafe-quiet-stylelint-deprecation-warning.js
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);
});
});

0 comments on commit 1c70f51

Please sign in to comment.