It'd be nice to have a rule that makes sure certain functions are free of external dependencies. For example, this won't work:
import makeSynchronous from 'make-synchronous'
export const fetchSync = () => {
const url = 'https://example.com'
const getText = makeSynchronous(async () => {
const res = await fetch(url)
return res.text()
})
console.log(getText())
}
(from make-synchronous docs: "The given function is executed in a subprocess, so you cannot use any variables/imports from outside the scope of the function. You can pass in arguments to the function. To import dependencies, use await import(…) in the function body.")
There are other scenarios where functions should/must avoid using variables from outside their scopes, like server actions (maybe?), or if using myFn.toString()
.
I don't know exactly what the conditions for banning scope-external variables should be, but maybe makeSynchronous(...)
-style calling would be a good start, given it's part of the Sindresorhus Cinematic Universe. Config could look similar to no-restricted-syntax:
'restrict-function-dependencies': ['error', [{
selector: 'CallExpression[name=makeSynchronous]',
]],
import makeSynchronous from 'make-synchronous'
export const fetchSync = () => {
const url = 'https://example.com'
const getText = makeSynchronous(async () => {
const res = await fetch(url)
return res.text()
})
console.log(getText())
}
import makeSynchronous from 'make-synchronous'
export const fetchSync = () => {
const getText = makeSynchronous(async () => {
const url = 'https://example.com'
const res = await fetch(url)
return res.text()
})
console.log(getText())
}
import makeSynchronous from 'make-synchronous'
export const fetchSync = () => {
const getText = makeSynchronous(async (url) => {
const res = await fetch(url)
return res.text()
})
console.log(getText('https://example.com'))
}
The devil would be in the details here, and it'd probably be impossible to completely get rid of false positives or false negatives but it could still be useful to (try to) remind people when they can't use normal function scoping.
I had a look for similar ideas in the docs/issues but maybe I missed something? Does this exist?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too