Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions crates/oxc_linter/src/rules/eslint/no_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ pub struct NoIterator;

declare_oxc_lint!(
/// ### What it does
///
/// Disallow the use of the `__iterator__` property
///
/// ### Why is this bad?
///
/// The `__iterator__` property was a SpiderMonkey extension to JavaScript
/// that could be used to create custom iterators that are compatible with
/// JavaScript’s for in and for each constructs. However, this property is
Expand Down Expand Up @@ -55,7 +57,7 @@ declare_oxc_lint!(
NoIterator,
eslint,
restriction,
pending // TODO: suggestion
suggestion
);

impl Rule for NoIterator {
Expand All @@ -65,10 +67,11 @@ impl Rule for NoIterator {
};
if let Some(static_property_name) = member_expression.static_property_name() {
if static_property_name == "__iterator__" {
ctx.diagnostic(no_iterator_diagnostic(Span::new(
member_expression.span().start,
member_expression.span().end,
)));
let mem_span = member_expression.span();
let obj_span = member_expression.object().span();
ctx.diagnostic_with_suggestion(no_iterator_diagnostic(mem_span), |fixer| {
fixer.replace(Span::new(obj_span.end, mem_span.end), "[Symbol.iterator]")
});
}
}
}
Expand All @@ -82,8 +85,7 @@ fn test() {
"var a = test[__iterator__];",
"var __iterator__ = null;",
"foo[`__iterator`] = null;",
"foo[`__iterator__
`] = null;",
"foo[`__iterator__\n`] = null;",
];

let fail = vec![
Expand All @@ -94,5 +96,18 @@ fn test() {
"test[`__iterator__`] = function () {};",
];

Tester::new(NoIterator::NAME, NoIterator::PLUGIN, pass, fail).test_and_snapshot();
let fix = vec![
("var a = test.__iterator__;", "var a = test[Symbol.iterator];"),
(
"Foo.prototype.__iterator__ = function() {};",
"Foo.prototype[Symbol.iterator] = function() {};",
),
("var a = test['__iterator__'];", "var a = test[Symbol.iterator];"),
("var a = test[`__iterator__`];", "var a = test[Symbol.iterator];"),
("test[`__iterator__`] = function () {};", "test[Symbol.iterator] = function () {};"),
];

Tester::new(NoIterator::NAME, NoIterator::PLUGIN, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}