Skip to content

Commit

Permalink
Add support for *-deprecation command-line flags of Node.js (#7550)
Browse files Browse the repository at this point in the history
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
Co-authored-by: Marc G <Mouvedia@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 19, 2024
1 parent d758d63 commit a02e4dd
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-hornets-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": minor
---

Added: support for `*-deprecation` command-line flags of Node.js
16 changes: 11 additions & 5 deletions lib/__tests__/plugins.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import process from 'node:process';

import postcss from 'postcss';

Expand Down Expand Up @@ -68,14 +69,14 @@ const processorRelativeAndExtendRelative = postcss().use(
);
const processorPluginRequire = postcss().use(stylelint({ config: configPluginRequire }));

let consoleWarn;
let emitWarning;

beforeEach(() => {
consoleWarn = import.meta.jest.spyOn(console, 'warn').mockImplementation(() => {});
emitWarning = import.meta.jest.spyOn(process, 'emitWarning').mockImplementation(() => {});
});

afterEach(() => {
consoleWarn.mockRestore();
emitWarning.mockRestore();
});

it('one plugin runs', async () => {
Expand Down Expand Up @@ -386,14 +387,19 @@ describe('deprecated CommonJS plugins', () => {
it('shows the warning', async () => {
await runLint();

expect(consoleWarn).toHaveBeenCalledWith(
expect(emitWarning).toHaveBeenCalledWith(
expect.stringMatching(/CommonJS plugins are deprecated \(".+plugin-warn-about-foo\.cjs"\)/),
expect.objectContaining({
code: 'stylelint:001',
detail: 'See https://stylelint.io/migration-guide/to-16',
type: 'DeprecationWarning',
}),
);
});

it('does not show the warning when `quietDeprecationWarnings` is enabled', async () => {
await runLint({ quietDeprecationWarnings: true });

expect(consoleWarn).not.toHaveBeenCalled();
expect(emitWarning).not.toHaveBeenCalled();
});
});
14 changes: 9 additions & 5 deletions lib/__tests__/standalone-deprecations.test.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import process from 'node:process';

import readJSONFile from '../testUtils/readJSONFile.mjs';
import standalone from '../standalone.mjs';

Expand Down Expand Up @@ -41,7 +43,7 @@ describe('standalone with deprecations', () => {

describe('standalone with the `output` property deprecation', () => {
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(process, 'emitWarning').mockImplementation(() => {});
});

it('warns when using the `output` property', async () => {
Expand All @@ -54,9 +56,11 @@ describe('standalone with the `output` property deprecation', () => {
expect(result.output).toBeTruthy();
expect(result.output).toBeTruthy();

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
'`output` is deprecated. Use `report` or `code` instead.',
);
expect(process.emitWarning).toHaveBeenCalledTimes(1);
expect(process.emitWarning).toHaveBeenCalledWith('`output` is deprecated.', {
code: 'stylelint:003',
detail: 'Use `report` or `code` instead.',
type: 'DeprecationWarning',
});
});
});
20 changes: 11 additions & 9 deletions lib/__tests__/standalone-quiet-deprecation-warnings.test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { platform } from 'node:process';
import process from 'node:process';

import report from '../utils/report.mjs';
import standalone from '../standalone.mjs';
Expand Down Expand Up @@ -51,37 +51,39 @@ it('standalone silences deprecation warnings when passed --quiet-deprecation-war
});

// TODO: This test fails only on Windows for some reason. Remove the test when CommonJS is unsupported.
const describeforCommonJS = platform === 'win32' ? describe.skip : describe;
const describeforCommonJS = process.platform === 'win32' ? describe.skip : describe;

describeforCommonJS('standalone CommonJS', () => {
let standaloneCommonJS;
let options;
let consoleWarn;
let emitWarning;

beforeEach(async () => {
standaloneCommonJS = (await import('../standalone.cjs')).default;
options = {
code: 'a {}',
config: { rules: {} },
};
consoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
emitWarning = jest.spyOn(process, 'emitWarning').mockImplementation(() => {});
});

afterEach(() => {
consoleWarn.mockRestore();
emitWarning.mockRestore();
});

it('warns about CommonJS API', async () => {
await standaloneCommonJS({ ...options, quietDeprecationWarnings: false });

expect(consoleWarn).toHaveBeenCalledWith(
'The CommonJS Node.js API is deprecated. See https://stylelint.io/migration-guide/to-16',
);
expect(emitWarning).toHaveBeenCalledWith('The CommonJS Node.js API is deprecated.', {
code: 'stylelint:002',
detail: 'See https://stylelint.io/migration-guide/to-16',
type: 'DeprecationWarning',
});
});

it('suppresses the warning about CommonJS API', async () => {
await standaloneCommonJS({ ...options, quietDeprecationWarnings: true });

expect(consoleWarn).not.toHaveBeenCalled();
expect(emitWarning).not.toHaveBeenCalled();
});
});
9 changes: 6 additions & 3 deletions lib/augmentConfig.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('node:path');
const globjoin = require('globjoin');
const micromatch = require('micromatch');
const normalizePath = require('normalize-path');
const process = require('node:process');
const configurationError = require('./utils/configurationError.cjs');
const dynamicImport = require('./utils/dynamicImport.cjs');
const getModulePath = require('./utils/getModulePath.cjs');
Expand Down Expand Up @@ -324,9 +325,11 @@ async function addPluginFunctions(config, { quietDeprecationWarnings }) {

// NOTE: This '.cjs' check is limited. Some CommonJS plugins may have the '.js' extension.
if (!quietDeprecationWarnings && pluginLookup.endsWith('.cjs')) {
console.warn(
`CommonJS plugins are deprecated ("${pluginLookup}"). See https://stylelint.io/migration-guide/to-16`,
);
process.emitWarning(`CommonJS plugins are deprecated ("${pluginLookup}").`, {
type: 'DeprecationWarning',
code: 'stylelint:001',
detail: 'See https://stylelint.io/migration-guide/to-16',
});
}
} else {
pluginImport = pluginLookup;
Expand Down
9 changes: 6 additions & 3 deletions lib/augmentConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { dirname, isAbsolute } from 'node:path';
import globjoin from 'globjoin';
import micromatch from 'micromatch';
import normalizePath from 'normalize-path';
import process from 'node:process';

import configurationError from './utils/configurationError.mjs';
import dynamicImport from './utils/dynamicImport.mjs';
Expand Down Expand Up @@ -322,9 +323,11 @@ async function addPluginFunctions(config, { quietDeprecationWarnings }) {

// NOTE: This '.cjs' check is limited. Some CommonJS plugins may have the '.js' extension.
if (!quietDeprecationWarnings && pluginLookup.endsWith('.cjs')) {
console.warn(
`CommonJS plugins are deprecated ("${pluginLookup}"). See https://stylelint.io/migration-guide/to-16`,
);
process.emitWarning(`CommonJS plugins are deprecated ("${pluginLookup}").`, {
type: 'DeprecationWarning',
code: 'stylelint:001',
detail: 'See https://stylelint.io/migration-guide/to-16',
});
}
} else {
pluginImport = pluginLookup;
Expand Down
6 changes: 5 additions & 1 deletion lib/createStylelint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const STOP_DIR = IS_TEST ? process.cwd() : undefined;
function createStylelint(options = {}) {

if (!options.quietDeprecationWarnings) {
console.warn('The CommonJS Node.js API is deprecated. See https://stylelint.io/migration-guide/to-16');
process.emitWarning('The CommonJS Node.js API is deprecated.', {
type: 'DeprecationWarning',
code: 'stylelint:002',
detail: 'See https://stylelint.io/migration-guide/to-16'
});
}

const cwd = options.cwd || process.cwd();
Expand Down
9 changes: 8 additions & 1 deletion lib/prepareReturnValue.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const process = require('node:process');

/** @typedef {import('stylelint').Formatter} Formatter */
/** @typedef {import('stylelint').LintResult} StylelintResult */
/** @typedef {import('stylelint').LinterOptions["maxWarnings"]} maxWarnings */
/** @typedef {import('stylelint').LinterResult} LinterResult */


/**
* @param {StylelintResult[]} stylelintResults
* @param {maxWarnings} maxWarnings
Expand Down Expand Up @@ -39,7 +42,11 @@ function prepareReturnValue(stylelintResults, maxWarnings, formatter, cwd) {
// TODO: Deprecated. Remove in the next major version.
get output() {
if (!this._outputWarned) {
console.warn('`output` is deprecated. Use `report` or `code` instead.');
process.emitWarning('`output` is deprecated.', {
type: 'DeprecationWarning',
code: 'stylelint:003',
detail: 'Use `report` or `code` instead.',
});
this._outputWarned = true;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/prepareReturnValue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/** @typedef {import('stylelint').LinterOptions["maxWarnings"]} maxWarnings */
/** @typedef {import('stylelint').LinterResult} LinterResult */

import process from 'node:process';

/**
* @param {StylelintResult[]} stylelintResults
* @param {maxWarnings} maxWarnings
Expand Down Expand Up @@ -35,7 +37,11 @@ export default function prepareReturnValue(stylelintResults, maxWarnings, format
// TODO: Deprecated. Remove in the next major version.
get output() {
if (!this._outputWarned) {
console.warn('`output` is deprecated. Use `report` or `code` instead.');
process.emitWarning('`output` is deprecated.', {
type: 'DeprecationWarning',
code: 'stylelint:003',
detail: 'Use `report` or `code` instead.',
});
this._outputWarned = true;
}

Expand Down
6 changes: 5 additions & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ function addWarningForCommonJS() {
' // [INSERT HERE] CommonJS deprecation code',
`
if (!options.quietDeprecationWarnings) {
console.warn('The CommonJS Node.js API is deprecated. See https://stylelint.io/migration-guide/to-16');
process.emitWarning('The CommonJS Node.js API is deprecated.', {
type: 'DeprecationWarning',
code: 'stylelint:002',
detail: 'See https://stylelint.io/migration-guide/to-16'
});
}
`,
);
Expand Down

0 comments on commit a02e4dd

Please sign in to comment.