Skip to content

Commit

Permalink
Fix corner case with automatic semicolon logic (#128)
Browse files Browse the repository at this point in the history
* Add test cases

* Fix corner case with automatic semicolon logic

* 0.8.4

* Misc

* Account for empty lines

* Improve solution

* Add pipe operator to prefixes

* Bump mo-fmt
  • Loading branch information
rvanasa authored Feb 14, 2024
1 parent ad91290 commit 76f471c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 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.8.3",
"version": "0.8.4",
"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.8.3",
"version": "0.8.4",
"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": "^3.0",
"prettier-plugin-motoko": "^0.8.3"
"prettier-plugin-motoko": "^0.8.4"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
Expand Down
13 changes: 11 additions & 2 deletions src/parsers/motoko-tt-parse/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ParserOptions } from 'prettier';
import outOfCharacter from 'out-of-character';
import wasm from '../../wasm';

const skipAutomaticSemiForNextLinePrefixes = ['.', '|>', ')', '}', ']'];

function getLineIndices(code: string): number[] {
const indices = [];
for (let i = 0; i < code.length; i++) {
Expand Down Expand Up @@ -67,6 +69,7 @@ export default function preprocess(
.map((line, i) => {
const trimmedLine = line.trim();
if (!trimmedLine) {
nextIndent = -1;
return line;
}

Expand All @@ -83,8 +86,14 @@ export default function preprocess(

if (
trimmedLine === '}' &&
// Skip when part of a path expression
!nextLineMaskedCommentsTrimmed.startsWith('.') &&
// Skip if prefix found on next line
(!nextLineMaskedCommentsTrimmed ||
skipAutomaticSemiForNextLinePrefixes.every(
(prefix) =>
!nextLineMaskedCommentsTrimmed.startsWith(
prefix,
),
)) &&
// Skip first block for if/else, try/catch
!/^(else|catch)([^a-zA-Z0-9_]|$)/.test(
nextLineMaskedCommentsTrimmed,
Expand Down
33 changes: 33 additions & 0 deletions tests/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ describe('Motoko formatter', () => {
'if () {} else {};\n',
);
expect(await format('{\n}\n.A')).toStrictEqual('{}.A;\n');
expect(
await format('[\n {\n abc;\n }\n { 123 }\n];\n', {
trailingComma: 'none',
}),
).toStrictEqual('[\n {\n abc;\n },\n { 123 }\n];\n');
});

test('automatic semicolons with line comment', async () => {
Expand Down Expand Up @@ -424,6 +429,34 @@ describe('Motoko formatter', () => {
expect(await format('[\na,b]')).toStrictEqual('[\n a,\n b,\n];\n');
expect(await format('[\na,]')).toStrictEqual('[\n a,\n];\n');
expect(await format('x : [\nT\n]')).toStrictEqual('x : [\n T\n];\n');
expect(await format('x : [\n { abc; }\n];\n')).toEqual(
'x : [{ abc }];\n',
);
await expectFormatted('x : [{\n abc;\n}] = 1;\n');
await expectFormatted('x : [{\n abc;\n}] = 1;\n');
await expectFormatted('type T = [{\n abc;\n}];\n');
await expectFormatted('let x = [\n {\n abc;\n },\n];\n');
expect(
await format(
`
public type T = {
x : [
{
a : A;
fn : shared query A -> async T;
}
];
}`,
),
).toEqual(
`
public type T = {
x : [{
a : A;
fn : shared query A -> async T;
}];
};`.trim() + '\n',
);
});

test('comma-parentheses', async () => {
Expand Down

0 comments on commit 76f471c

Please sign in to comment.