Skip to content

Commit

Permalink
feat(linter): handle cjs module.exports.foo = bar and `exports.foo …
Browse files Browse the repository at this point in the history
…= bar` (#2492)
  • Loading branch information
Boshen authored Feb 24, 2024
1 parent d0a9c46 commit f64c7e0
Show file tree
Hide file tree
Showing 291 changed files with 65 additions and 647 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static_assertions = { version = "1.1.0" }
stacker = { version = "0.1.15" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }
insta = { version = "1.34.0", features = ["glob"] }
insta = { version = "1.35.1", features = ["glob"] }
codspeed-criterion-compat = { version = "2.3.3", default-features = false }
glob = { version = "0.3.1" }
mime_guess = { version = "2.0.4" }
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ impl<'a> MemberExpression<'a> {
}
}

pub fn static_property_info(&'a self) -> Option<(Span, &'a str)> {
pub fn static_property_info(&self) -> Option<(Span, &str)> {
match self {
MemberExpression::ComputedMemberExpression(expr) => match &expr.expression {
Expression::StringLiteral(lit) => Some((lit.span, &lit.value)),
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_linter/fixtures/import/oxc_named.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports.foo = 1;
exports.bar = 1;
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/import/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use oxc_diagnostics::{
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::{Atom, Span};
use oxc_span::Span;
use oxc_syntax::module_record::ImportImportName;

use crate::{context::LintContext, rule::Rule};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-import(default): No default export found in imported module {0:?}")]
#[diagnostic(severity(warning), help("does {0:?} have the default export?"))]
struct DefaultDiagnostic(Atom, #[label] pub Span);
struct DefaultDiagnostic(String, #[label] pub Span);

/// <https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/default.md>
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Rule for Default {
&& !remote_module_record_ref.exported_bindings.contains_key("default")
{
ctx.diagnostic(DefaultDiagnostic(
specifier.clone(),
specifier.to_string(),
import_entry.module_request.span(),
));
}
Expand Down
16 changes: 10 additions & 6 deletions crates/oxc_linter/src/rules/import/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use oxc_diagnostics::{
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::{Atom, Span};
use oxc_span::Span;
use oxc_syntax::module_record::{ExportImportName, ImportImportName};

use crate::{context::LintContext, rule::Rule};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-import(named): named import {0:?} not found")]
#[diagnostic(severity(warning), help("does {1:?} have the export {0:?}?"))]
struct NamedDiagnostic(Atom, Atom, #[label] pub Span);
struct NamedDiagnostic(String, String, #[label] pub Span);

/// <https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/named.md>
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -56,8 +56,8 @@ impl Rule for Named {
continue;
}
ctx.diagnostic(NamedDiagnostic(
import_name.name().clone(),
specifier.clone(),
import_name.name().to_string(),
specifier.to_string(),
import_name.span(),
));
}
Expand All @@ -80,8 +80,8 @@ impl Rule for Named {
continue;
}
ctx.diagnostic(NamedDiagnostic(
import_name.name().clone(),
specifier.clone(),
import_name.name().to_string(),
specifier.to_string(),
import_name.span(),
));
}
Expand Down Expand Up @@ -164,6 +164,8 @@ fn test() {
// "import { foo } from './export-all'",
// TypeScript export assignment
"import x from './typescript-export-assign-object'",
// oxc
"import { foo, bar } from './oxc_named'",
];

let fail = vec![
Expand Down Expand Up @@ -198,6 +200,8 @@ fn test() {
// Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead.
"import { NotExported } from './typescript-export-assign-object'",
"import { FooBar } from './typescript-export-assign-object'",
// oxc
"import { baz, quaz } from './oxc_named'",
];

Tester::new(Named::NAME, pass, fail)
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Runtime {
.map(|p| TsconfigOptions { config_file: p, references: TsconfigReferences::Auto });
Resolver::new(ResolveOptions {
extensions: VALID_EXTENSIONS.iter().map(|ext| format!(".{ext}")).collect(),
condition_names: vec!["require".into(), "module".into()],
condition_names: vec!["module".into(), "require".into()],
tsconfig,
..ResolveOptions::default()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: adjacent_overload_signatures
---

typescript-eslint(adjacent-overload-signatures): All "foo" signatures should be adjacent.
╭─[adjacent_overload_signatures.tsx:3:18]
2function foo(s: string);
Expand Down Expand Up @@ -369,4 +368,3 @@ expression: adjacent_overload_signatures
· ──────────
6 │ }
╰────

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/alt_text.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: alt_text
---

eslint-plugin-jsx-a11y(alt-text): Missing `alt` attribute.
╭─[alt_text.tsx:1:1]
1<img />;
Expand Down Expand Up @@ -450,4 +449,3 @@ expression: alt_text
· ──────────────────────
╰────
help: <input> elements with type="image" must have a text alternative through the `alt`, `aria-label`, or `aria-labelledby` prop.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/anchor_has_content.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: anchor_has_content
---

eslint-plugin-jsx-a11y(anchor-has-content): Missing accessible content when using `a` elements.
╭─[anchor_has_content.tsx:1:1]
1<a />
Expand Down Expand Up @@ -30,4 +29,3 @@ expression: anchor_has_content
· ────────
╰────
help: Provide screen reader accessible content when using `a` elements.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/anchor_is_valid.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: anchor_is_valid
---

eslint-plugin-jsx-a11y(anchor-is-valid): Missing `href` attribute for the `a` element.
╭─[anchor_is_valid.tsx:1:2]
1<a />
Expand Down Expand Up @@ -92,4 +91,3 @@ expression: anchor_is_valid
· ────
╰────
help: Use a `button` element instead of an `a` element.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/approx_constant.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: approx_constant
---

oxc(approx-constant): Approximate value of `PI` found.
╭─[approx_constant.tsx:1:29]
1const getArea = (radius) => 3.141 * radius * radius;
Expand Down Expand Up @@ -65,4 +64,3 @@ expression: approx_constant
· ────────
╰────
help: Use `Math.SQRT2` instead

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: aria_activedescendant_has_tabindex
---

eslint-plugin-jsx-a11y(aria-activedescendant-has-tabindex): Enforce elements with aria-activedescendant are tabbable.
╭─[aria_activedescendant_has_tabindex.tsx:1:2]
1<div aria-activedescendant={someID} />;
Expand All @@ -16,4 +15,3 @@ expression: aria_activedescendant_has_tabindex
· ───────────────
╰────
help: An element that manages focus with `aria-activedescendant` must have a tabindex.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/aria_props.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: aria_props
---

eslint-plugin-jsx-a11y(aria-props): Invalid ARIA prop.
╭─[aria_props.tsx:1:6]
1<div aria-="foobar" />
Expand All @@ -23,4 +22,3 @@ expression: aria_props
· ───────────────────────────────
╰────
help: `aria-skldjfaria-klajsd` is an invalid ARIA attribute.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/aria_role.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: aria_role
---

eslint-plugin-jsx-a11y(aria-role): Elements with ARIA roles must use a valid, non-abstract ARIA role.
╭─[aria_role.tsx:1:11]
1<div role='foobar' />
Expand Down Expand Up @@ -113,4 +112,3 @@ expression: aria_role
· ────────
╰────
help: Set a valid, non-abstract ARIA role for element with ARIA, `Button` is an invalid aria role

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: aria_unsupported_elements
---

eslint-plugin-jsx-a11y(aria-unsupported-elements): This element does not support ARIA roles, states and properties.
╭─[aria_unsupported_elements.tsx:1:7]
1<base role {...props} />
Expand Down Expand Up @@ -226,4 +225,3 @@ expression: aria_unsupported_elements
· ───────────
╰────
help: Try removing the prop `aria-hidden`.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/array_callback_return.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: array_callback_return
---

eslint(array-callback-return): Missing return on some path for array method "Array.from"
╭─[array_callback_return.tsx:1:26]
1Array.from(x, function() {})
Expand Down Expand Up @@ -792,4 +791,3 @@ expression: array_callback_return
· ────────────────────────
╰────
help: Array method "Array.prototype.filter" needs to have valid return on all code paths

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/array_type.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: array_type
---

typescript-eslint(array-type): Array type using 'Array<number>' is forbidden. Use 'number[]' instead.
╭─[array_type.tsx:1:8]
1let a: Array<number> = [];
Expand Down Expand Up @@ -565,4 +564,3 @@ expression: array_type
1const foo: ReadonlyArray<new (...args: any[]) => void> = [];
· ───────────────────────────────────────────
╰────

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/autocomplete_valid.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: autocomplete_valid
---

eslint-plugin-jsx-a11y(autocomplete-valid): `foo` is not a valid value for autocomplete.
╭─[autocomplete_valid.tsx:1:20]
1<input type='text' autocomplete='foo' />;
Expand Down Expand Up @@ -44,4 +43,3 @@ expression: autocomplete_valid
· ──────────────────
╰────
help: Change `baz` to a valid value for autocomplete.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: bad_array_method_on_arguments
---

deepscan(bad-array-method-on-arguments): Bad array method on arguments
╭─[bad_array_method_on_arguments.tsx:1:16]
1function fn() {arguments['map'](() => {})}
Expand Down Expand Up @@ -233,4 +232,3 @@ expression: bad_array_method_on_arguments
· ───────────────────────
╰────
help: The 'arguments' object does not have '@@iterator()' method. If an array method was intended, consider converting the 'arguments' object to an array or using ES6 rest parameter instead.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/bad_bitwise_operator.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: bad_bitwise_operator
---

deepscan(bad-bitwise-operator): Bad bitwise operator
╭─[bad_bitwise_operator.tsx:1:9]
1var a = obj & obj.a
Expand Down Expand Up @@ -107,4 +106,3 @@ expression: bad_bitwise_operator
· ──────────────────
╰────
help: Bitwise operator '|=' seems unintended. Consider using non-compound assignment and logical operator '||' instead.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/bad_char_at_comparison.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: bad_char_at_comparison
---

deepscan(bad-char-at-comparison): Invalid comparison with `charAt` method
╭─[bad_char_at_comparison.tsx:1:1]
1a.charAt(4) === 'aa'
Expand Down Expand Up @@ -56,4 +55,3 @@ expression: bad_char_at_comparison
· ╰── `charAt` called here
╰────
help: `String.prototype.charAt` returns a string of length 1. If the return value is compared with a string of length greater than 1, the comparison will always be false.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/bad_comparison_sequence.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: bad_comparison_sequence
---

deepscan(bad-comparison-sequence): Bad comparison sequence
╭─[bad_comparison_sequence.tsx:1:5]
1if (a == b == c) { console.log('foo') }
Expand Down Expand Up @@ -254,4 +253,3 @@ expression: bad_comparison_sequence
· ───────────
╰────
help: Comparison result should not be used directly as an operand of another comparison. If you need to compare three or more operands, you should connect each comparison operation with logical AND operator (`&&`)

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/bad_min_max_func.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: bad_min_max_func
---

deepscan(bad-min-max-func): Math.min and Math.max combination leads to constant result
╭─[bad_min_max_func.tsx:1:1]
1Math.min(Math.max(100, x), 0)
Expand Down Expand Up @@ -58,4 +57,3 @@ expression: bad_min_max_func
· ──────────────────────────────────
╰────
help: This evaluates to 155.0 because of the incorrect `Math.min`/`Math.max` combination

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: bad_object_literal_comparison
---

deepscan(bad-object-literal-comparison): Unexpected object literal comparison.
╭─[bad_object_literal_comparison.tsx:1:5]
1if (y === {}) { }
Expand Down Expand Up @@ -72,4 +71,3 @@ expression: bad_object_literal_comparison
· ────────────
╰────
help: This comparison will always return true as array literals are never equal to each other. Consider using `Array.length` if empty checking was intended.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/bad_replace_all_arg.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: bad_replace_all_arg
---

deepscan(bad-replace-all-arg): Global flag (g) is missing in the regular expression supplied to the `replaceAll` method.
╭─[bad_replace_all_arg.tsx:1:12]
1withSpaces.replaceAll(/\s+/, ',');
Expand Down Expand Up @@ -80,4 +79,3 @@ expression: bad_replace_all_arg
5
╰────
help: To replace all occurrences of a string, use the `replaceAll` method with the global flag (g) in the regular expression.

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/ban_ts_comment.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: ban_ts_comment
---

typescript-eslint(ban-ts-comment): Do not use @ts-expect-error because it alters compilation errors.
╭─[ban_ts_comment.tsx:1:3]
1// @ts-expect-error
Expand Down Expand Up @@ -286,4 +285,3 @@ expression: ban_ts_comment
1// @ts-check: TS1234
· ──────────────────
╰────

2 changes: 0 additions & 2 deletions crates/oxc_linter/src/snapshots/ban_tslint_comment.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/oxc_linter/src/tester.rs
expression: ban_tslint_comment
---

typescript-eslint(ban-tslint-comment): tslint comment detected: "tslint:disable"
╭─[ban_tslint_comment.tsx:1:1]
1/* tslint:disable */
Expand Down Expand Up @@ -53,4 +52,3 @@ expression: ban_tslint_comment
3console.log(woah);
4
╰────

Loading

0 comments on commit f64c7e0

Please sign in to comment.