Skip to content

Commit

Permalink
fix: avoid potential clash with neostandard n/
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jul 1, 2024
1 parent a8e038e commit 4e70c1a
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions base-configs/node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import nPlugin from 'eslint-plugin-n';

/**
Expand All @@ -8,35 +7,67 @@ import nPlugin from 'eslint-plugin-n';
export function nodeRules (cjs) {
return [
{
...nPlugin.configs['flat/recommended-module'],
...renamePlugin('n', 'voxpelli-n', nPlugin.configs['flat/recommended-module']),
// If CommonJS, only target *.mjs, target everything but *.cjs
...cjs ? { files: ['**/*.mjs'] } : { ignores: ['**/*.cjs'] },
},
{
...nPlugin.configs['flat/recommended-script'],
...renamePlugin('n', 'voxpelli-n', nPlugin.configs['flat/recommended-script']),
// If CommonJS, target everything but *.mjs, else only target *.cjs
...cjs ? { ignores: ['**/*.mjs'] } : { files: ['**/*.cjs'] },
},
{
name: '@voxpelli/additional/node',
rules: {
// Overriding
'n/no-process-exit': 'off',
'voxpelli-n/no-process-exit': 'off',

// Adding
'n/prefer-global/console': 'warn',
'n/prefer-promises/fs': 'warn',
'n/no-process-env': 'warn',
'n/no-sync': 'error',
'voxpelli-n/prefer-global/console': 'warn',
'voxpelli-n/prefer-promises/fs': 'warn',
'voxpelli-n/no-process-env': 'warn',
'voxpelli-n/no-sync': 'error',
},
},
{
name: '@voxpelli/additional/node/ts',
files: ['**/*.ts'],
rules: {
// TODO: Remove when *.js files can be properly resolved from *.d.ts
'n/no-missing-import': 'off',
'voxpelli-n/no-missing-import': 'off',
},
},
];
}

/**
* @param {string} existingPluginName
* @param {string} newPluginName
* @param {import('eslint').Linter.FlatConfig} rawConfig
* @returns {import('eslint').Linter.FlatConfig}
*/
function renamePlugin (existingPluginName, newPluginName, rawConfig) {
const config = {
...rawConfig,
plugins: { ...rawConfig.plugins },
rules: { ...rawConfig.rules },
};

if ('plugins' in config && 'rules' in config) {
const plugin = config.plugins[existingPluginName];

if (plugin) {
config.plugins[newPluginName] = plugin;
delete config.plugins[existingPluginName];

for (const key in config.rules) {
config.rules[key.replaceAll(existingPluginName + '/', newPluginName + '/')] = config.rules[key];
delete config.rules[key];
}

return config;
}
}

throw new Error(`Could not rename "${existingPluginName}" plugin`);
}

0 comments on commit 4e70c1a

Please sign in to comment.