Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 21, 2023
1 parent 2a5b391 commit 475621c
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/language-js/needs-parens.js
Expand Up @@ -783,6 +783,15 @@ function needsParens(path, options) {
case "MemberExpression":
case "TaggedTemplateExpression":
case "TSNonNullExpression":
if (
name === "expression" &&
parent.type === "Decorator" &&
isMemberExpression(node) &&
node.computed
) {
return true;
}

if (
name === "callee" &&
(parent.type === "BindExpression" || parent.type === "NewExpression")
Expand Down
140 changes: 140 additions & 0 deletions tests/format/js/decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -162,6 +162,146 @@ export class Bar {}
================================================================================
`;
exports[`member-expression.js [acorn] format 1`] = `
"Unexpected character '@' (3:5)
1 | [
2 | class {
> 3 | @(decorators[0])
| ^
4 | method() {}
5 | },
6 |"
`;
exports[`member-expression.js [espree] format 1`] = `
"Unexpected character '@' (3:5)
1 | [
2 | class {
> 3 | @(decorators[0])
| ^
4 | method() {}
5 | },
6 |"
`;
exports[`member-expression.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
[
class {
@(decorators[0])
method() {}
},
class {
@(decorators?.[0])
method() {}
},
class {
@(decorators.at(0))
method() {}
},
class {
@(decorators?.at(0))
method() {}
},
class {
@(decorators.first)
method() {}
},
class {
@(decorators?.first)
method() {}
},
class {
@(decorators[first])
method() {}
},
class {
@(decorators["first"])
method() {}
},
@(decorators[first])
class {
method() {}
},
@(decorators[0])
class {
method() {}
},
]
=====================================output=====================================
[
class {
@decorators[0]
method() {}
},
class {
@(decorators?.[0])
method() {}
},
class {
@decorators.at(0)
method() {}
},
class {
@(decorators?.at(0))
method() {}
},
class {
@decorators.first
method() {}
},
class {
@(decorators?.first)
method() {}
},
class {
@decorators[first]
method() {}
},
class {
@decorators["first"]
method() {}
},
(
@decorators[first]
class {
method() {}
}
),
(
@decorators[0]
class {
method() {}
}
),
];
================================================================================
`;
exports[`methods.js [acorn] format 1`] = `
"Unexpected character '@' (3:3)
1 |
Expand Down
51 changes: 51 additions & 0 deletions tests/format/js/decorators/member-expression.js
@@ -0,0 +1,51 @@
[
class {
@(decorators[0])
method() {}
},

class {
@(decorators?.[0])
method() {}
},

class {
@(decorators.at(0))
method() {}
},

class {
@(decorators?.at(0))
method() {}
},

class {
@(decorators.first)
method() {}
},

class {
@(decorators?.first)
method() {}
},

class {
@(decorators[first])
method() {}
},

class {
@(decorators["first"])
method() {}
},

@(decorators[first])
class {
method() {}
},

@(decorators[0])
class {
method() {}
},
]

0 comments on commit 475621c

Please sign in to comment.