Skip to content

Commit

Permalink
Merge branch 'main' into no-empty-file-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
grshakirova committed Apr 17, 2024
2 parents f840f3e + e0dfed2 commit a1e184d
Show file tree
Hide file tree
Showing 64 changed files with 1,093 additions and 751 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/main.yml
Expand Up @@ -16,19 +16,20 @@ jobs:
matrix:
node-version:
- 20
- 16
- 18
os:
- ubuntu-latest
- windows-latest
include:
- os: ubuntu-latest
node-version: 18
# Even numbers of node-version
# include:
# - os: ubuntu-latest
# node-version: 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm install --legacy-peer-deps
- run: npx ava
lint-test:
runs-on: ${{ matrix.os }}
Expand All @@ -43,24 +44,25 @@ jobs:
with:
# Locked due to the difference of `zlib.gzipSync()` between Node.js versions
node-version: 20
- run: npm install
- run: npm install --legacy-peer-deps
- run: npm run lint
- run: npx del-cli test/snapshots --verbose
# Force update snapshots, https://github.com/avajs/ava/discussions/2754
- run: npx c8 ava --update-snapshots
env:
AVA_FORCE_CI: not-ci
- run: git diff --exit-code
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
files: coverage/lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
run-rules-on-codebase:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm install
- run: npm install --legacy-peer-deps
- run: npm run run-rules-on-codebase
integration:
name: Integration test (${{ matrix.group }})
Expand All @@ -83,5 +85,5 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm install
- run: npm install --legacy-peer-deps
- run: npm run integration -- --group ${{ matrix.group }}
1 change: 1 addition & 0 deletions docs/rules/no-array-for-each.md
Expand Up @@ -12,6 +12,7 @@ Benefits of [`for…of` statement](https://developer.mozilla.org/en-US/docs/Web/
- Faster
- Better readability
- Ability to exit early with `break` or `return`
- Ability to skip iterations with `continue`

Additionally, using `for…of` has great benefits if you are using TypeScript, because it does not cause a function boundary to be crossed. This means that type-narrowing earlier on in the current scope will work properly while inside of the loop (without having to re-type-narrow). Furthermore, any mutated variables inside of the loop will picked up on for the purposes of determining if a variable is being used.

Expand Down
30 changes: 20 additions & 10 deletions docs/rules/prefer-number-properties.md
Expand Up @@ -39,14 +39,6 @@ const foo = isFinite(10);
if (Object.is(foo, NaN)) {}
```

```js
const isPositiveZero = value => value === 0 && 1 / value === Infinity;
```

```js
const isNegativeZero = value => value === 0 && 1 / value === -Infinity;
```

```js
const {parseInt} = Number;
const foo = parseInt('10', 2);
Expand Down Expand Up @@ -82,16 +74,24 @@ const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INF
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;
```

```js
const isPositiveZero = value => value === 0 && 1 / value === Infinity;
```

```js
const isNegativeZero = value => value === 0 && 1 / value === -Infinity;
```

## Options

Type: `object`

### checkInfinity

Type: `boolean`\
Default: `true`
Default: `false`

Pass `checkInfinity: false` to disable check on `Infinity`.
Pass `checkInfinity: true` to enable check on `Infinity`.

#### Fail

Expand All @@ -116,3 +116,13 @@ const foo = Infinity;
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}]
const foo = -Infinity;
```

```js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INFINITY;
```

```js
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;
```
13 changes: 7 additions & 6 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-unicorn",
"version": "51.0.1",
"version": "52.0.0",
"description": "More than 100 powerful ESLint rules",
"license": "MIT",
"repository": "sindresorhus/eslint-plugin-unicorn",
Expand Down Expand Up @@ -51,7 +51,7 @@
"dependencies": {
"@babel/helper-validator-identifier": "^7.22.20",
"@eslint-community/eslint-utils": "^4.4.0",
"@eslint/eslintrc": "^2.1.4",
"@eslint/eslintrc": "^3.0.0",
"ci-info": "^4.0.0",
"clean-regexp": "^1.0.0",
"core-js-compat": "^3.34.0",
Expand All @@ -69,20 +69,21 @@
"devDependencies": {
"@babel/code-frame": "^7.23.5",
"@babel/core": "^7.23.6",
"@babel/eslint-parser": "^7.23.3",
"@babel/eslint-parser": "^7.24.1",
"@lubien/fixture-beta-package": "^1.0.0-beta.1",
"@typescript-eslint/parser": "^6.15.0",
"@typescript-eslint/parser": "^7.5.0",
"ava": "^6.0.1",
"c8": "^8.0.1",
"chalk": "^5.3.0",
"enquirer": "^2.4.1",
"eslint": "^8.56.0",
"eslint": "^9.0.0",
"eslint-ava-rule-tester": "^5.0.1",
"eslint-doc-generator": "^1.6.1",
"eslint-doc-generator": "^1.7.0",
"eslint-plugin-eslint-plugin": "^5.2.1",
"eslint-plugin-internal-rules": "file:./scripts/internal-rules/",
"eslint-remote-tester": "^3.0.1",
"eslint-remote-tester-repositories": "^1.0.1",
"espree": "^10.0.0",
"execa": "^8.0.1",
"listr": "^0.14.3",
"lodash-es": "^4.17.21",
Expand Down
6 changes: 6 additions & 0 deletions rules/import-style.js
Expand Up @@ -117,9 +117,15 @@ const defaultStyles = {
path: {
default: true,
},
'node:path': {
default: true,
},
util: {
named: true,
},
'node:util': {
named: true,
},
};

/** @param {import('eslint').Rule.RuleContext} context */
Expand Down
18 changes: 2 additions & 16 deletions rules/no-useless-switch-case.js
Expand Up @@ -16,31 +16,17 @@ const create = context => ({
* SwitchStatement(switchStatement) {
const {cases} = switchStatement;

// TypeScript allows multiple `default` cases
const defaultCases = cases.filter(switchCase => switchCase.test === null);
if (defaultCases.length !== 1) {
return;
}

const [defaultCase] = defaultCases;

// We only check cases where the last case is the `default` case
if (defaultCase !== cases.at(-1)) {
if (cases.length < 2 || cases.at(-1).test !== null) {
return;
}

const uselessCases = [];

for (let index = cases.length - 2; index >= 0; index--) {
const node = cases[index];
if (isEmptySwitchCase(node)) {
uselessCases.unshift(node);
} else {
if (!isEmptySwitchCase(node)) {
break;
}
}

for (const node of uselessCases) {
yield {
node,
loc: getSwitchCaseHeadLocation(node, context.sourceCode),
Expand Down
2 changes: 1 addition & 1 deletion rules/prefer-number-properties.js
Expand Up @@ -78,7 +78,7 @@ const create = context => {
const {
checkInfinity,
} = {
checkInfinity: true,
checkInfinity: false,
...context.options[0],
};
const {sourceCode} = context;
Expand Down
6 changes: 4 additions & 2 deletions rules/prefer-spread.js
Expand Up @@ -6,6 +6,7 @@ const {
needsSemicolon,
isNodeMatches,
isMethodNamed,
hasOptionalChainElement,
} = require('./utils/index.js');
const {removeMethodCall} = require('./fix/index.js');
const {isLiteral, isMethodCall} = require('./ast/index.js');
Expand Down Expand Up @@ -403,7 +404,8 @@ const create = context => {
optionalCall: false,
optionalMember: false,
})
&& node.callee.object.type !== 'ArrayExpression'
&& !isArrayLiteral(node.callee.object)
&& !hasOptionalChainElement(node.callee.object)
)) {
return;
}
Expand Down Expand Up @@ -476,7 +478,7 @@ const create = context => {
const resultBySpread = [...value];

hasSameResult = resultBySplit.length === resultBySpread.length
&& resultBySplit.every((character, index) => character === resultBySpread[index]);
&& resultBySplit.every((character, index) => character === resultBySpread[index]);
}

const problem = {
Expand Down
2 changes: 1 addition & 1 deletion rules/prevent-abbreviations.js
Expand Up @@ -116,7 +116,7 @@ const getNameReplacements = (name, options, limit = 3) => {
}

// Split words
const words = name.split(/(?=[^a-z])|(?<=[^A-Za-z])/).filter(Boolean);
const words = name.split(/(?=\P{Lowercase_Letter})|(?<=\P{Letter})/u).filter(Boolean);

let hasReplacements = false;
const combinations = words.map(word => {
Expand Down
23 changes: 4 additions & 19 deletions test/better-regex.mjs
@@ -1,8 +1,6 @@
import {createRequire} from 'node:module';
import {getTester} from './utils/test.mjs';
import {getTester, parsers} from './utils/test.mjs';

const {test} = getTester(import.meta);
const require = createRequire(import.meta.url);

const MESSAGE_ID = 'better-regex';

Expand Down Expand Up @@ -220,21 +218,6 @@ test({
errors: createError('[0-9]', '\\d'),
output: 'const foo = new RegExp(\'\\\\d\', \'ig\')',
},
{
code: 'const foo = new RegExp(/[0-9]/)',
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = new RegExp(/\\d/)',
},
{
code: 'const foo = new RegExp(/[0-9]/, \'ig\')',
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = new RegExp(/\\d/, \'ig\')',
},
{
code: 'const foo = new RegExp(/[0-9]/)',
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = new RegExp(/\\d/)',
},
{
code: 'const foo = new RegExp(/[0-9]/, \'ig\')',
errors: createError('/[0-9]/', '/\\d/'),
Expand Down Expand Up @@ -310,7 +293,9 @@ test({
message: 'Problem parsing /(/: \n\n/(/\n ^\nUnexpected token: "/" at 1:2.',
},
],
parser: require.resolve('@typescript-eslint/parser'),
languageOptions: {
parser: parsers.typescript,
},
},

// Not fixable
Expand Down
15 changes: 8 additions & 7 deletions test/catch-error-name.mjs
Expand Up @@ -29,7 +29,6 @@ function invalidTestCase(options) {

test({
valid: [
'try {} catch (error) {}',
{
code: 'try {} catch (err) {}',
options: [{name: 'err'}],
Expand Down Expand Up @@ -900,12 +899,14 @@ test.typescript({

test.babel({
testerOptions: {
parserOptions: {
babelOptions: {
parserOpts: {
plugins: [
['decorators', {decoratorsBeforeExport: true}],
],
languageOptions: {
parserOptions: {
babelOptions: {
parserOpts: {
plugins: [
['decorators', {decoratorsBeforeExport: true}],
],
},
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions test/consistent-destructuring.mjs
Expand Up @@ -444,7 +444,7 @@ test({
b
}
} = foo;
console.log(foo.a.c);
console.log(foo.a.c); // 2
`,
errors: [{
message: 'Use destructured variables over properties.',
Expand All @@ -453,15 +453,15 @@ test({
{
code: outdent`
const {a} = foo;
console.log(foo.a);
console.log(foo.a); // 2
`,
errors: [{
message: 'Use destructured variables over properties.',
suggestions: [{
desc: 'Replace `foo.a` with destructured property `a`.',
output: outdent`
const {a} = foo;
console.log(a);
console.log(a); // 2
`,
}],
}],
Expand Down

0 comments on commit a1e184d

Please sign in to comment.