Use Array#findLastIndex (#16384)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-06-18 08:00:16 +09:00
1 parent f3c2dbda8a
commit 6d265ec100
5 files changed
+62 -10

No files matched your search

@@ -0,0 +1,20 @@
const arrayFindLastIndex = (isOptionalObject, array, callback) => {
if (isOptionalObject && (array === undefined || array === null)) {
return;
}
if (array.findLastIndex) {
return array.findLastIndex(callback);
}
for (let index = array.length - 1; index >= 0; index--) {
const element = array[index];
if (callback(element, index, array)) {
return index;
}
}
return -1;
};
export default arrayFindLastIndex;
+3 -1
View File
@@ -1,11 +1,13 @@
import transformArrayFindLast from "./transform-array-find-last.js";
import transformArrayFindLastIndex from "./transform-array-find-last-index.js";
import transformObjectHasOwnCall from "./transform-object-has-own.js";
import transformRelativeIndexing from "./transform-relative-indexing.js";
import transformStringReplaceAll from "./transform-string-replace-all.js";
export default [
transformArrayFindLast,
transformArrayFindLastIndex,
transformObjectHasOwnCall,
transformRelativeIndexing,
transformStringReplaceAll,
transformArrayFindLast,
];
@@ -0,0 +1,13 @@
import createMethodCallTransform from "./create-method-call-transform.js";
const transformArrayFindLastIndex = createMethodCallTransform({
methodName: "findLastIndex",
argumentsLength: 1,
functionName: "__arrayFindLastIndex",
functionImplementationUrl: new URL(
"../../shims/array-find-last-index.js",
import.meta.url,
),
});
export default transformArrayFindLastIndex;
+8 -9
View File
@@ -24,16 +24,15 @@ function findCommonAncestor(startNodeAndParents, endNodeAndParents) {
}
function dropRootParents(parents) {
let lastParentIndex = parents.length - 1;
while (true) {
const parent = parents[lastParentIndex];
if (parent?.type === "Program" || parent?.type === "File") {
lastParentIndex--;
} else {
break;
}
const index = parents.findLastIndex(
(node) => node.type !== "Program" && node.type !== "File",
);
if (index === -1) {
return parents;
}
return parents.slice(0, lastParentIndex + 1);
return parents.slice(0, index + 1);
}
function findSiblingAncestors(
+18
View File
@@ -75,3 +75,21 @@ test("Array#findLast", () => {
transform("foo.findLast(callback, thisArgument)"),
).toMatchInlineSnapshot(`"foo.findLast(callback, thisArgument)"`);
});
test("Array#findLastIndex", () => {
expect(transform("foo.findLastIndex(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLastIndex from "<SHIMS>/array-find-last-index.js";
__arrayFindLastIndex( /* isOptionalObject */false, foo, callback);"
`);
expect(transform("foo?.findLastIndex(callback)")).toMatchInlineSnapshot(`
"import __arrayFindLastIndex from "<SHIMS>/array-find-last-index.js";
__arrayFindLastIndex( /* isOptionalObject */true, foo, callback);"
`);
// Don't support
expect(
transform("foo.findLastIndex(callback, thisArgument)"),
).toMatchInlineSnapshot(`"foo.findLastIndex(callback, thisArgument)"`);
});