diff --git a/package-lock.json b/package-lock.json index 73fed86..20dd26e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prettier-plugin-motoko", - "version": "0.5.1", + "version": "0.5.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prettier-plugin-motoko", - "version": "0.5.1", + "version": "0.5.2", "license": "Apache-2.0", "dependencies": { "out-of-character": "^1.2.1" diff --git a/package.json b/package.json index 7ecf579..6e7c347 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/mo-fmt/package-lock.json b/packages/mo-fmt/package-lock.json index 77731df..a5a0bdb 100644 --- a/packages/mo-fmt/package-lock.json +++ b/packages/mo-fmt/package-lock.json @@ -1,18 +1,18 @@ { "name": "mo-fmt", - "version": "0.5.1", + "version": "0.5.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mo-fmt", - "version": "0.5.1", + "version": "0.5.2", "license": "Apache-2.0", "dependencies": { "commander": "^9.4.0", "fast-glob": "^3.2.11", "prettier": "^2.7", - "prettier-plugin-motoko": "^0.5.1" + "prettier-plugin-motoko": "^0.5.2" }, "bin": { "mo-fmt": "bin/mo-fmt.js" @@ -4684,9 +4684,9 @@ } }, "node_modules/prettier-plugin-motoko": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/prettier-plugin-motoko/-/prettier-plugin-motoko-0.5.1.tgz", - "integrity": "sha512-/McB+8sVCpfBV78tK3LmZ8HyH5S5E4MACoGkG6HEIAymlJZRZ9aguGV2GDuN7c+yUlCeeXnoKzxn0mtC0siSvg==", + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/prettier-plugin-motoko/-/prettier-plugin-motoko-0.5.2.tgz", + "integrity": "sha512-0KjHdFqeJDFcf1Z4dB7dbAWjfCZIo6J/X/ONGMyOq85mDh0eg6exB3nRVXvCz7wIOY49ZlQEZZIAnmJqEW3+dA==", "dependencies": { "out-of-character": "^1.2.1" }, @@ -9365,9 +9365,9 @@ "integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==" }, "prettier-plugin-motoko": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/prettier-plugin-motoko/-/prettier-plugin-motoko-0.5.1.tgz", - "integrity": "sha512-/McB+8sVCpfBV78tK3LmZ8HyH5S5E4MACoGkG6HEIAymlJZRZ9aguGV2GDuN7c+yUlCeeXnoKzxn0mtC0siSvg==", + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/prettier-plugin-motoko/-/prettier-plugin-motoko-0.5.2.tgz", + "integrity": "sha512-0KjHdFqeJDFcf1Z4dB7dbAWjfCZIo6J/X/ONGMyOq85mDh0eg6exB3nRVXvCz7wIOY49ZlQEZZIAnmJqEW3+dA==", "requires": { "out-of-character": "^1.2.1" } diff --git a/packages/mo-fmt/package.json b/packages/mo-fmt/package.json index 5e7a998..84b7bbf 100644 --- a/packages/mo-fmt/package.json +++ b/packages/mo-fmt/package.json @@ -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": { @@ -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", diff --git a/src/printers/motoko-tt-ast/print.ts b/src/printers/motoko-tt-ast/print.ts index 1ee73ef..771fbcd 100644 --- a/src/printers/motoko-tt-ast/print.ts +++ b/src/printers/motoko-tt-ast/print.ts @@ -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(); @@ -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), ); @@ -371,6 +369,10 @@ function printTokenTree( ); } } + + if (isIgnoreComment) { + ignoringNextStatement = true; + } } } endGroup(); diff --git a/tests/formatter.test.ts b/tests/formatter.test.ts index 32aa882..2ddb497 100644 --- a/tests/formatter.test.ts +++ b/tests/formatter.test.ts @@ -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');