Skip to content

Commit

Permalink
feat: conditional requireFlag on require-unicode-regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Sep 6, 2024
1 parent 8718079 commit d06080f
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 55 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# eslint-config-ash-nazg CHANGES

## 36.14.0

- feat: conditional `requireFlag` on `require-unicode-regexp`
- feat: update deps.

## 36.13.0

- feat: switch to renamed markdown plugin
Expand Down
37 changes: 36 additions & 1 deletion detectEnv.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Todo: Move to own repo
/* eslint-disable n/no-sync -- Not sure possible to use here */

import browserslist from 'browserslist';

/**
* You can use this in your `eslint.config.js` as follows...
Expand Down Expand Up @@ -31,7 +34,6 @@
* 3|5|6|7|8|9|10|11|12|13|14|15} EcmaVersion
*/

/* eslint-disable n/no-sync -- Not sure possible to use here */
import {readFileSync} from 'node:fs';
import {join} from 'node:path';
import semver from 'semver';
Expand Down Expand Up @@ -110,6 +112,39 @@ const detectNodeVersion = (packagePath) => {
return nodeVersion;
};

/**
* @param {{
* engines?: {node?: string}
* }} pkg
* @param {string} range
*/
export const pkgSatisfiesNodeVersion = (pkg, range) => {
const {engines: {node} = {}} = pkg;
return node
? semver.satisfies(node, range)
: (() => {
throw new Error('Could not detect Node version from package.json');
});
};

/**
* @param {{
* browserslist?: string[]|string
* }} pkg
* @param {string} range
*/
export const pkgSatisfiesBrowserVersion = (pkg, range) => {
if (!pkg?.browserslist) {
throw new Error('No `browserslist` found in `package.json`');
}
const desiredBrowsers = browserslist(pkg.browserslist);
const browsers = browserslist(range);
const badBrowsers = desiredBrowsers.filter(
(browser) => browsers.includes(browser)
).join(', ');
return !badBrowsers;
};

/**
* @param {string} cwd
*/
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default function index (types, config) {
languageOptions
});
} else {
configs.push(...sauron(pkg));
configs.push(...sauron(pkg, types));
}
// basic config ("saruman") is the default
} else if (types.includes('node')) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-ash-nazg",
"version": "36.13.0",
"version": "36.14.0",
"description": "An expansion and tweaking of the \"standard\" config style for ESLint",
"main": "index.js",
"type": "module",
Expand Down Expand Up @@ -75,6 +75,7 @@
"@eslint/markdown": "^6.1.0",
"@fintechstudios/eslint-plugin-chai-as-promised": "^3.1.0",
"@stylistic/eslint-plugin": "^2.7.2",
"browserslist": "^4.23.3",
"es-file-traverse": "^1.2.0",
"eslint-plugin-array-func": "^5.0.2",
"eslint-plugin-chai-expect": "^3.1.0",
Expand Down
52 changes: 8 additions & 44 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion sauron-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import sauron from './sauron.js';
export default function sauronNode (pkg) {
return [
...node(pkg),
...sauron(pkg),
...sauron(pkg, ['node']),
{
name: 'ash-nazg/sauron-node',
rules: {
Expand Down
2 changes: 1 addition & 1 deletion sauron-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ import mocha from './mocha.js';
* }
*/
export default function sauronOverrides (types, pkg) {
return [...sauron(pkg), ...overrides(types, pkg), ...mocha];
return [...sauron(pkg, []), ...overrides(types, pkg), ...mocha];
}
2 changes: 1 addition & 1 deletion sauron-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import script from './script.js';
* }) => import('eslint').Linter.Config[]}
*/
export default function sauronScript (pkg) {
return [...sauron(pkg), ...script];
return [...sauron(pkg, []), ...script];
}
31 changes: 26 additions & 5 deletions sauron.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import main from './main.js';
import {
pkgSatisfiesNodeVersion, pkgSatisfiesBrowserVersion
} from './detectEnv.js';

/**
* @type {(pkg: {
* type?: "module"|"commonjs"
* }) => import('eslint').Linter.Config[]}
*/
export default function sauron (pkg) {
* type?: "module"|"commonjs",
* engines?: {node?: string},
* browserslist?: string[]|string
* }, types: string[]) => import('eslint').Linter.Config[]}
*/
export default function sauron (pkg, types) {
return [
...main(pkg),
{
Expand Down Expand Up @@ -102,7 +107,23 @@ export default function sauron (pkg) {
}],
'prefer-named-capture-group': ['warn'],
'prefer-numeric-literals': ['warn'],
'require-unicode-regexp': ['warn'],
'require-unicode-regexp': [
'warn',
(
(types.includes('node') &&
pkgSatisfiesNodeVersion(pkg, '>=20.0.0')) ||
(types.includes('browser') &&
pkgSatisfiesBrowserVersion(
pkg,
// Keep in sync with eslint-plugin-escompat
'edge > 0, safari < 17, firefox < 116, chrome < 112'
))
)
? {
requireFlag: 'v'
}
: {}
],
'vars-on-top': ['warn'],

'no-implicit-globals': ['error', {lexicalBindings: true}],
Expand Down

0 comments on commit d06080f

Please sign in to comment.