sindresorhus/eslint-plugin-unicorn

Rule proposal: `require-proxy-set-returns-true`

クローズ

#2,178 opened on 2023/07/19

 (2 件のコメント) (2 件のリアクション) (0 人の担当者)JavaScript (468 件のフォーク)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 個のスター)
PR merge metrics
 (平均マージ 1d 16h) (30d で 399 merged PRs)

説明

Description

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/set#return_value

If the set() method returns false, and the assignment happened in strict-mode code, a TypeError will be thrown.

The following code throws.

(function () {
    "use strict";
    const proxy = new Proxy({}, {
        set(target, property, value) {
        }
    });
    proxy.foo = 'bar'
})()

// Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'foo'

This rule enforce the set to explicitly return true. This rule also allow return Reflect.set() call.

Fail

const proxy = new Proxy({}, {
    set(target, property, value) {
        target[property] = value;
    }
});
const proxy = new Proxy({}, {
    set(target, property, value) {
        target[property] = value;
        return 1; // Will work, but should be `true` to be clear.
    }
});

Pass

const proxy = new Proxy({}, {
    set(target, property, value) {
        target[property] = value;
        return true;
    }
});
const proxy = new Proxy({}, {
    set(target, property, value) {
        return Reflect.set(...arguments);
    }
});

Additional Info

No response

コントリビューターガイド