@venizia/dev-configs 0.5.0-1 → 0.5.0-10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -13
- package/dist/checks/purity/manifest.js +11 -3
- package/dist/checks/purity/manifest.js.map +1 -1
- package/dist/checks/purity/probe.js +19 -2
- package/dist/checks/purity/probe.js.map +1 -1
- package/dist/checks/split-report.js +2 -1
- package/dist/checks/split-report.js.map +1 -1
- package/dist/checks/test-controls.d.ts +9 -1
- package/dist/checks/test-controls.js +9 -3
- package/dist/checks/test-controls.js.map +1 -1
- package/dist/checks/test-dead-code.d.ts +9 -1
- package/dist/checks/test-dead-code.js +13 -2
- package/dist/checks/test-dead-code.js.map +1 -1
- package/dist/checks/test-mutate.d.ts +9 -1
- package/dist/checks/test-mutate.js +12 -6
- package/dist/checks/test-mutate.js.map +1 -1
- package/dist/cli/check.js +42 -38
- package/dist/cli/check.js.map +1 -1
- package/dist/cli/clean.js +3 -2
- package/dist/cli/clean.js.map +1 -1
- package/dist/cli/codemod/any-aliases.d.ts +1 -1
- package/dist/cli/codemod/any-aliases.js +14 -13
- package/dist/cli/codemod/any-aliases.js.map +1 -1
- package/dist/cli/codemod/async-style.d.ts +1 -1
- package/dist/cli/codemod/async-style.js +18 -17
- package/dist/cli/codemod/async-style.js.map +1 -1
- package/dist/cli/codemod/binding-keys.d.ts +32 -1
- package/dist/cli/codemod/binding-keys.js +238 -58
- package/dist/cli/codemod/binding-keys.js.map +1 -1
- package/dist/cli/codemod/class-renames.d.ts +39 -0
- package/dist/cli/codemod/class-renames.js +297 -0
- package/dist/cli/codemod/class-renames.js.map +1 -0
- package/dist/cli/codemod/connector-imports.d.ts +1 -1
- package/dist/cli/codemod/connector-imports.js +13 -12
- package/dist/cli/codemod/connector-imports.js.map +1 -1
- package/dist/cli/codemod/const-class-sets.d.ts +55 -0
- package/dist/cli/codemod/const-class-sets.js +219 -0
- package/dist/cli/codemod/const-class-sets.js.map +1 -0
- package/dist/cli/codemod/const-classes.d.ts +21 -3
- package/dist/cli/codemod/const-classes.js +151 -36
- package/dist/cli/codemod/const-classes.js.map +1 -1
- package/dist/cli/codemod/drop-redundant-casts.d.ts +6 -1
- package/dist/cli/codemod/drop-redundant-casts.js +27 -19
- package/dist/cli/codemod/drop-redundant-casts.js.map +1 -1
- package/dist/cli/codemod/env-get.d.ts +3 -3
- package/dist/cli/codemod/env-get.js +19 -18
- package/dist/cli/codemod/env-get.js.map +1 -1
- package/dist/cli/codemod/files.d.ts +4 -2
- package/dist/cli/codemod/files.js +7 -8
- package/dist/cli/codemod/files.js.map +1 -1
- package/dist/cli/codemod/function-to-arrow.d.ts +76 -0
- package/dist/cli/codemod/function-to-arrow.js +357 -0
- package/dist/cli/codemod/function-to-arrow.js.map +1 -0
- package/dist/cli/codemod/index.js +25 -4
- package/dist/cli/codemod/index.js.map +1 -1
- package/dist/cli/codemod/namespace-functions/index.d.ts +1 -1
- package/dist/cli/codemod/namespace-functions/index.js +14 -13
- package/dist/cli/codemod/namespace-functions/index.js.map +1 -1
- package/dist/cli/codemod/namespace-functions/mappings/dev-configs.js +3 -3
- package/dist/cli/codemod/namespace-functions/mappings/dev-configs.js.map +1 -1
- package/dist/cli/codemod/namespace-functions/mappings/helpers.js +1 -1
- package/dist/cli/codemod/namespace-functions/mappings/helpers.js.map +1 -1
- package/dist/cli/codemod/typed-route-context.d.ts +58 -0
- package/dist/cli/codemod/typed-route-context.js +918 -0
- package/dist/cli/codemod/typed-route-context.js.map +1 -0
- package/dist/cli/index.js +5 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/common/cli-output.d.ts +16 -0
- package/dist/common/cli-output.js +49 -0
- package/dist/common/cli-output.js.map +1 -0
- package/dist/compilers/bun.js +1 -1
- package/dist/compilers/bun.js.map +1 -1
- package/dist/linters/eslint.js +1 -1
- package/dist/linters/eslint.js.map +1 -1
- package/dist/linters/house/browser-api-patterns.d.ts +43 -0
- package/dist/linters/house/browser-api-patterns.js +87 -0
- package/dist/linters/house/browser-api-patterns.js.map +1 -0
- package/dist/linters/house/common.d.ts +17 -2
- package/dist/linters/house/common.js +18 -2
- package/dist/linters/house/common.js.map +1 -1
- package/dist/linters/house/configs.js +25 -0
- package/dist/linters/house/configs.js.map +1 -1
- package/dist/linters/house/const-class-values.rule.d.ts +2 -2
- package/dist/linters/house/const-class-values.rule.js +14 -5
- package/dist/linters/house/const-class-values.rule.js.map +1 -1
- package/dist/linters/house/effect-cleanup-consistency.rule.d.ts +2 -2
- package/dist/linters/house/effect-cleanup-consistency.rule.js +1 -1
- package/dist/linters/house/effect-cleanup-consistency.rule.js.map +1 -1
- package/dist/linters/house/framework-api-patterns.d.ts +54 -0
- package/dist/linters/house/framework-api-patterns.js +62 -0
- package/dist/linters/house/framework-api-patterns.js.map +1 -0
- package/dist/linters/house/framework-exports.d.ts +62 -0
- package/dist/linters/house/framework-exports.js +325 -0
- package/dist/linters/house/framework-exports.js.map +1 -0
- package/dist/linters/house/import-support.d.ts +56 -0
- package/dist/linters/house/import-support.js +179 -0
- package/dist/linters/house/import-support.js.map +1 -0
- package/dist/linters/house/index.d.ts +12 -0
- package/dist/linters/house/index.js +15 -1
- package/dist/linters/house/index.js.map +1 -1
- package/dist/linters/house/max-params-own.rule.d.ts +2 -2
- package/dist/linters/house/max-params-own.rule.js +1 -1
- package/dist/linters/house/max-params-own.rule.js.map +1 -1
- package/dist/linters/house/no-async-hook-callback.rule.d.ts +2 -2
- package/dist/linters/house/no-async-hook-callback.rule.js +1 -1
- package/dist/linters/house/no-async-hook-callback.rule.js.map +1 -1
- package/dist/linters/house/no-await-new-chain.rule.d.ts +2 -2
- package/dist/linters/house/no-await-new-chain.rule.js +1 -1
- package/dist/linters/house/no-await-new-chain.rule.js.map +1 -1
- package/dist/linters/house/no-await-promise-chain.rule.d.ts +2 -2
- package/dist/linters/house/no-await-promise-chain.rule.js +5 -5
- package/dist/linters/house/no-await-promise-chain.rule.js.map +1 -1
- package/dist/linters/house/no-inline-await.rule.d.ts +2 -2
- package/dist/linters/house/no-inline-await.rule.js +1 -1
- package/dist/linters/house/no-inline-await.rule.js.map +1 -1
- package/dist/linters/house/no-leaked-render.rule.d.ts +2 -2
- package/dist/linters/house/no-leaked-render.rule.js +1 -1
- package/dist/linters/house/no-leaked-render.rule.js.map +1 -1
- package/dist/linters/house/no-new-error.rule.d.ts +2 -2
- package/dist/linters/house/no-new-error.rule.js +2 -2
- package/dist/linters/house/no-new-error.rule.js.map +1 -1
- package/dist/linters/house/no-parent-import.rule.d.ts +45 -0
- package/dist/linters/house/no-parent-import.rule.js +176 -0
- package/dist/linters/house/no-parent-import.rule.js.map +1 -0
- package/dist/linters/house/no-return-await.rule.d.ts +2 -2
- package/dist/linters/house/no-return-await.rule.js +1 -1
- package/dist/linters/house/no-return-await.rule.js.map +1 -1
- package/dist/linters/house/no-shadow-framework-type.rule.d.ts +24 -0
- package/dist/linters/house/no-shadow-framework-type.rule.js +100 -0
- package/dist/linters/house/no-shadow-framework-type.rule.js.map +1 -0
- package/dist/linters/house/no-silent-catch.rule.d.ts +2 -2
- package/dist/linters/house/no-silent-catch.rule.js +3 -3
- package/dist/linters/house/no-silent-catch.rule.js.map +1 -1
- package/dist/linters/house/no-unbraced-case.rule.d.ts +2 -2
- package/dist/linters/house/no-unbraced-case.rule.js +1 -1
- package/dist/linters/house/no-unbraced-case.rule.js.map +1 -1
- package/dist/linters/house/plugin.d.ts +1 -1
- package/dist/linters/house/plugin.js +18 -2
- package/dist/linters/house/plugin.js.map +1 -1
- package/dist/linters/house/prefer-framework-api.rule.d.ts +83 -0
- package/dist/linters/house/prefer-framework-api.rule.js +562 -0
- package/dist/linters/house/prefer-framework-api.rule.js.map +1 -0
- package/dist/linters/house/prefer-http-method.rule.d.ts +22 -0
- package/dist/linters/house/prefer-http-method.rule.js +182 -0
- package/dist/linters/house/prefer-http-method.rule.js.map +1 -0
- package/dist/linters/house/prefer-promise-sleep.rule.d.ts +26 -0
- package/dist/linters/house/prefer-promise-sleep.rule.js +155 -0
- package/dist/linters/house/prefer-promise-sleep.rule.js.map +1 -0
- package/dist/linters/house/prefer-tnullable.rule.d.ts +25 -0
- package/dist/linters/house/prefer-tnullable.rule.js +124 -0
- package/dist/linters/house/prefer-tnullable.rule.js.map +1 -0
- package/dist/linters/house/typed-route-context.rule.d.ts +19 -0
- package/dist/linters/house/typed-route-context.rule.js +334 -0
- package/dist/linters/house/typed-route-context.rule.js.map +1 -0
- package/dist/linters/house/zod-schema-convention.rule.d.ts +82 -0
- package/dist/linters/house/zod-schema-convention.rule.js +477 -0
- package/dist/linters/house/zod-schema-convention.rule.js.map +1 -0
- package/dist/linters/oxlint.js +11 -1
- package/dist/linters/oxlint.js.map +1 -1
- package/dist/linters/react-eslint.js.map +1 -1
- package/dist/linters/rules/naming-convention.rule.d.ts +4 -1
- package/dist/linters/rules/naming-convention.rule.js +30 -1
- package/dist/linters/rules/naming-convention.rule.js.map +1 -1
- package/dist/linters/rules/no-restricted-properties.rule.js +1 -1
- package/dist/linters/rules/no-restricted-properties.rule.js.map +1 -1
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -2
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/{rejection.helper.d.ts → rejection.utility.d.ts} +4 -4
- package/dist/testing/{rejection.helper.js → rejection.utility.js} +7 -7
- package/dist/testing/rejection.utility.js.map +1 -0
- package/dist/testing/{spawn.helper.d.ts → spawn.utility.d.ts} +2 -2
- package/dist/testing/{spawn.helper.js → spawn.utility.js} +6 -6
- package/dist/testing/spawn.utility.js.map +1 -0
- package/package.json +7 -7
- package/dist/testing/rejection.helper.js.map +0 -1
- package/dist/testing/spawn.helper.js.map +0 -1
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PreferFrameworkApiRule = void 0;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const browser_api_patterns_1 = require("./browser-api-patterns");
|
|
6
|
+
const common_1 = require("./common");
|
|
7
|
+
const framework_api_patterns_1 = require("./framework-api-patterns");
|
|
8
|
+
const framework_exports_1 = require("./framework-exports");
|
|
9
|
+
/** The objects a global event listener is added to. */
|
|
10
|
+
const GLOBAL_TARGETS = ['window', 'document', 'self', 'globalThis'];
|
|
11
|
+
/** The names that stand for the global object. */
|
|
12
|
+
const GLOBAL_OBJECTS = ['window', 'globalThis', 'self'];
|
|
13
|
+
/**
|
|
14
|
+
* `@venizia/prefer-framework-api`. A browser API that `@venizia/ardor-react` wraps in a hook (`window.matchMedia`, a
|
|
15
|
+
* `ResizeObserver`, `navigator.onLine`, a `beforeunload` listener, `navigator.clipboard.writeText`, `window.confirm`...) is
|
|
16
|
+
* used through the hook, which owns the subscribe and cleanup. The rows are `BrowserApiPatterns.TABLE`; the message names
|
|
17
|
+
* the hook. The rule is for apps: the package that ships the hooks (the nearest `package.json` is named
|
|
18
|
+
* `@venizia/ardor-react`) is not reported.
|
|
19
|
+
*
|
|
20
|
+
* The rows of `FrameworkApiPatterns.TABLE` and of the `extra` option are not about React: they report anywhere in a file,
|
|
21
|
+
* with their own message, in a package their `dependsOn` and `owner` allow.
|
|
22
|
+
*
|
|
23
|
+
* For the hook rows, only a reference to the global is reported: a `window`, `navigator` or `document` the file binds itself is not. And only
|
|
24
|
+
* where a hook can be called: in a component (a PascalCase function that draws JSX) or a hook (`useX`), see `isInReact`. A
|
|
25
|
+
* module function, a class and an event handler making a one-off call are left alone. Hooks the author wants to bypass on
|
|
26
|
+
* purpose take an `eslint-disable-next-line` with the reason, or `ignore` lists pattern ids.
|
|
27
|
+
*/
|
|
28
|
+
class PreferFrameworkApiRule {
|
|
29
|
+
static { this.NAME = `${common_1.HOUSE_PLUGIN_NAMESPACE}/prefer-framework-api`; }
|
|
30
|
+
/** The dotted text of `a.b.c` (identifiers only), or `undefined` for anything else. */
|
|
31
|
+
static pathOf(opts) {
|
|
32
|
+
const { node } = opts;
|
|
33
|
+
if (common_1.AstNode.getType({ node }) === common_1.AstNodeTypes.IDENTIFIER) {
|
|
34
|
+
const name = common_1.AstNode.readField({ node, key: 'name' });
|
|
35
|
+
return typeof name === 'string' ? name : undefined;
|
|
36
|
+
}
|
|
37
|
+
if (common_1.AstNode.getType({ node }) !== common_1.AstNodeTypes.MEMBER_EXPRESSION) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const property = common_1.AstNode.readField({ node, key: 'property' });
|
|
41
|
+
const name = common_1.AstNode.readField({ node: property, key: 'name' });
|
|
42
|
+
const head = PreferFrameworkApiRule.pathOf({
|
|
43
|
+
node: common_1.AstNode.readField({ node, key: 'object' }),
|
|
44
|
+
});
|
|
45
|
+
return head !== undefined &&
|
|
46
|
+
typeof name === 'string' &&
|
|
47
|
+
common_1.AstNode.readField({ node, key: 'computed' }) !== true
|
|
48
|
+
? `${head}.${name}`
|
|
49
|
+
: undefined;
|
|
50
|
+
}
|
|
51
|
+
/** `window.navigator.onLine` is `navigator.onLine`. */
|
|
52
|
+
static withoutGlobalRoot(opts) {
|
|
53
|
+
const [first, ...rest] = opts.path.split('.');
|
|
54
|
+
return GLOBAL_OBJECTS.includes(first ?? '') && rest.length > 0 ? rest.join('.') : opts.path;
|
|
55
|
+
}
|
|
56
|
+
/** Whether the root identifier of `path` is the global one: no scope of the file binds it. */
|
|
57
|
+
static isGlobal(opts) {
|
|
58
|
+
const { context, node, path } = opts;
|
|
59
|
+
const root = path.split('.')[0] ?? '';
|
|
60
|
+
let scope = context.sourceCode.getScope(node);
|
|
61
|
+
while (scope) {
|
|
62
|
+
if ((scope.set.get(root)?.defs.length ?? 0) > 0) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
scope = scope.upper;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
static { this.FUNCTIONS = new Set([
|
|
70
|
+
common_1.AstNodeTypes.FUNCTION_DECLARATION,
|
|
71
|
+
common_1.AstNodeTypes.FUNCTION_EXPRESSION,
|
|
72
|
+
common_1.AstNodeTypes.ARROW_FUNCTION_EXPRESSION,
|
|
73
|
+
]); }
|
|
74
|
+
/** Whether a function has JSX anywhere in it: with a PascalCase name, that is a component. */
|
|
75
|
+
static { this.jsxCache = new WeakMap(); }
|
|
76
|
+
static containsJsx(opts) {
|
|
77
|
+
const { context, node } = opts;
|
|
78
|
+
if (typeof node !== 'object' || node === null) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const cached = PreferFrameworkApiRule.jsxCache.get(node);
|
|
82
|
+
if (cached !== undefined) {
|
|
83
|
+
return cached;
|
|
84
|
+
}
|
|
85
|
+
const type = common_1.AstNode.getType({ node });
|
|
86
|
+
let found = type === common_1.AstNodeTypes.JSX_ELEMENT || type === common_1.AstNodeTypes.JSX_FRAGMENT;
|
|
87
|
+
for (const key of type === undefined ? [] : (context.sourceCode.visitorKeys[type] ?? [])) {
|
|
88
|
+
const child = common_1.AstNode.readField({ node, key });
|
|
89
|
+
for (const entry of Array.isArray(child) ? child : [child]) {
|
|
90
|
+
found = found || PreferFrameworkApiRule.containsJsx({ context, node: entry });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
PreferFrameworkApiRule.jsxCache.set(node, found);
|
|
94
|
+
return found;
|
|
95
|
+
}
|
|
96
|
+
/** The name a function is known by: its own, or the variable it is assigned to (through one `memo(...)` / `forwardRef(...)`). */
|
|
97
|
+
static nameOf(opts) {
|
|
98
|
+
const { fn } = opts;
|
|
99
|
+
const own = common_1.AstNode.readField({
|
|
100
|
+
node: common_1.AstNode.readField({ node: fn, key: 'id' }),
|
|
101
|
+
key: 'name',
|
|
102
|
+
});
|
|
103
|
+
if (typeof own === 'string') {
|
|
104
|
+
return own;
|
|
105
|
+
}
|
|
106
|
+
let parent = common_1.AstNode.readField({ node: fn, key: 'parent' });
|
|
107
|
+
if (common_1.AstNode.getType({ node: parent }) === common_1.AstNodeTypes.CALL_EXPRESSION) {
|
|
108
|
+
parent = common_1.AstNode.readField({ node: parent, key: 'parent' });
|
|
109
|
+
}
|
|
110
|
+
const name = common_1.AstNode.readField({
|
|
111
|
+
node: common_1.AstNode.readField({ node: parent, key: 'id' }),
|
|
112
|
+
key: 'name',
|
|
113
|
+
});
|
|
114
|
+
return common_1.AstNode.getType({ node: parent }) === common_1.AstNodeTypes.VARIABLE_DECLARATOR &&
|
|
115
|
+
typeof name === 'string'
|
|
116
|
+
? name
|
|
117
|
+
: undefined;
|
|
118
|
+
}
|
|
119
|
+
/** A hook (`useX`), or a component: a PascalCase function that draws JSX. */
|
|
120
|
+
static isReactFunction(opts) {
|
|
121
|
+
const name = PreferFrameworkApiRule.nameOf({ fn: opts.fn });
|
|
122
|
+
if (name === undefined) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return (/^use[A-Z0-9]/.test(name) ||
|
|
126
|
+
(/^[A-Z]/.test(name) &&
|
|
127
|
+
PreferFrameworkApiRule.containsJsx({ context: opts.context, node: opts.fn })));
|
|
128
|
+
}
|
|
129
|
+
/** Whether `fn` is an argument of a hook call (`useEffect(() => ...)`): the callback runs for the component or the hook. */
|
|
130
|
+
static isHookCallback(opts) {
|
|
131
|
+
const parent = common_1.AstNode.readField({ node: opts.fn, key: 'parent' });
|
|
132
|
+
const callee = common_1.AstNode.readField({ node: parent, key: 'callee' });
|
|
133
|
+
const name = common_1.AstNode.readField({ node: callee, key: 'name' }) ??
|
|
134
|
+
common_1.AstNode.readField({
|
|
135
|
+
node: common_1.AstNode.readField({ node: callee, key: 'property' }),
|
|
136
|
+
key: 'name',
|
|
137
|
+
});
|
|
138
|
+
return (common_1.AstNode.getType({ node: parent }) === common_1.AstNodeTypes.CALL_EXPRESSION &&
|
|
139
|
+
typeof name === 'string' &&
|
|
140
|
+
/^use[A-Z0-9]/.test(name));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Whether `node` is in a component or a hook, which is where a hook can be called. A subscription (a listener, an
|
|
144
|
+
* observer) counts through any handler or helper on the way up: it belongs in a hook. A plain read (`window.innerWidth`,
|
|
145
|
+
* `window.confirm`) counts only in the body of the component or the hook, or in a callback it hands to a hook
|
|
146
|
+
* (`useEffect`, `useMemo`): inside an event handler, or outside every component (a module function, a class), it is a
|
|
147
|
+
* one-off call a hook cannot make.
|
|
148
|
+
*/
|
|
149
|
+
static isInReact(opts) {
|
|
150
|
+
const { context, node, subscribes } = opts;
|
|
151
|
+
for (const ancestor of [...context.sourceCode.getAncestors(node)].reverse()) {
|
|
152
|
+
const type = common_1.AstNode.getType({ node: ancestor });
|
|
153
|
+
if (type === undefined ||
|
|
154
|
+
!PreferFrameworkApiRule.FUNCTIONS.has(type) ||
|
|
155
|
+
!common_1.AstNode.isNode(ancestor)) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (PreferFrameworkApiRule.isReactFunction({ context, fn: ancestor })) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
if (!subscribes && !PreferFrameworkApiRule.isHookCallback({ fn: ancestor })) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
/** A dev tool's own configuration: what it names never reaches a browser or a deployment (a dev server's proxy target). */
|
|
168
|
+
static { this.DEV_TOOL_CONFIG = /(?:^|\/)(?:vite\.config\.[cm]?[jt]s|\.configs\/builders\/.+)$/; }
|
|
169
|
+
/** The rows that apply in a file of this package: the table and `extra`, less the ignored ones. */
|
|
170
|
+
static rowsFor(opts) {
|
|
171
|
+
const { manifest, extra, ignore } = opts;
|
|
172
|
+
const isDevToolConfig = PreferFrameworkApiRule.DEV_TOOL_CONFIG.test(opts.filename.replaceAll('\\', '/'));
|
|
173
|
+
return [...framework_api_patterns_1.FrameworkApiPatterns.TABLE, ...extra].filter((row) => {
|
|
174
|
+
const { id, dependsOn, owner } = row;
|
|
175
|
+
if (isDevToolConfig && id === 'localhost-url') {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
if (ignore.has(id) || (owner !== undefined && manifest?.name === owner)) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
return (dependsOn === undefined ||
|
|
182
|
+
(manifest?.dependencies.some((entry) => entry.startsWith(dependsOn)) ?? false));
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/** The listeners the rows outside React need, by node type or selector. */
|
|
186
|
+
static rowListeners(opts) {
|
|
187
|
+
const { context, rows } = opts;
|
|
188
|
+
const listeners = [];
|
|
189
|
+
for (const { match, message } of rows) {
|
|
190
|
+
const report = (node) => {
|
|
191
|
+
context.report({ node, message });
|
|
192
|
+
};
|
|
193
|
+
/** Reports a global written as a member path, when `accepts` takes it. */
|
|
194
|
+
const onPath = (given) => {
|
|
195
|
+
listeners.push([
|
|
196
|
+
common_1.AstNodeTypes.MEMBER_EXPRESSION,
|
|
197
|
+
(node) => {
|
|
198
|
+
const path = PreferFrameworkApiRule.pathOf({
|
|
199
|
+
node: given.key === '' ? node : common_1.AstNode.readField({ node, key: given.key }),
|
|
200
|
+
});
|
|
201
|
+
if (path !== undefined &&
|
|
202
|
+
!(given.isReadOnly === true && PreferFrameworkApiRule.isWritten({ node })) &&
|
|
203
|
+
(given.property === undefined ||
|
|
204
|
+
given.property.test(PreferFrameworkApiRule.memberName({ node }))) &&
|
|
205
|
+
given.accepts(PreferFrameworkApiRule.withoutGlobalRoot({ path })) &&
|
|
206
|
+
PreferFrameworkApiRule.isGlobal({ context, node, path })) {
|
|
207
|
+
report(node);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
]);
|
|
211
|
+
};
|
|
212
|
+
switch (match.type) {
|
|
213
|
+
case 'path': {
|
|
214
|
+
onPath({ key: '', accepts: (bare) => match.paths.includes(bare) });
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case 'member': {
|
|
218
|
+
onPath({
|
|
219
|
+
key: 'object',
|
|
220
|
+
accepts: (bare) => match.objects.includes(bare),
|
|
221
|
+
property: match.property === undefined ? undefined : new RegExp(match.property),
|
|
222
|
+
isReadOnly: match.isReadOnly,
|
|
223
|
+
});
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
case 'selector': {
|
|
227
|
+
listeners.push([match.selector, report]);
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case 'literal': {
|
|
231
|
+
const pattern = new RegExp(match.pattern);
|
|
232
|
+
const check = (node, text) => {
|
|
233
|
+
const isWanted = match.isFallbackOnly !== true || PreferFrameworkApiRule.isFallback({ node });
|
|
234
|
+
if (typeof text === 'string' && isWanted && pattern.test(text)) {
|
|
235
|
+
report(node);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
listeners.push([
|
|
239
|
+
common_1.AstNodeTypes.LITERAL,
|
|
240
|
+
(node) => check(node, common_1.AstNode.readField({ node, key: 'value' })),
|
|
241
|
+
]);
|
|
242
|
+
listeners.push([
|
|
243
|
+
common_1.AstNodeTypes.TEMPLATE_ELEMENT,
|
|
244
|
+
(node) => {
|
|
245
|
+
const value = common_1.AstNode.readField({ node, key: 'value' });
|
|
246
|
+
const template = common_1.AstNode.readField({ node, key: 'parent' });
|
|
247
|
+
const parts = common_1.AstNode.readField({ node: template, key: 'quasis' });
|
|
248
|
+
// The first part is where a URL starts, and the template is what stands in a position.
|
|
249
|
+
if (common_1.AstNode.isNode(template) && Array.isArray(parts) && parts[0] === node) {
|
|
250
|
+
check(template, common_1.AstNode.readField({ node: value, key: 'cooked' }));
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
]);
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
case 'import': {
|
|
257
|
+
const check = (node) => {
|
|
258
|
+
const source = common_1.AstNode.readField({ node, key: 'source' });
|
|
259
|
+
const name = common_1.AstNode.readField({ node: source, key: 'value' });
|
|
260
|
+
if (typeof name === 'string' &&
|
|
261
|
+
match.sources.includes(name) &&
|
|
262
|
+
!PreferFrameworkApiRule.isTypeOnly({ node })) {
|
|
263
|
+
report(node);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
listeners.push([common_1.AstNodeTypes.IMPORT_DECLARATION, check]);
|
|
267
|
+
listeners.push([common_1.AstNodeTypes.IMPORT_EXPRESSION, check]);
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
default: {
|
|
271
|
+
// The other types are hook rows of `BrowserApiPatterns`; as an `extra` row they match nothing here.
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return listeners;
|
|
277
|
+
}
|
|
278
|
+
/** The name of `a.name` or `a['name']`; empty for `a[name]`, whose name the file does not state. */
|
|
279
|
+
static memberName(opts) {
|
|
280
|
+
const property = common_1.AstNode.readField({ node: opts.node, key: 'property' });
|
|
281
|
+
const computed = common_1.AstNode.readField({ node: opts.node, key: 'computed' }) === true;
|
|
282
|
+
const name = common_1.AstNode.readField({ node: property, key: computed ? 'value' : 'name' });
|
|
283
|
+
return typeof name === 'string' ? name : '';
|
|
284
|
+
}
|
|
285
|
+
/** The field each type of an `extra` row cannot do without. */
|
|
286
|
+
static { this.ROW_FIELDS = new Map([
|
|
287
|
+
['path', 'paths'],
|
|
288
|
+
['member', 'objects'],
|
|
289
|
+
['selector', 'selector'],
|
|
290
|
+
['literal', 'pattern'],
|
|
291
|
+
['import', 'sources'],
|
|
292
|
+
]); }
|
|
293
|
+
/** Whether an `extra` entry is a row this rule can run. The option schema refuses the others before it gets here. */
|
|
294
|
+
static isRow(value) {
|
|
295
|
+
const match = common_1.AstNode.readField({ node: value, key: 'match' });
|
|
296
|
+
const field = PreferFrameworkApiRule.ROW_FIELDS.get(common_1.AstNode.getType({ node: match }));
|
|
297
|
+
return (typeof common_1.AstNode.readField({ node: value, key: 'id' }) === 'string' &&
|
|
298
|
+
typeof common_1.AstNode.readField({ node: value, key: 'message' }) === 'string' &&
|
|
299
|
+
field !== undefined &&
|
|
300
|
+
common_1.AstNode.readField({ node: match, key: field }) !== undefined);
|
|
301
|
+
}
|
|
302
|
+
/** The schema of the `match` of an `extra` row: one shape per type, each with its own field required. */
|
|
303
|
+
static matchSchema() {
|
|
304
|
+
const text = { type: 'string' };
|
|
305
|
+
const list = { type: 'array', items: text };
|
|
306
|
+
const flag = { type: 'boolean' };
|
|
307
|
+
const shapes = [
|
|
308
|
+
{ type: 'path' },
|
|
309
|
+
{ type: 'member', more: { property: text, isReadOnly: flag } },
|
|
310
|
+
{ type: 'selector' },
|
|
311
|
+
{ type: 'literal', more: { isFallbackOnly: flag } },
|
|
312
|
+
{ type: 'import' },
|
|
313
|
+
];
|
|
314
|
+
return {
|
|
315
|
+
oneOf: shapes.map(({ type, more }) => {
|
|
316
|
+
const field = PreferFrameworkApiRule.ROW_FIELDS.get(type) ?? type;
|
|
317
|
+
const isText = type === 'selector' || type === 'literal';
|
|
318
|
+
return {
|
|
319
|
+
type: 'object',
|
|
320
|
+
properties: { type: { enum: [type] }, [field]: isText ? text : list, ...more },
|
|
321
|
+
required: ['type', field],
|
|
322
|
+
additionalProperties: false,
|
|
323
|
+
};
|
|
324
|
+
}),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Whether `node` is assigned to or deleted, rather than read. A member that a destructuring pattern writes
|
|
329
|
+
* (`[process.env.APP_ENV_A] = values`) counts as a read and is reported.
|
|
330
|
+
*/
|
|
331
|
+
static isWritten(opts) {
|
|
332
|
+
const { node } = opts;
|
|
333
|
+
const parent = common_1.AstNode.readField({ node, key: 'parent' });
|
|
334
|
+
const type = common_1.AstNode.getType({ node: parent });
|
|
335
|
+
if (type === common_1.AstNodeTypes.ASSIGNMENT_EXPRESSION) {
|
|
336
|
+
return common_1.AstNode.readField({ node: parent, key: 'left' }) === node;
|
|
337
|
+
}
|
|
338
|
+
return (type === common_1.AstNodeTypes.UNARY_EXPRESSION &&
|
|
339
|
+
common_1.AstNode.readField({ node: parent, key: 'operator' }) === 'delete');
|
|
340
|
+
}
|
|
341
|
+
static { this.FALLBACK_KEYS = new Set(['default', 'defaultValue', 'fallback']); }
|
|
342
|
+
/** Whether `node` is the value used when another is missing. */
|
|
343
|
+
static isFallback(opts) {
|
|
344
|
+
const { node } = opts;
|
|
345
|
+
const parent = common_1.AstNode.readField({ node, key: 'parent' });
|
|
346
|
+
const type = common_1.AstNode.getType({ node: parent });
|
|
347
|
+
const isRight = common_1.AstNode.readField({ node: parent, key: 'right' }) === node;
|
|
348
|
+
if (type === common_1.AstNodeTypes.LOGICAL_EXPRESSION) {
|
|
349
|
+
return isRight && common_1.AstNode.readField({ node: parent, key: 'operator' }) !== '&&';
|
|
350
|
+
}
|
|
351
|
+
if (type === common_1.AstNodeTypes.ASSIGNMENT_PATTERN) {
|
|
352
|
+
return isRight;
|
|
353
|
+
}
|
|
354
|
+
if (type === common_1.AstNodeTypes.ASSIGNMENT_EXPRESSION) {
|
|
355
|
+
const operator = common_1.AstNode.readField({ node: parent, key: 'operator' });
|
|
356
|
+
return isRight && (operator === '??=' || operator === '||=');
|
|
357
|
+
}
|
|
358
|
+
return (type === common_1.AstNodeTypes.PROPERTY &&
|
|
359
|
+
common_1.AstNode.readField({ node: parent, key: 'value' }) === node &&
|
|
360
|
+
PreferFrameworkApiRule.FALLBACK_KEYS.has(common_1.AstNode.readField({
|
|
361
|
+
node: common_1.AstNode.readField({ node: parent, key: 'key' }),
|
|
362
|
+
key: 'name',
|
|
363
|
+
})));
|
|
364
|
+
}
|
|
365
|
+
/** Whether an import declaration brings in types only. */
|
|
366
|
+
static isTypeOnly(opts) {
|
|
367
|
+
const { node } = opts;
|
|
368
|
+
const specifiers = common_1.AstNode.readField({ node, key: 'specifiers' });
|
|
369
|
+
const isType = (item) => common_1.AstNode.readField({ node: item, key: 'importKind' }) === 'type';
|
|
370
|
+
return (isType(node) ||
|
|
371
|
+
(Array.isArray(specifiers) && specifiers.length > 0 && specifiers.every(isType)));
|
|
372
|
+
}
|
|
373
|
+
/** One listener object: two rows that visit the same node both run. */
|
|
374
|
+
static merge(opts) {
|
|
375
|
+
const byKey = new Map();
|
|
376
|
+
for (const [key, listener] of opts.listeners) {
|
|
377
|
+
byKey.set(key, [...(byKey.get(key) ?? []), listener]);
|
|
378
|
+
}
|
|
379
|
+
return Object.fromEntries([...byKey].map(([key, all]) => [
|
|
380
|
+
key,
|
|
381
|
+
(node) => {
|
|
382
|
+
for (const listener of all) {
|
|
383
|
+
listener(node);
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
]));
|
|
387
|
+
}
|
|
388
|
+
static { this.RULE = {
|
|
389
|
+
meta: {
|
|
390
|
+
type: 'suggestion',
|
|
391
|
+
schema: [
|
|
392
|
+
{
|
|
393
|
+
type: 'object',
|
|
394
|
+
properties: {
|
|
395
|
+
hooks: { type: 'boolean' },
|
|
396
|
+
ignore: { type: 'array', items: { type: 'string' } },
|
|
397
|
+
extra: {
|
|
398
|
+
type: 'array',
|
|
399
|
+
items: {
|
|
400
|
+
type: 'object',
|
|
401
|
+
properties: {
|
|
402
|
+
id: { type: 'string' },
|
|
403
|
+
message: { type: 'string' },
|
|
404
|
+
dependsOn: { type: 'string' },
|
|
405
|
+
owner: { type: 'string' },
|
|
406
|
+
match: PreferFrameworkApiRule.matchSchema(),
|
|
407
|
+
},
|
|
408
|
+
required: ['id', 'match', 'message'],
|
|
409
|
+
additionalProperties: false,
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
additionalProperties: false,
|
|
414
|
+
},
|
|
415
|
+
],
|
|
416
|
+
messages: {
|
|
417
|
+
framework: "Use {{hooks}} instead of {{written}}, the hook owns the subscribe and the cleanup: `import { {{name}} } from '{{package}}';`",
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
create: (context) => {
|
|
421
|
+
const manifest = framework_exports_1.FrameworkExports.manifestFor({ directory: (0, node_path_1.dirname)(context.filename) });
|
|
422
|
+
const options = context.options[0];
|
|
423
|
+
const ignored = common_1.AstNode.readField({ node: options, key: 'ignore' });
|
|
424
|
+
const ignore = new Set(Array.isArray(ignored) ? ignored : []);
|
|
425
|
+
const added = common_1.AstNode.readField({ node: options, key: 'extra' });
|
|
426
|
+
const rows = PreferFrameworkApiRule.rowsFor({
|
|
427
|
+
filename: context.filename,
|
|
428
|
+
manifest,
|
|
429
|
+
extra: Array.isArray(added)
|
|
430
|
+
? added.filter((row) => PreferFrameworkApiRule.isRow(row))
|
|
431
|
+
: [],
|
|
432
|
+
ignore,
|
|
433
|
+
});
|
|
434
|
+
// The package that ships the hooks writes them with the browser APIs they wrap.
|
|
435
|
+
const patterns = common_1.AstNode.readField({ node: options, key: 'hooks' }) === false ||
|
|
436
|
+
manifest?.name === browser_api_patterns_1.BrowserApiPatterns.PACKAGE
|
|
437
|
+
? []
|
|
438
|
+
: browser_api_patterns_1.BrowserApiPatterns.TABLE.filter((pattern) => !ignore.has(pattern.id));
|
|
439
|
+
const report = (opts) => {
|
|
440
|
+
const { node, pattern } = opts;
|
|
441
|
+
const subscribes = pattern.match.type === 'listener' || pattern.match.type === 'new';
|
|
442
|
+
if (!PreferFrameworkApiRule.isInReact({ context, node, subscribes })) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
context.report({
|
|
446
|
+
node,
|
|
447
|
+
messageId: 'framework',
|
|
448
|
+
data: {
|
|
449
|
+
hooks: pattern.hooks,
|
|
450
|
+
// One import line, for the first hook the row suggests (the `useX` words are in backticks).
|
|
451
|
+
name: /`(use\w+)`/.exec(pattern.hooks)?.[1],
|
|
452
|
+
package: browser_api_patterns_1.BrowserApiPatterns.PACKAGE,
|
|
453
|
+
written: pattern.written,
|
|
454
|
+
},
|
|
455
|
+
});
|
|
456
|
+
};
|
|
457
|
+
const hooks = {
|
|
458
|
+
// `window.matchMedia`, `window.innerWidth`, `navigator.onLine`, `navigator.clipboard.writeText`...
|
|
459
|
+
[common_1.AstNodeTypes.MEMBER_EXPRESSION]: (node) => {
|
|
460
|
+
const path = PreferFrameworkApiRule.pathOf({ node });
|
|
461
|
+
if (path === undefined || !PreferFrameworkApiRule.isGlobal({ context, node, path })) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const bare = PreferFrameworkApiRule.withoutGlobalRoot({ path });
|
|
465
|
+
for (const pattern of patterns) {
|
|
466
|
+
const { match } = pattern;
|
|
467
|
+
if ((match.type === 'global' &&
|
|
468
|
+
GLOBAL_OBJECTS.includes(path.split('.')[0] ?? '') &&
|
|
469
|
+
path.split('.').length === 2 &&
|
|
470
|
+
match.names.includes(bare)) ||
|
|
471
|
+
(match.type === 'path' && match.paths.includes(bare))) {
|
|
472
|
+
report({ node, pattern });
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
},
|
|
476
|
+
[common_1.AstNodeTypes.CALL_EXPRESSION]: (node) => {
|
|
477
|
+
const callee = common_1.AstNode.readField({ node, key: 'callee' });
|
|
478
|
+
const args = common_1.AstNode.readField({ node, key: 'arguments' });
|
|
479
|
+
const first = Array.isArray(args) ? args[0] : undefined;
|
|
480
|
+
const event = common_1.AstNode.readField({ node: first, key: 'value' });
|
|
481
|
+
// `matchMedia(...)`, `confirm(...)` with no `window.`.
|
|
482
|
+
if (common_1.AstNode.getType({ node: callee }) === common_1.AstNodeTypes.IDENTIFIER) {
|
|
483
|
+
const name = common_1.AstNode.readField({ node: callee, key: 'name' });
|
|
484
|
+
for (const pattern of patterns) {
|
|
485
|
+
if (pattern.match.type === 'global' &&
|
|
486
|
+
typeof name === 'string' &&
|
|
487
|
+
pattern.match.names.includes(name) &&
|
|
488
|
+
PreferFrameworkApiRule.isGlobal({ context, node, path: name })) {
|
|
489
|
+
report({ node, pattern });
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
// `<object>.addEventListener('<event>', ...)`.
|
|
494
|
+
const path = PreferFrameworkApiRule.pathOf({ node: callee });
|
|
495
|
+
if (path === undefined || typeof event !== 'string') {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const parts = path.split('.');
|
|
499
|
+
const method = parts.pop();
|
|
500
|
+
const owner = parts.join('.');
|
|
501
|
+
if (method !== 'addEventListener') {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
// A listener on a socket, an element or an `EventTarget` of the author's own is not the browser's.
|
|
505
|
+
const isOwnerGlobal = owner === '' ||
|
|
506
|
+
(PreferFrameworkApiRule.isGlobal({ context, node, path: owner }) &&
|
|
507
|
+
GLOBAL_TARGETS.includes(owner));
|
|
508
|
+
for (const pattern of patterns) {
|
|
509
|
+
const { match } = pattern;
|
|
510
|
+
if (match.type === 'listener' &&
|
|
511
|
+
match.events.includes(event) &&
|
|
512
|
+
isOwnerGlobal &&
|
|
513
|
+
(match.objects === undefined || owner === '' || match.objects.includes(owner))) {
|
|
514
|
+
report({ node, pattern });
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
[common_1.AstNodeTypes.NEW_EXPRESSION]: (node) => {
|
|
519
|
+
const path = PreferFrameworkApiRule.pathOf({
|
|
520
|
+
node: common_1.AstNode.readField({ node, key: 'callee' }),
|
|
521
|
+
});
|
|
522
|
+
if (path === undefined || !PreferFrameworkApiRule.isGlobal({ context, node, path })) {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
const bare = PreferFrameworkApiRule.withoutGlobalRoot({ path });
|
|
526
|
+
for (const pattern of patterns) {
|
|
527
|
+
if (pattern.match.type === 'new' && pattern.match.callees.includes(bare)) {
|
|
528
|
+
report({ node, pattern });
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
};
|
|
533
|
+
return PreferFrameworkApiRule.merge({
|
|
534
|
+
listeners: [
|
|
535
|
+
...Object.entries(hooks),
|
|
536
|
+
...PreferFrameworkApiRule.rowListeners({ context, rows }),
|
|
537
|
+
],
|
|
538
|
+
});
|
|
539
|
+
},
|
|
540
|
+
}; }
|
|
541
|
+
/**
|
|
542
|
+
* `hooks: false` leaves out the rows of `BrowserApiPatterns`, for code that is not React: a `use...` function of another
|
|
543
|
+
* library reads as a hook. An entry that sets options replaces the earlier ones, so it states `hooks` again.
|
|
544
|
+
*/
|
|
545
|
+
static build(opts = {}) {
|
|
546
|
+
const { severity = 'error', hooks, ignore, extra } = opts;
|
|
547
|
+
// Only what was given: the option schema refuses a key whose value is `undefined`.
|
|
548
|
+
const options = {};
|
|
549
|
+
if (hooks !== undefined) {
|
|
550
|
+
options.hooks = hooks;
|
|
551
|
+
}
|
|
552
|
+
if (ignore !== undefined) {
|
|
553
|
+
options.ignore = [...ignore];
|
|
554
|
+
}
|
|
555
|
+
if (extra !== undefined) {
|
|
556
|
+
options.extra = [...extra];
|
|
557
|
+
}
|
|
558
|
+
return Object.keys(options).length === 0 ? [severity] : [severity, options];
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
exports.PreferFrameworkApiRule = PreferFrameworkApiRule;
|
|
562
|
+
//# sourceMappingURL=prefer-framework-api.rule.js.map
|