Skip to content

Commit 9c75763

Browse files
committed
feat(no-sync)!: move ts-declaration-location to peerDependencies
- Add `typescript` as an optional peer dependency for the `n/no-sync` rule, used on `lib/util/get-full-type-name.js`. - Set `ts-declaration-location` as an optional peer dependency for the `n/no-sync` rule. - Move `ts-declaration-location` to `devDependencies` for development and testing. - Update the `n/no-sync` documentation to refer the updated peer dependency requirement. - Update `n/no-sync` rule to conditionally require `ts-declaration-location` only when advanced ignores are used.
1 parent 42464ab commit 9c75763

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

docs/rules/no-sync.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ Examples of **correct** code for this rule with the `{ ignores: ['readFileSync']
7979
fs.readFileSync(somePath);
8080
```
8181

82+
> [!WARNING]
83+
> Advanced `ignores` options (object specifiers) require TypeScript and the [`ts-declaration-location`](https://www.npmjs.com/package/ts-declaration-location) package. This package is an **optional peer dependency** for the `n/no-sync` rule. If you want to use advanced TypeScript-based ignores, please install it in your project:
84+
>
85+
> ```sh
86+
> npm install --save-dev ts-declaration-location
87+
> ```
88+
8289
##### Advanced (TypeScript only)
8390
8491
You can provide a list of specifiers to ignore. Specifiers are typed as follows:
@@ -102,6 +109,9 @@ type Specifier =
102109
}
103110
```
104111
112+
> [!NOTE]
113+
> To use advanced TypeScript-based ignores, you must have `ts-declaration-location` installed as a dependency in your project.
114+
105115
###### From a file
106116

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

lib/rules/no-sync.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@
55
"use strict"
66

77
let typeMatchesSpecifier =
8-
/** @type {import('ts-declaration-location').default | undefined} */
8+
/** @type {import('ts-declaration-location').default | undefined | null} */
99
(undefined)
1010

11-
try {
12-
typeMatchesSpecifier =
13-
/** @type {import('ts-declaration-location').default} */ (
14-
/** @type {unknown} */ (require("ts-declaration-location"))
15-
)
16-
17-
// eslint-disable-next-line no-empty -- Deliberately left empty.
18-
} catch {}
1911
const getTypeOfNode = require("../util/get-type-of-node")
2012
const getParserServices = require("../util/get-parser-services")
2113
const getFullTypeName = require("../util/get-full-type-name")
@@ -124,6 +116,31 @@ module.exports = {
124116
const selector = options.allowAtRootLevel
125117
? selectors.map(selector => `:function ${selector}`)
126118
: selectors
119+
120+
const hasAdvancedIgnores = ignores.some(
121+
ignore => typeof ignore !== "string"
122+
)
123+
124+
// Only require `ts-declaration-location` if needed and not already required.
125+
if (hasAdvancedIgnores) {
126+
if (typeMatchesSpecifier === undefined) {
127+
try {
128+
typeMatchesSpecifier =
129+
/** @type {import('ts-declaration-location').default} */ (
130+
/** @type {unknown} */ (
131+
require("ts-declaration-location")
132+
)
133+
)
134+
} catch {
135+
typeMatchesSpecifier === null
136+
}
137+
} else {
138+
throw new Error(
139+
'ts-declaration-location not available. Rule "n/no-sync" is configured to use "ignores" option with a non-string value. This requires ts-declaration-location to be available.'
140+
)
141+
}
142+
}
143+
127144
return {
128145
/**
129146
* @param {import('estree').Identifier & {parent: import('estree').Node}} node
@@ -160,12 +177,6 @@ module.exports = {
160177
)
161178
}
162179

163-
if (typeMatchesSpecifier === undefined) {
164-
throw new Error(
165-
'ts-declaration-location not available. Rule "n/no-sync" is configured to use "ignores" option with a non-string value. This requires ts-declaration-location to be available.'
166-
)
167-
}
168-
169180
type =
170181
type === undefined
171182
? getTypeOfNode(node, parserServices)

package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@
1414
"types/index.d.ts"
1515
],
1616
"peerDependencies": {
17-
"eslint": ">=8.23.0"
17+
"eslint": ">=8.23.0",
18+
"ts-declaration-location": "^1.0.6",
19+
"typescript": ">=4.8.4"
20+
},
21+
"peerDependenciesMeta": {
22+
"ts-declaration-location": {
23+
"optional": true
24+
},
25+
"typescript": {
26+
"optional": true
27+
}
1828
},
1929
"dependencies": {
2030
"@eslint-community/eslint-utils": "^4.5.0",
@@ -25,8 +35,7 @@
2535
"globals": "^15.11.0",
2636
"ignore": "^5.3.2",
2737
"minimatch": "^9.0.5",
28-
"semver": "^7.6.3",
29-
"ts-declaration-location": "^1.0.6"
38+
"semver": "^7.6.3"
3039
},
3140
"devDependencies": {
3241
"@eslint/js": "^9.14.0",
@@ -52,6 +61,7 @@
5261
"punycode": "^2.3.1",
5362
"release-it": "^17.10.0",
5463
"rimraf": "^5.0.10",
64+
"ts-declaration-location": "^1.0.6",
5565
"ts-ignore-import": "^4.0.1",
5666
"type-fest": "^4.26.1",
5767
"typescript": "~5.6"
@@ -122,4 +132,4 @@
122132
"imports": {
123133
"#test-helpers": "./tests/test-helpers.js"
124134
}
125-
}
135+
}

0 commit comments

Comments
 (0)