-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathpatch-depd-deprecations.ts
More file actions
45 lines (43 loc) · 1.45 KB
/
patch-depd-deprecations.ts
File metadata and controls
45 lines (43 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
/**
* Some dependencies of Next.js use depd to deprecate some of their functions, depd uses `eval` to generate
* a deprecated version of such functions, this causes `eval` warnings in the terminal even if these functions
* are never called, this function fixes that by patching the depd `wrapfunction` function so that it still
* retains the same type of behavior but without using `eval`
*/
export function patchDepdDeprecations(updater: ContentUpdater): Plugin {
return updater.updateContent("patch-depd-deprecations", [
{
filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
contentFilter: /argument fn must be a function/,
callback: ({ contents }) => patchCode(contents, rule),
},
]);
}
export const rule = `
rule:
kind: function_declaration
pattern: function wrapfunction($FN, $MESSAGE) { $$$ }
all:
- has:
kind: variable_declarator
stopBy: end
has:
field: name
pattern: deprecatedfn
- has:
kind: call_expression
stopBy: end
has:
kind: identifier
pattern: eval
fix:
function wrapfunction($FN, $MESSAGE) {
if(typeof $FN !== 'function') throw new Error("argument fn must be a function");
return function deprecated_$FN(...args) {
console.warn($MESSAGE);
return $FN(...args);
}
}
`;