Skip to content

Commit

Permalink
Fix false positive CJS deprecation warning for dual-package plugins (#…
Browse files Browse the repository at this point in the history
…7532)

Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
JounQin and ybiquitous committed Mar 8, 2024
1 parent d2c8d0d commit fc88e99
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-needles-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: false positive CJS deprecation warning for dual-package plugins
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
!lib/__tests__/fixtures/node_modules
!lib/utils/__tests__/fixtures/node_modules
*.log
.coverage
.eslintcache
Expand Down
10 changes: 5 additions & 5 deletions lib/augmentConfig.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const path = require('node:path');
const globjoin = require('globjoin');
const micromatch = require('micromatch');
const normalizePath = require('normalize-path');
Expand All @@ -21,7 +21,7 @@ const normalizeAllRuleSettings = require('./normalizeAllRuleSettings.cjs');
* @returns {string}
*/
function absolutizeGlob(glob, basedir) {
const result = node_path.isAbsolute(glob.replace(/^!/, '')) ? glob : globjoin(basedir, glob);
const result = path.isAbsolute(glob.replace(/^!/, '')) ? glob : globjoin(basedir, glob);

// Glob patterns for micromatch should be in POSIX-style
return normalizePath(result);
Expand Down Expand Up @@ -83,7 +83,7 @@ function augmentConfigExtended(cwd) {
return null;
}

const configDir = node_path.dirname(cosmiconfigResult.filepath || '');
const configDir = path.dirname(cosmiconfigResult.filepath || '');
const { config } = cosmiconfigResult;

const augmentedConfig = absolutizePaths(config, configDir, cwd);
Expand All @@ -109,7 +109,7 @@ async function augmentConfigFull(stylelint, filePath, cosmiconfigResult) {
const config = cosmiconfigResult.config;
const filepath = cosmiconfigResult.filepath;

const configDir = stylelint._options.configBasedir || node_path.dirname(filepath || '');
const configDir = stylelint._options.configBasedir || path.dirname(filepath || '');

let augmentedConfig = await augmentConfigBasic(
stylelint,
Expand Down Expand Up @@ -191,7 +191,7 @@ async function extendConfig(stylelint, config, configDir, rootConfigDir, filePat

if (extendResult) {
let extendResultConfig = extendResult.config;
const extendConfigDir = node_path.dirname(extendResult.filepath || '');
const extendConfigDir = path.dirname(extendResult.filepath || '');

extendResultConfig = await augmentConfigBasic(
stylelint,
Expand Down
4 changes: 2 additions & 2 deletions lib/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import picocolors from 'picocolors';
const { dim, red } = picocolors;

import meow from 'meow';
import resolveFrom from 'resolve-from';

import { isBoolean, isNumber, isObject, isPlainObject, isString } from './utils/validateTypes.mjs';
import checkInvalidCLIOptions from './utils/checkInvalidCLIOptions.mjs';
import dynamicImport from './utils/dynamicImport.mjs';
import printConfig from './printConfig.mjs';
import resolveSilent from './utils/resolveSilent.mjs';
import standalone from './standalone.mjs';
import writeOutputFile from './writeOutputFile.mjs';

Expand Down Expand Up @@ -400,7 +400,7 @@ export default async function main(argv) {
// c. relative path relative to `process.cwd()`.
// If none of the above work, we'll try a relative path starting
// in `process.cwd()`.
options.configFile = resolveFrom.silent(cwd, configFile) || join(cwd, configFile);
options.configFile = resolveSilent(cwd, configFile) || join(cwd, configFile);
}

if (isString(configBasedir)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/getConfigForFile.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const path = require('node:path');
const process = require('node:process');
const cosmiconfig = require('cosmiconfig');
const augmentConfig = require('./augmentConfig.cjs');
Expand Down Expand Up @@ -42,7 +42,7 @@ async function getConfigForFile(
config: optionsConfig,
// Add the extra path part so that we can get the directory without being
// confused
filepath: node_path.join(cwd, 'argument-config'),
filepath: path.join(cwd, 'argument-config'),
});

stylelint._specifiedConfigCache.set(optionsConfig, augmentedResult);
Expand Down
4 changes: 2 additions & 2 deletions lib/lintSource.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const path = require('node:path');
const descriptionlessDisables = require('./descriptionlessDisables.cjs');
const getConfigForFile = require('./getConfigForFile.cjs');
const getPostcssResult = require('./getPostcssResult.cjs');
Expand Down Expand Up @@ -35,7 +35,7 @@ async function lintSource(stylelint, options = {}) {

const inputFilePath = isCodeNotFile ? options.codeFilename : options.filePath;

if (inputFilePath !== undefined && !node_path.isAbsolute(inputFilePath)) {
if (inputFilePath !== undefined && !path.isAbsolute(inputFilePath)) {
if (isCodeNotFile) {
return Promise.reject(new Error('codeFilename must be an absolute path'));
}
Expand Down
6 changes: 3 additions & 3 deletions lib/postcssPlugin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const path = require('node:path');
const process = require('node:process');
const createStylelint = require('./createStylelint.cjs');
const validateTypes = require('./utils/validateTypes.cjs');
Expand Down Expand Up @@ -31,8 +31,8 @@ function postcssPlugin(options = {}) {
async Once(root, { result }) {
let filePath = root.source && root.source.input.file;

if (filePath && !node_path.isAbsolute(filePath)) {
filePath = node_path.join(cwd, filePath);
if (filePath && !path.isAbsolute(filePath)) {
filePath = path.join(cwd, filePath);
}

await lintSource(stylelint, {
Expand Down
4 changes: 2 additions & 2 deletions lib/resolveConfig.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const path = require('node:path');
const process = require('node:process');
const createStylelint = require('./createStylelint.cjs');
const getConfigForFile = require('./getConfigForFile.cjs');
Expand Down Expand Up @@ -30,7 +30,7 @@ async function resolveConfig(
cwd,
});

const absoluteFilePath = !node_path.isAbsolute(filePath) ? node_path.join(cwd, filePath) : node_path.normalize(filePath);
const absoluteFilePath = !path.isAbsolute(filePath) ? path.join(cwd, filePath) : path.normalize(filePath);

const configSearchPath = stylelint._options.configFile || absoluteFilePath;

Expand Down
18 changes: 9 additions & 9 deletions lib/standalone.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const node_path = require('node:path');
const path = require('node:path');
const fs = require('node:fs');
const process = require('node:process');
const createDebug = require('debug');
Expand Down Expand Up @@ -113,14 +113,14 @@ async function standalone({
validateTypes.assertString(code);

const absoluteCodeFilename =
codeFilename !== undefined && !node_path.isAbsolute(codeFilename)
? node_path.join(cwd, codeFilename)
codeFilename !== undefined && !path.isAbsolute(codeFilename)
? path.join(cwd, codeFilename)
: codeFilename;

// if file is ignored, return nothing
if (
absoluteCodeFilename &&
!filterFilePaths(ignorer, [node_path.relative(cwd, absoluteCodeFilename)]).length
!filterFilePaths(ignorer, [path.relative(cwd, absoluteCodeFilename)]).length
) {
return prepareReturnValue([], maxWarnings, formatterFunction, cwd);
}
Expand Down Expand Up @@ -158,7 +158,7 @@ async function standalone({

let fileList = [files].flat().map((entry) => {
const globCWD = (globbyOptions && globbyOptions.cwd) || cwd;
const absolutePath = !node_path.isAbsolute(entry) ? node_path.join(globCWD, entry) : node_path.normalize(entry);
const absolutePath = !path.isAbsolute(entry) ? path.join(globCWD, entry) : path.normalize(entry);

if (fs.existsSync(absolutePath)) {
// This path points to a file. Return an escaped path to avoid globbing
Expand Down Expand Up @@ -195,16 +195,16 @@ async function standalone({
// The ignorer filter needs to check paths relative to cwd
filePaths = filterFilePaths(
ignorer,
filePaths.map((p) => node_path.relative(globCWD, p)),
filePaths.map((p) => path.relative(globCWD, p)),
);

let stylelintResults;

if (filePaths.length) {
let absoluteFilePaths = filePaths.map((filePath) => {
const absoluteFilepath = !node_path.isAbsolute(filePath)
? node_path.join(globCWD, filePath)
: node_path.normalize(filePath);
const absoluteFilepath = !path.isAbsolute(filePath)
? path.join(globCWD, filePath)
: path.normalize(filePath);

return absoluteFilepath;
});
Expand Down
Empty file.
Empty file.

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

13 changes: 13 additions & 0 deletions lib/utils/__tests__/resolveSilent.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from 'node:path';

import { fileURLToPath } from 'node:url';

import resolveSilent from '../resolveSilent.mjs';

const fixturesPath = fileURLToPath(new URL('./fixtures', import.meta.url));

it('should resolve ESM over commonjs for dual package', () => {
expect(resolveSilent(fixturesPath, '@stylelint/dual-package')).toBe(
path.resolve(fixturesPath, 'node_modules/@stylelint/dual-package/index.mjs'),
);
});
8 changes: 4 additions & 4 deletions lib/utils/getModulePath.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

const process = require('node:process');
const globalModules = require('global-modules');
const resolveFrom = require('resolve-from');
const configurationError = require('./configurationError.cjs');
const resolveSilent = require('./resolveSilent.cjs');

/**
* @param {string} basedir
Expand All @@ -17,14 +17,14 @@ function getModulePath(basedir, lookup, cwd = process.cwd()) {
// 1. Try to resolve from the provided directory
// 2. Try to resolve from `cwd` or `process.cwd()`
// 3. Try to resolve from global `node_modules` directory
let path = resolveFrom.silent(basedir, lookup);
let path = resolveSilent(basedir, lookup);

if (!path) {
path = resolveFrom.silent(cwd, lookup);
path = resolveSilent(cwd, lookup);
}

if (!path) {
path = resolveFrom.silent(globalModules, lookup);
path = resolveSilent(globalModules, lookup);
}

if (!path) {
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/getModulePath.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import process from 'node:process';

import globalModules from 'global-modules';
import resolveFrom from 'resolve-from';

import configurationError from './configurationError.mjs';
import resolveSilent from './resolveSilent.mjs';

/**
* @param {string} basedir
Expand All @@ -15,14 +15,14 @@ export default function getModulePath(basedir, lookup, cwd = process.cwd()) {
// 1. Try to resolve from the provided directory
// 2. Try to resolve from `cwd` or `process.cwd()`
// 3. Try to resolve from global `node_modules` directory
let path = resolveFrom.silent(basedir, lookup);
let path = resolveSilent(basedir, lookup);

if (!path) {
path = resolveFrom.silent(cwd, lookup);
path = resolveSilent(cwd, lookup);
}

if (!path) {
path = resolveFrom.silent(globalModules, lookup);
path = resolveSilent(globalModules, lookup);
}

if (!path) {
Expand Down
60 changes: 60 additions & 0 deletions lib/utils/resolveSilent.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// NOTICE: This file is generated by Rollup. To modify it,
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const fs = require('node:fs');
const path = require('node:path');
const node_url = require('node:url');
const importMetaResolve = require('@dual-bundle/import-meta-resolve');
const resolveFrom = require('resolve-from');

/**
* TODO: These suffixes are here for commonjs compatibility reason, we should remove these codes after migrating to pure ESM, because in ESM accurate paths are required
*/

const pathSuffixes = ['', '.js', '.json', `${path.sep}index.js`, `${path.sep}index.json`];

const specifierSuffixes = ['', '.js', '.json', '/index.js', '/index.json'];

/**
* @param {string} parent
* @param {string} lookup
* @return {string | undefined}
*/
function resolveSilent(parent, lookup) {
if (path.isAbsolute(lookup)) {
for (const suffix of pathSuffixes) {
const filename = lookup + suffix;

if (fs.existsSync(filename)) {
return filename;
}
}

return;
}

const base = node_url.pathToFileURL(path.resolve(parent, 'noop.js')).toString();

for (const suffix of specifierSuffixes) {
try {
const resolved = node_url.fileURLToPath(importMetaResolve.resolve(lookup + suffix, base));

if (fs.existsSync(resolved)) {
return resolved;
}
} catch {
//
}
}

/**
* Yarn P'n'P does not support pure ESM well by default, this is only a workaround for it
* @see https://github.com/wooorm/import-meta-resolve/issues/23
*
* TODO: this workaround is still necessary before native `import.meta.resolve` replacement
*/
return resolveFrom.silent(parent, lookup);
}

module.exports = resolveSilent;

0 comments on commit fc88e99

Please sign in to comment.