Skip to content

Commit

Permalink
feat(es/lints): Add no-prototype-builtins rule (#8684)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturAralin committed Mar 11, 2024
1 parent 8c02489 commit a5dbb17
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"jsc": {
"lints": {
"noPrototypeBuiltins": [
"error"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
foo.hasOwnProperty('bar');

foo.isPrototypeOf('bar');

foo.propertyIsEnumerable('bar');

foo.bar.hasOwnProperty('bar');

foo.bar.baz.isPrototypeOf('bar');

bar?.foo?.hasOwnProperty('bar');

foo?.bar.hasOwnProperty('baz');

foo.hasOwnProperty?.('bar');

foo?.hasOwnProperty('bar').baz;

foo.hasOwnProperty('bar')?.baz;

(a,b).hasOwnProperty('bar');

(foo?.hasOwnProperty)('bar');

(((foo?.hasOwnProperty)))('dlya-tex-kto-dumaet-cho-on-samiy-umniy');

(foo?.anotherProp, foo?.hasOwnProperty)('bar');

(foo.hasOwnProperty('ok'), foo?.hasOwnProperty)('bar');

foo['hasOwnProperty']('bar');

foo[`isPrototypeOf`]('bar').baz;

// valid

Object.prototype.hasOwnProperty.call(foo, 'bar');
Object.prototype.isPrototypeOf.call(foo, 'bar');
Object.prototype.propertyIsEnumerable.call(foo, 'bar');
Object.prototype.hasOwnProperty.apply(foo, ['bar']);
Object.prototype.isPrototypeOf.apply(foo, ['bar']);
Object.prototype.propertyIsEnumerable.apply(foo, ['bar']);
foo.hasOwnProperty;
foo.hasOwnProperty.bar();
foo(hasOwnProperty);
hasOwnProperty(foo, 'bar');
isPrototypeOf(foo, 'bar');
propertyIsEnumerable(foo, 'bar');
({}.hasOwnProperty.call(foo, 'bar'));
({}.isPrototypeOf.call(foo, 'bar'));
({}.propertyIsEnumerable.call(foo, 'bar'));
({}.hasOwnProperty.apply(foo, ['bar']));
({}.isPrototypeOf.apply(foo, ['bar']));
({}.propertyIsEnumerable.apply(foo, ['bar']));
foo[hasOwnProperty]('bar');
foo['HasOwnProperty']('bar');
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[1:1]
1 | foo.hasOwnProperty('bar');
: ^^^^^^^^^^^^^^
2 |
3 | foo.isPrototypeOf('bar');
`----

x Do not access Object.prototype method 'isPrototypeOf' from target object
,-[1:1]
1 | foo.hasOwnProperty('bar');
2 |
3 | foo.isPrototypeOf('bar');
: ^^^^^^^^^^^^^
4 |
5 | foo.propertyIsEnumerable('bar');
`----

x Do not access Object.prototype method 'propertyIsEnumerable' from target object
,-[2:1]
2 |
3 | foo.isPrototypeOf('bar');
4 |
5 | foo.propertyIsEnumerable('bar');
: ^^^^^^^^^^^^^^^^^^^^
6 |
7 | foo.bar.hasOwnProperty('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[4:1]
4 |
5 | foo.propertyIsEnumerable('bar');
6 |
7 | foo.bar.hasOwnProperty('bar');
: ^^^^^^^^^^^^^^
8 |
9 | foo.bar.baz.isPrototypeOf('bar');
`----

x Do not access Object.prototype method 'isPrototypeOf' from target object
,-[6:1]
6 |
7 | foo.bar.hasOwnProperty('bar');
8 |
9 | foo.bar.baz.isPrototypeOf('bar');
: ^^^^^^^^^^^^^
10 |
11 | bar?.foo?.hasOwnProperty('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[8:1]
8 |
9 | foo.bar.baz.isPrototypeOf('bar');
10 |
11 | bar?.foo?.hasOwnProperty('bar');
: ^^^^^^^^^^^^^^
12 |
13 | foo?.bar.hasOwnProperty('baz');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[10:1]
10 |
11 | bar?.foo?.hasOwnProperty('bar');
12 |
13 | foo?.bar.hasOwnProperty('baz');
: ^^^^^^^^^^^^^^
14 |
15 | foo.hasOwnProperty?.('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[12:1]
12 |
13 | foo?.bar.hasOwnProperty('baz');
14 |
15 | foo.hasOwnProperty?.('bar');
: ^^^^^^^^^^^^^^
16 |
17 | foo?.hasOwnProperty('bar').baz;
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[14:1]
14 |
15 | foo.hasOwnProperty?.('bar');
16 |
17 | foo?.hasOwnProperty('bar').baz;
: ^^^^^^^^^^^^^^
18 |
19 | foo.hasOwnProperty('bar')?.baz;
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[16:1]
16 |
17 | foo?.hasOwnProperty('bar').baz;
18 |
19 | foo.hasOwnProperty('bar')?.baz;
: ^^^^^^^^^^^^^^
20 |
21 | (a,b).hasOwnProperty('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[18:1]
18 |
19 | foo.hasOwnProperty('bar')?.baz;
20 |
21 | (a,b).hasOwnProperty('bar');
: ^^^^^^^^^^^^^^
22 |
23 | (foo?.hasOwnProperty)('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[20:1]
20 |
21 | (a,b).hasOwnProperty('bar');
22 |
23 | (foo?.hasOwnProperty)('bar');
: ^^^^^^^^^^^^^^
24 |
25 | (((foo?.hasOwnProperty)))('dlya-tex-kto-dumaet-cho-on-samiy-umniy');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[22:1]
22 |
23 | (foo?.hasOwnProperty)('bar');
24 |
25 | (((foo?.hasOwnProperty)))('dlya-tex-kto-dumaet-cho-on-samiy-umniy');
: ^^^^^^^^^^^^^^
26 |
27 | (foo?.anotherProp, foo?.hasOwnProperty)('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[24:1]
24 |
25 | (((foo?.hasOwnProperty)))('dlya-tex-kto-dumaet-cho-on-samiy-umniy');
26 |
27 | (foo?.anotherProp, foo?.hasOwnProperty)('bar');
: ^^^^^^^^^^^^^^
28 |
29 | (foo.hasOwnProperty('ok'), foo?.hasOwnProperty)('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[26:1]
26 |
27 | (foo?.anotherProp, foo?.hasOwnProperty)('bar');
28 |
29 | (foo.hasOwnProperty('ok'), foo?.hasOwnProperty)('bar');
: ^^^^^^^^^^^^^^
30 |
31 | foo['hasOwnProperty']('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[26:1]
26 |
27 | (foo?.anotherProp, foo?.hasOwnProperty)('bar');
28 |
29 | (foo.hasOwnProperty('ok'), foo?.hasOwnProperty)('bar');
: ^^^^^^^^^^^^^^
30 |
31 | foo['hasOwnProperty']('bar');
`----

x Do not access Object.prototype method 'hasOwnProperty' from target object
,-[28:1]
28 |
29 | (foo.hasOwnProperty('ok'), foo?.hasOwnProperty)('bar');
30 |
31 | foo['hasOwnProperty']('bar');
: ^^^^^^^^^^^^^^^^
32 |
33 | foo[`isPrototypeOf`]('bar').baz;
`----

x Do not access Object.prototype method 'isPrototypeOf' from target object
,-[30:1]
30 |
31 | foo['hasOwnProperty']('bar');
32 |
33 | foo[`isPrototypeOf`]('bar').baz;
: ^^^^^^^^^^^^^^^
34 |
35 | // valid
`----
2 changes: 2 additions & 0 deletions crates/swc_ecma_lints/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ pub struct LintConfig {
pub no_cond_assign: RuleConfig<()>,

#[cfg(feature = "non_critical_lints")]
#[serde(default, alias = "noPrototypeBuiltins")]
pub no_prototype_builtins: RuleConfig<()>,
#[serde(default, alias = "noNewObject")]
pub no_new_object: RuleConfig<()>,
}
5 changes: 5 additions & 0 deletions crates/swc_ecma_lints/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) mod non_critical_lints {
pub mod no_new_symbol;
pub mod no_obj_calls;
pub mod no_param_reassign;
pub mod no_prototype_builtins;
pub mod no_restricted_syntax;
pub mod no_sparse_arrays;
pub mod no_throw_literal;
Expand Down Expand Up @@ -196,6 +197,10 @@ pub fn all(lint_params: LintParams) -> Vec<Box<dyn Rule>> {

rules.extend(no_cond_assign::no_cond_assign(&lint_config.no_cond_assign));

rules.extend(no_prototype_builtins::no_prototype_builtins(
&lint_config.no_prototype_builtins,
));

rules.extend(no_new_object::no_new_object(
unresolved_ctxt,
&lint_config.no_new_object,
Expand Down

0 comments on commit a5dbb17

Please sign in to comment.