Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

no-hermes-unsupported-array-methods.ts

45 lines1.6 KB

1import { defineRule } from "@oxlint/plugins";
2​
3// ES2023 change-array-by-copy methods. Hermes does not implement them, and
4// tsconfig targets ESNext, so nothing but this rule stands between a call and a
5// TypeError that is fatal on every mobile launch that reaches it.
6const UNSUPPORTED_METHODS = new Map([
7 ["toSorted", "[...array].sort(...)"],
8 ["toReversed", "[...array].reverse()"],
9 // splice returns the removed elements, so the copy itself is the result.
10 ["toSpliced", "const copy = [...array]; copy.splice(...); use copy"],
11]);
12​
13export default defineRule({
14 meta: {
15 type: "problem",
16 docs: {
17 description:
18 "Disallow ES2023 array-by-copy methods (toSorted, toReversed, toSpliced) in code that runs on Hermes.",
19 },
20 },
21 create(context) {
22 return {
23 CallExpression(node) {
24 if (node.callee.type !== "MemberExpression") return;
25 const { property } = node.callee;
26 const name =
27 property.type === "Identifier"
28 ? property.name
29 : property.type === "Literal" && typeof property.value === "string"
30 ? property.value
31 : property.type === "TemplateLiteral" && property.expressions.length === 0
32 ? (property.quasis[0]?.value.cooked ?? null)
33 : null;
34 if (name === null) return;
35 const replacement = UNSUPPORTED_METHODS.get(name);
36 if (replacement === undefined) return;
37​
38 context.report({
39 node: property,
40 message: `Hermes does not implement Array#${name}. Copy the array first: ${replacement}.`,
41 });
42 },
43 };
44 },
45});