From f1ff74b5ca30b74064a02dd2669400896a301bb0 Mon Sep 17 00:00:00 2001 From: Dang Mai Date: Sun, 23 Apr 2023 10:30:31 -0700 Subject: [PATCH] Fix #738 - Add support for User Mode in Database Operations --- CHANGELOG.md | 4 ++++ src/printer.ts | 2 ++ tests/dml/DmlClass.cls | 12 +++++++++++ tests/dml/__snapshots__/jsfmt.spec.ts.snap | 24 ++++++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f38bc9..45a8aeb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Unreleased +## Formatting Changes + +- Add support for User Mode in Database Operations ([issue](https://github.com/dangmai/prettier-plugin-apex/issues/738)). + ## Internal Changes - `start-apex-server` takes optional `-c` flag, which will be passed on to `apex-ast-serializer` as a comma-delimited list of allowed origins that will be added to the CORS headers returned by the parsing server. diff --git a/src/printer.ts b/src/printer.ts index fc1a67ad..f13f8352 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -922,6 +922,7 @@ function handleStatement( const parts: Doc[] = []; parts.push(doc); parts.push(" "); + pushIfExist(parts, path.call(print, "runAsMode", "value"), [" "], ["as "]); parts.push(path.call(print, "expr")); // upsert statement has an extra param that can be tacked on at the end if (node.id) { @@ -935,6 +936,7 @@ function handleDmlMergeStatement(path: AstPath, print: printFn): Doc { const parts: Doc[] = []; parts.push("merge"); parts.push(" "); + pushIfExist(parts, path.call(print, "runAsMode", "value"), [" "], ["as "]); parts.push(path.call(print, "expr1")); parts.push(line); parts.push(path.call(print, "expr2")); diff --git a/tests/dml/DmlClass.cls b/tests/dml/DmlClass.cls index b0bdb76d..3d85fee3 100644 --- a/tests/dml/DmlClass.cls +++ b/tests/dml/DmlClass.cls @@ -12,6 +12,8 @@ class DMLClass { new Account(Name = 'Inner Acme Three'), new Account(Name = 'Inner Acme Four') }; + insert as user account; + insert as system account; } void updateTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -27,6 +29,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + update as user account; + update as system account; } void upsertWithoutOptFieldTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -42,6 +46,8 @@ class DMLClass { new Account(Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + upsert as user account; + upsert as system account; } void upsertWithOptFieldTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -72,6 +78,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + delete as user account; + delete as system account; } void undeleteTest() { Id accountId = [SELECT Id FROM Account LIMIT 1 ALL ROWS].Id; @@ -90,6 +98,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + undelete as user account; + undelete as system account; } void mergeTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -104,6 +114,8 @@ class DMLClass { merge account accounts; merge account accountId; merge account accountIds; + merge as user account accountId; + merge as system account accountIds; merge new Account(Id = accountId, Name = 'Acme One') new Account(Id = accountId, Name = 'Acme Two'); merge new Account(Id = accountId, Name = 'Acme One') new Account[]{new Account(Id = accountId, Name = 'Acme Two')}; diff --git a/tests/dml/__snapshots__/jsfmt.spec.ts.snap b/tests/dml/__snapshots__/jsfmt.spec.ts.snap index 1c48a663..bffc9389 100644 --- a/tests/dml/__snapshots__/jsfmt.spec.ts.snap +++ b/tests/dml/__snapshots__/jsfmt.spec.ts.snap @@ -15,6 +15,8 @@ class DMLClass { new Account(Name = 'Inner Acme Three'), new Account(Name = 'Inner Acme Four') }; + insert as user account; + insert as system account; } void updateTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -30,6 +32,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + update as user account; + update as system account; } void upsertWithoutOptFieldTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -45,6 +49,8 @@ class DMLClass { new Account(Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + upsert as user account; + upsert as system account; } void upsertWithOptFieldTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -75,6 +81,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + delete as user account; + delete as system account; } void undeleteTest() { Id accountId = [SELECT Id FROM Account LIMIT 1 ALL ROWS].Id; @@ -93,6 +101,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + undelete as user account; + undelete as system account; } void mergeTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -107,6 +117,8 @@ class DMLClass { merge account accounts; merge account accountId; merge account accountIds; + merge as user account accountId; + merge as system account accountIds; merge new Account(Id = accountId, Name = 'Acme One') new Account(Id = accountId, Name = 'Acme Two'); merge new Account(Id = accountId, Name = 'Acme One') new Account[]{new Account(Id = accountId, Name = 'Acme Two')}; @@ -129,6 +141,8 @@ class DMLClass { new Account(Name = 'Inner Acme Three'), new Account(Name = 'Inner Acme Four') }; + insert as user account; + insert as system account; } void updateTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -144,6 +158,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + update as user account; + update as system account; } void upsertWithoutOptFieldTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -159,6 +175,8 @@ class DMLClass { new Account(Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + upsert as user account; + upsert as system account; } void upsertWithOptFieldTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -195,6 +213,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + delete as user account; + delete as system account; } void undeleteTest() { Id accountId = [SELECT Id FROM Account LIMIT 1 ALL ROWS].Id; @@ -213,6 +233,8 @@ class DMLClass { new Account(Id = accountId, Name = 'Inner Acme Three'), new Account(Id = accountId, Name = 'Inner Acme Four') }; + undelete as user account; + undelete as system account; } void mergeTest() { Id accountId = [SELECT Id FROM Account LIMIT 1].Id; @@ -227,6 +249,8 @@ class DMLClass { merge account accounts; merge account accountId; merge account accountIds; + merge as user account accountId; + merge as system account accountIds; merge new Account(Id = accountId, Name = 'Acme One') new Account(Id = accountId, Name = 'Acme Two');