Skip to content

Commit

Permalink
Fix corner case with 'prettier-ignore' comment whitespace (#98)
Browse files Browse the repository at this point in the history
* Fix corner case with 'prettier-ignore' comment whitespace

* 0.5.2

* Bump mo-fmt
  • Loading branch information
rvanasa authored Apr 25, 2023
1 parent a68a9ed commit 98c4c2f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 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.5.1",
"version": "0.5.2",
"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.5.1",
"version": "0.5.2",
"description": "An easy-to-use Motoko formatter command.",
"main": "src/cli.js",
"bin": {
Expand All @@ -21,7 +21,7 @@
"commander": "^9.4.0",
"fast-glob": "^3.2.11",
"prettier": "^2.7",
"prettier-plugin-motoko": "^0.5.1"
"prettier-plugin-motoko": "^0.5.2"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
Expand Down
12 changes: 7 additions & 5 deletions src/printers/motoko-tt-ast/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,8 @@ function printTokenTree(
// allow trailing comma/semicolon if something exists in the group other than a comment
allowTrailingSeparator = true;
}
// check for prettier-ignore comments
else if (comment === 'prettier-ignore') {
ignoringNextStatement = true;
}

const isIgnoreComment = comment === 'prettier-ignore';

if (isSeparator) {
endGroup();
Expand Down Expand Up @@ -333,7 +331,7 @@ function printTokenTree(
: printTokenTree(a, path, options, print, args),
);
}
if (i < trees.length - 1) {
if (i < trees.length - 1 && !isIgnoreComment) {
resultArray.push(
printBetween(trees, i, i + 1, leftMap, rightMap),
);
Expand Down Expand Up @@ -371,6 +369,10 @@ function printTokenTree(
);
}
}

if (isIgnoreComment) {
ignoringNextStatement = true;
}
}
}
endGroup();
Expand Down
4 changes: 4 additions & 0 deletions tests/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ describe('Motoko formatter', () => {
expect(format('a<(b,\n//c\n)>()')).toStrictEqual('a<(b, /* c */)>()\n');
});

test('prettier-ignore line comment as first line in block', () => {
expect(format('{\n// prettier-ignore\n 123}')).toStrictEqual('{\n // prettier-ignore\n 123\n};\n');
});

test('unclosed quotes in comments', () => {
expect(format("// a'b\n '")).toStrictEqual("// a'b\n';\n");
expect(format('// a"b\n "')).toStrictEqual('// a"b\n";\n');
Expand Down

0 comments on commit 98c4c2f

Please sign in to comment.