Skip to content

Commit

Permalink
fix: Revert "feat: add support for ignoring sync methods from certain…
Browse files Browse the repository at this point in the history
… locations" (#416)
  • Loading branch information
scagood authored Mar 4, 2025
1 parent 90de242 commit 0779e2f
Show file tree
Hide file tree
Showing 18 changed files with 9 additions and 543 deletions.
57 changes: 0 additions & 57 deletions docs/rules/no-sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ fs.readFileSync(somePath).toString();
#### ignores

You can `ignore` specific function names using this option.
Additionally, if you are using TypeScript you can optionally specify where the function is declared.

Examples of **incorrect** code for this rule with the `{ ignores: ['readFileSync'] }` option:

Expand All @@ -79,62 +78,6 @@ Examples of **correct** code for this rule with the `{ ignores: ['readFileSync']
fs.readFileSync(somePath);
```

##### Advanced (TypeScript only)

You can provide a list of specifiers to ignore. Specifiers are typed as follows:

```ts
type Specifier =
| string
| {
from: "file";
path?: string;
name?: string[];
}
| {
from: "package";
package?: string;
name?: string[];
}
| {
from: "lib";
name?: string[];
}
```
###### From a file
Examples of **correct** code for this rule with the ignore file specifier:
```js
/*eslint n/no-sync: ["error", { ignores: [{ from: 'file', path: './foo.ts' }]}] */

import { fooSync } from "./foo"
fooSync()
```

###### From a package

Examples of **correct** code for this rule with the ignore package specifier:

```js
/*eslint n/no-sync: ["error", { ignores: [{ from: 'package', package: 'effect' }]}] */

import { Effect } from "effect"
const value = Effect.runSync(Effect.succeed(42))
```

###### From the TypeScript library

Examples of **correct** code for this rule with the ignore lib specifier:

```js
/*eslint n/no-sync: ["error", { ignores: [{ from: 'lib' }]}] */

const stylesheet = new CSSStyleSheet()
stylesheet.replaceSync("body { font-size: 1.4em; } p { color: red; }")
```

## 🔎 Implementation

- [Rule source](../../lib/rules/no-sync.js)
Expand Down
114 changes: 4 additions & 110 deletions lib/rules/no-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
*/
"use strict"

const typeMatchesSpecifier =
/** @type {import('ts-declaration-location').default} */ (
/** @type {unknown} */ (require("ts-declaration-location"))
)
const getTypeOfNode = require("../util/get-type-of-node")
const getParserServices = require("../util/get-parser-services")
const getFullTypeName = require("../util/get-full-type-name")

const selectors = [
// fs.readFileSync()
// readFileSync.call(null, 'path')
Expand Down Expand Up @@ -40,56 +32,7 @@ module.exports = {
},
ignores: {
type: "array",
items: {
oneOf: [
{ type: "string" },
{
type: "object",
properties: {
from: { const: "file" },
path: {
type: "string",
},
name: {
type: "array",
items: {
type: "string",
},
},
},
additionalProperties: false,
},
{
type: "object",
properties: {
from: { const: "lib" },
name: {
type: "array",
items: {
type: "string",
},
},
},
additionalProperties: false,
},
{
type: "object",
properties: {
from: { const: "package" },
package: {
type: "string",
},
name: {
type: "array",
items: {
type: "string",
},
},
},
additionalProperties: false,
},
],
},
items: { type: "string" },
default: [],
},
},
Expand All @@ -114,64 +57,15 @@ module.exports = {
* @returns {void}
*/
[selector.join(",")](node) {
const parserServices = getParserServices(context)

/**
* @type {import('typescript').Type | undefined | null}
*/
let type = undefined

/**
* @type {string | undefined | null}
*/
let fullName = undefined

for (const ignore of ignores) {
if (typeof ignore === "string") {
if (ignore === node.name) {
return
}

continue
}

if (
parserServices === null ||
parserServices.program === null
) {
throw new Error(
'TypeScript parser services not available. Rule "n/no-sync" is configured to use "ignores" option with a non-string value. This requires TypeScript parser services to be available.'
)
}

type =
type === undefined
? getTypeOfNode(node, parserServices)
: type

fullName =
fullName === undefined
? getFullTypeName(type)
: fullName

if (
typeMatchesSpecifier(
parserServices.program,
ignore,
type
) &&
(ignore.name === undefined ||
ignore.name.includes(fullName ?? node.name))
) {
return
}
if (ignores.includes(node.name)) {
return
}

context.report({
node: node.parent,
messageId: "noSync",
data: {
propertyName: fullName ?? node.name,
propertyName: node.name,
},
})
},
Expand Down
47 changes: 0 additions & 47 deletions lib/util/get-full-type-name.js

This file was deleted.

24 changes: 0 additions & 24 deletions lib/util/get-parser-services.js

This file was deleted.

21 changes: 0 additions & 21 deletions lib/util/get-type-of-node.js

This file was deleted.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@
},
"dependencies": {
"@eslint-community/eslint-utils": "^4.4.1",
"@typescript-eslint/utils": "^8.21.0",
"enhanced-resolve": "^5.17.1",
"eslint-plugin-es-x": "^7.8.0",
"get-tsconfig": "^4.8.1",
"globals": "^15.11.0",
"ignore": "^5.3.2",
"minimatch": "^9.0.5",
"semver": "^7.6.3",
"ts-declaration-location": "^1.0.5"
"semver": "^7.6.3"
},
"devDependencies": {
"@eslint/js": "^9.14.0",
"@types/eslint": "^9.6.1",
"@types/estree": "^1.0.6",
"@types/node": "^20.17.5",
"@typescript-eslint/parser": "^8.21.0",
"@typescript-eslint/typescript-estree": "^8.21.0",
"@typescript-eslint/parser": "^8.12.2",
"@typescript-eslint/typescript-estree": "^8.12.2",
"eslint": "^9.14.0",
"eslint-config-prettier": "^9.1.0",
"eslint-doc-generator": "^1.7.1",
Expand All @@ -54,7 +52,7 @@
"rimraf": "^5.0.10",
"ts-ignore-import": "^4.0.1",
"type-fest": "^4.26.1",
"typescript": "^5.7"
"typescript": "~5.6"
},
"scripts": {
"build": "node scripts/update",
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/no-sync/base/file.ts

This file was deleted.

10 changes: 0 additions & 10 deletions tests/fixtures/no-sync/base/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/no-sync/file.ts

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/no-sync/ignore-package/file.ts

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

7 changes: 0 additions & 7 deletions tests/fixtures/no-sync/ignore-package/package.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/fixtures/no-sync/ignore-package/tsconfig.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/fixtures/no-sync/tsconfig.json

This file was deleted.

Loading

0 comments on commit 0779e2f

Please sign in to comment.