Skip to content

Commit

Permalink
Fix unary operator after keyword (#148)
Browse files Browse the repository at this point in the history
* Fix unary operator after keyword

* 0.10.1

* Bump mo-fmt
  • Loading branch information
rvanasa authored Nov 26, 2024
1 parent 06a1c4e commit 0eaff57
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-motoko",
"version": "0.10.0",
"version": "0.10.1",
"description": "A code formatter for the Motoko smart contract language.",
"main": "lib/environments/node.js",
"browser": "lib/environments/web.js",
Expand Down
18 changes: 9 additions & 9 deletions packages/mo-fmt/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/mo-fmt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mo-fmt",
"version": "0.10.0",
"version": "0.10.1",
"description": "An easy-to-use Motoko formatter command.",
"main": "src/cli.js",
"bin": {
Expand All @@ -22,7 +22,7 @@
"commander": "^9.4.0",
"fast-glob": "^3.2.11",
"prettier": "2",
"prettier-plugin-motoko": "^0.10.0"
"prettier-plugin-motoko": "^0.10.1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^28.0.1",
Expand Down
19 changes: 15 additions & 4 deletions src/printers/motoko-tt-ast/spaceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,16 @@ const tokenEndsWith = (end: string) =>
const tokenTypes = (types: Token['token_type'][]) =>
token((token) => types.includes(token.token_type));

const any = (conditions: ((tt: TokenTree) => boolean)[]) => (tt: TokenTree) =>
conditions.some((condition) => condition(tt));
const and =
(...conditions: ((tt: TokenTree) => boolean)[]) =>
(tt: TokenTree) =>
conditions.every((condition) => condition(tt));
const or =
(...conditions: ((tt: TokenTree) => boolean)[]) =>
(tt: TokenTree) =>
conditions.some((condition) => condition(tt));
const not = (condition: (tt: TokenTree) => boolean) => (tt: TokenTree) =>
!condition(tt);

// match both "block comments" and "comment groups" (lexer implementation detail)
const blockComment = (tt: TokenTree) =>
Expand Down Expand Up @@ -136,8 +144,11 @@ const spaceConfig: SpaceConfig = {
// unary operators
[
{
left: tokenTypes(['Close', 'Ident', 'Literal']),
main: any([tokenEquals('+'), tokenEquals('-')]),
left: and(
tokenTypes(['Close', 'Ident', 'Literal']),
not(keyword),
),
main: or(tokenEquals('+'), tokenEquals('-')),
},
'_',
'space',
Expand Down
2 changes: 2 additions & 0 deletions tests/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ public type T = {
await expectFormatted('1 + 1\n');
await expectFormatted('x + 1\n');
await expectFormatted('x - +1\n');
await expectFormatted('return +1\n');
expect(await format('1+1')).toEqual('1 + 1\n');
expect(await format('1+1.0')).toEqual('1 + 1.0\n');
expect(await format('x+1')).toEqual('x + 1\n');
Expand All @@ -585,6 +586,7 @@ public type T = {
await expectFormatted('x + -1\n');
await expectFormatted('x; -1\n');
await expectFormatted('x(-1)\n');
await expectFormatted('return -1\n');
expect(await format('1-1')).toEqual('1 - 1\n');
expect(await format('1.0-1.0')).toEqual('1.0 - 1.0\n');
expect(await format('x-1')).toEqual('x - 1\n');
Expand Down

0 comments on commit 0eaff57

Please sign in to comment.