Skip to content

Prefer Native Collection Copy

ErrorAuto-fixableRoblox
small-rules/prefer-native-collection-copy

Prefer table.clone over a manual shallow Map or Set copy loop.

Rationale

A loop that inserts every Map or Set value unchanged is a shallow copy written the long way. table.clone states that operation directly.

The rule only reports when syntax and scope analysis prove that both collections have the same native kind. It skips opaque iterables, weak collections, conversions, filtering, and transformed values. A report may omit its fix when some other table shadows the global, or attached comments make it unsafe to delete the loop.

Diagnostic Messages

preferNativeCollectionCopy
Use table.clone for this shallow collection copy.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/prefer-native-collection-copy": "error"
}
}

Examples

manual shallow Map copy
const current = new Map<number, NodePosition>(entries);
const updated = new Map<number, NodePosition>();
for (const [key, value] of current) updated.set(key, value);

After auto-fix

const current = new Map<number, NodePosition>(entries);
const updated = table.clone(current);
native shallow collection copy
const current = new Map(entries);
const updated = table.clone(current);