fix(linter) Fix no object as default param with parenthesis (#1244)

This commit is contained in:
Cameron 2023-11-13 00:41:47 +00:00 committed by GitHub
parent 3a92828a0a
commit f3f3383733
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View file

@ -49,7 +49,9 @@ impl Rule for NoObjectAsDefaultParameter {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::AssignmentPattern(assignment_pat) = node.kind() else { return };
let Expression::ObjectExpression(object_expr) = &assignment_pat.right else {
let Expression::ObjectExpression(object_expr) =
&assignment_pat.right.without_parenthesized()
else {
return;
};
@ -98,6 +100,7 @@ fn test() {
r#"const abc = (foo = 123) => {};"#,
r#"const abc = (foo = true) => {};"#,
r#"const abc = (foo = "bar") => {};"#,
r#"const abc = (foo = ("bar")) => {};"#,
r#"const abc = (foo = 123, bar = "foo") => {};"#,
r#"const abc = (foo = {}) => {};"#,
r#"const abc = ({a = true, b = "foo"}) => {};"#,
@ -107,6 +110,7 @@ fn test() {
r#"const {abc = {foo: undefined}} = undefined;"#,
r#"const abc = ([{foo = false, bar = 123}]) => {};"#,
r#"const abc = ({foo = {a: 123}}) => {};"#,
r#"const abc = ({foo = ({a: 123})}) => {};"#,
r#"const abc = ([foo = {a: 123}]) => {};"#,
r#"const abc = ({foo: bar = {a: 123}}) => {};"#,
r#"const abc = () => (foo = {a: 123});"#,
@ -116,6 +120,7 @@ fn test() {
r#"function abc(foo = {a: 123}) {}"#,
r#"async function * abc(foo = {a: 123}) {}"#,
r#"function abc(foo = {a: false}) {}"#,
r#"function abc(foo = ({a: false})) {}"#,
r#"function abc(foo = {a: "bar"}) {}"#,
r#"function abc(foo = {a: "bar", b: {c: true}}) {}"#,
r#"const abc = (foo = {a: false}) => {};"#,

View file

@ -20,6 +20,12 @@ expression: no_object_as_default_parameter
· ──────────
╰────
⚠ eslint-plugin-unicorn(no-object-as-default-parameter): Do not use an object literal as default for parameter `foo`.
╭─[no_object_as_default_parameter.tsx:1:1]
1 │ function abc(foo = ({a: false})) {}
· ──────────
╰────
⚠ eslint-plugin-unicorn(no-object-as-default-parameter): Do not use an object literal as default for parameter `foo`.
╭─[no_object_as_default_parameter.tsx:1:1]
1 │ function abc(foo = {a: "bar"}) {}