Skip to content

Commit

Permalink
Fix trailing comma in multi-line array type (#88)
Browse files Browse the repository at this point in the history
* Fix trailing comma in multi-line array type

* 0.3.5

* Comment out diff test

* Update mo-fmt
  • Loading branch information
rvanasa committed Feb 24, 2023
1 parent b181955 commit f71f53c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 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.3.4",
"version": "0.3.5",
"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.3.4",
"version": "0.3.5",
"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.3.4"
"prettier-plugin-motoko": "^0.3.5"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
Expand Down
9 changes: 7 additions & 2 deletions src/printers/motoko-tt-ast/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function printTokenTree(
// add everything except trailing delimiter
if (!isDelim || i !== trees.length - 1) {
resultArray.push(
isDelim
isDelim
? printToken(getGroupDelimToken(groupType))
: printTokenTree(a, path, options, print, args),
);
Expand All @@ -342,7 +342,12 @@ function printTokenTree(
(groupType === 'Unenclosed' || groupType === 'Curly'
? options.semi
: options.trailingComma !== 'none') &&
!isPossiblyRecordExtension()
!isPossiblyRecordExtension() &&
!(
groupType === 'Square' &&
results.length === 1 &&
!isSeparator
) // possibly array type
) {
results.push(
ifBreak(printToken(getGroupDelimToken(groupType))),
Expand Down
8 changes: 7 additions & 1 deletion tests/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('Motoko formatter', () => {
`${'x'.repeat(80)}[0];\n`,
);
expect(format(`${'x'.repeat(80)}[\n0]`)).toStrictEqual(
`${'x'.repeat(80)}[\n 0,\n];\n`,
`${'x'.repeat(80)}[\n 0\n];\n`,
);
expect(format(`${'x'.repeat(80)}[0,]`)).toStrictEqual(
`${'x'.repeat(80)}[0];\n`,
Expand Down Expand Up @@ -342,6 +342,12 @@ describe('Motoko formatter', () => {
expect(format("'\\\"'; //abc")).toStrictEqual("'\\\"'; //abc\n");
});

test('trailing comma in square brackets', () => {
expect(format("[\na,b]")).toStrictEqual("[\n a,\n b,\n];\n");
expect(format("[\na,]")).toStrictEqual("[\n a,\n];\n");
expect(format("x : [\nT\n]")).toStrictEqual("x : [\n T\n];\n");
});

// test('generate diff files from compiler tests', () => {
// for (const extension of ['mo', 'did']) {
// let preOutput = '';
Expand Down

0 comments on commit f71f53c

Please sign in to comment.