Skip to content

Commit a108985

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. - Remove unused eslint-disable directive n/no-unpublished-require on `lib/util/get-full-type-name.js`, as `typescript` is now an optional peer dependency.
1 parent 42464ab commit a108985

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
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: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ let typeMatchesSpecifier =
88
/** @type {import('ts-declaration-location').default | undefined} */
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,27 @@ 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+
try {
127+
typeMatchesSpecifier ||=
128+
/** @type {import('ts-declaration-location').default} */ (
129+
/** @type {unknown} */ (
130+
require("ts-declaration-location")
131+
)
132+
)
133+
} catch {
134+
throw new Error(
135+
'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.'
136+
)
137+
}
138+
}
139+
127140
return {
128141
/**
129142
* @param {import('estree').Identifier & {parent: import('estree').Node}} node
@@ -160,12 +173,6 @@ module.exports = {
160173
)
161174
}
162175

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-
169176
type =
170177
type === undefined
171178
? getTypeOfNode(node, parserServices)
@@ -177,7 +184,7 @@ module.exports = {
177184
: fullName
178185

179186
if (
180-
typeMatchesSpecifier(
187+
typeMatchesSpecifier && typeMatchesSpecifier(
181188
parserServices.program,
182189
ignore,
183190
type

lib/util/get-full-type-name.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const ts = (() => {
44
try {
5-
// eslint-disable-next-line n/no-unpublished-require
65
return require("typescript")
76
} catch {
87
return null

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)