Repository: source-academy/js-slang
Pull request #425 (2020-03-03 13:08:13)

Week70
Fine-tuning property names

coveralls: ## Pull Request Test Coverage Report for [Build 2008](https://coveralls.io/builds/29098131)

* **3** of **3**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.06%**) to **88.43%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/29098131/badge)](https://coveralls.io/builds/29098131) |
| :-- | --: |
| Change from base [Build 1992](https://coveralls.io/builds/29096330): |  -0.06% |
| Covered Lines: | 2172 |
| Relevant Lines: | 2383 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/README.rst b/README.rst
index 243ea28e..03aaa70b 100644
--- a/README.rst
+++ b/README.rst
@@ -77,7 +77,10 @@ js-slang comes with an extensive test suite. To run the tests after you made you
 ``yarn test``. Regression tests are run automatically when you want to push changes to this repository. The regression tests are generated using `jest` and stored as snapshots in ``src/__tests__``.
 
 After modifying js-slang, carefully inspect any failing regression tests reported in red in the command line. If you are convinced that the regression tests and not your changes are at fault, you can update the regression tests as follows:
-``yarn test -- --coverage --updateSnapshot``
+
+.. code-block::
+
+  $ yarn test -- --updateSnapshot
 
 Error messages
 --------------
diff --git a/package.json b/package.json
index d31b222b..8f187e50 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "js-slang",
-  "version": "0.4.3",
+  "version": "0.4.4",
   "description": "Javascript-based interpreter for slang, written in Typescript",
   "author": {
     "name": "Source Academy",
diff --git a/src/stdlib/parser.ts b/src/stdlib/parser.ts
index 582e842f..c171236c 100644
--- a/src/stdlib/parser.ts
+++ b/src/stdlib/parser.ts
@@ -47,24 +47,21 @@ transformers = new Map([
 
   [
     'BlockStatement',
-    (node: es.Node) => {
-      node = node as es.BlockStatement
+    (node: es.BlockStatement) => {
       return vector_to_list(['block', makeSequenceIfNeeded(node.body)])
     }
   ],
 
   [
     'ExpressionStatement',
-    (node: es.Node) => {
-      node = node as es.ExpressionStatement
+    (node: es.ExpressionStatement) => {
       return transform(node.expression)
     }
   ],
 
   [
     'IfStatement',
-    (node: es.Node) => {
-      node = node as es.IfStatement
+    (node: es.IfStatement) => {
       return vector_to_list([
         'conditional_statement',
         transform(node.test),
@@ -76,8 +73,7 @@ transformers = new Map([
 
   [
     'FunctionDeclaration',
-    (node: es.Node) => {
-      node = node as es.FunctionDeclaration
+    (node: es.FunctionDeclaration) => {
       return vector_to_list([
         'constant_declaration',
         transform(node.id as es.Identifier),
@@ -95,8 +91,7 @@ transformers = new Map([
 
   [
     'VariableDeclaration',
-    (node: es.Node) => {
-      node = node as es.VariableDeclaration
+    (node: es.VariableDeclaration) => {
       if (node.kind === 'let') {
         return vector_to_list([
           'variable_declaration',
@@ -118,16 +113,14 @@ transformers = new Map([
 
   [
     'ReturnStatement',
-    (node: es.Node) => {
-      node = node as es.ReturnStatement
+    (node: es.ReturnStatement) => {
       return vector_to_list(['return_statement', transform(node.argument as es.Expression)])
     }
   ],
 
   [
     'CallExpression',
-    (node: es.Node) => {
-      node = node as es.CallExpression
+    (node: es.CallExpression) => {
       return vector_to_list([
         'application',
         transform(node.callee),
@@ -138,8 +131,7 @@ transformers = new Map([
 
   [
     'UnaryExpression',
-    (node: es.Node) => {
-      node = node as es.UnaryExpression
+    (node: es.UnaryExpression) => {
       return vector_to_list([
         'application',
         vector_to_list(['name', node.operator]),
@@ -150,8 +142,7 @@ transformers = new Map([
 
   [
     'BinaryExpression',
-    (node: es.Node) => {
-      node = node as es.BinaryExpression
+    (node: es.BinaryExpression) => {
       return vector_to_list([
         'application',
         vector_to_list(['name', node.operator]),
@@ -162,8 +153,7 @@ transformers = new Map([
 
   [
     'LogicalExpression',
-    (node: es.Node) => {
-      node = node as es.LogicalExpression
+    (node: es.LogicalExpression) => {
       return vector_to_list([
         'boolean_operation',
         vector_to_list(['name', node.operator]),
@@ -174,8 +164,7 @@ transformers = new Map([
 
   [
     'ConditionalExpression',
-    (node: es.Node) => {
-      node = node as es.ConditionalExpression
+    (node: es.ConditionalExpression) => {
       return vector_to_list([
         'conditional_expression',
         transform(node.test),
@@ -203,32 +192,28 @@ transformers = new Map([
 
   [
     'Identifier',
-    (node: es.Node) => {
-      node = node as es.Identifier
+    (node: es.Identifier) => {
       return vector_to_list(['name', node.name])
     }
   ],
 
   [
     'Literal',
-    (node: es.Node) => {
-      node = node as es.Literal
+    (node: es.Literal) => {
       return node.value
     }
   ],
 
   [
     'ArrayExpression',
-    (node: es.Node) => {
-      node = node as es.ArrayExpression
+    (node: es.ArrayExpression) => {
       return vector_to_list(['array_expression', vector_to_list(node.elements.map(transform))])
     }
   ],
 
   [
     'AssignmentExpression',
-    (node: es.Node) => {
-      node = node as es.AssignmentExpression
+    (node: es.AssignmentExpression) => {
       if (node.left.type === 'Identifier') {
         return vector_to_list([
           'assignment',
@@ -250,8 +235,7 @@ transformers = new Map([
 
   [
     'ForStatement',
-    (node: es.Node) => {
-      node = node as es.ForStatement
+    (node: es.ForStatement) => {
       return vector_to_list([
         'for_loop',
         transform(node.init as es.VariableDeclaration | es.Expression),
@@ -264,41 +248,40 @@ transformers = new Map([
 
   [
     'WhileStatement',
-    (node: es.Node) => {
-      node = node as es.WhileStatement
+    (node: es.WhileStatement) => {
       return vector_to_list(['while_loop', transform(node.test), transform(node.body)])
     }
   ],
 
   [
     'BreakStatement',
-    (node: es.Node) => {
-      node = node as es.BreakStatement
+    (node: es.BreakStatement) => {
       return vector_to_list(['break_statement'])
     }
   ],
 
   [
     'ContinueStatement',
-    (node: es.Node) => {
-      node = node as es.ContinueStatement
+    (node: es.ContinueStatement) => {
       return vector_to_list(['continue_statement'])
     }
   ],
 
   [
     'ObjectExpression',
-    (node: es.Node) => {
-      node = node as es.ObjectExpression
+    (node: es.ObjectExpression) => {
       return vector_to_list(['object_expression', vector_to_list(node.properties.map(transform))])
     }
   ],
 
   [
     'MemberExpression',
-    (node: es.Node) => {
-      node = node as es.MemberExpression
-      return vector_to_list(['array_access', transform(node.object), transform(node.property)])
+    (node: es.MemberExpression) => {
+      const key =
+        node.property.type === 'Identifier'
+          ? vector_to_list(['property', node.property.name])
+          : transform(node.property)
+      return vector_to_list(['array_access', transform(node.object), key])
     }
   ],
 
@@ -308,7 +291,7 @@ transformers = new Map([
       if (node.key.type === 'Literal') {
         return [node.key.value, transform(node.value)]
       } else if (node.key.type === 'Identifier') {
-        return [node.key.name, transform(node.value)]
+        return [vector_to_list(['property', node.key.name]), transform(node.value)]
       } else {
         unreachable()
         throw new ParseError('Invalid property key type')


Test patch:
diff --git a/src/__tests__/__snapshots__/allowed-syntax.ts.snap b/src/__tests__/__snapshots__/allowed-syntax.ts.snap
index 26c014f3..0b5f7248 100644
--- a/src/__tests__/__snapshots__/allowed-syntax.ts.snap
+++ b/src/__tests__/__snapshots__/allowed-syntax.ts.snap
@@ -6347,12 +6347,24 @@ Object {
     Array [
       Array [
         Array [
-          "a",
+          Array [
+            "property",
+            Array [
+              "a",
+              null,
+            ],
+          ],
           1,
         ],
         Array [
           Array [
-            "b",
+            Array [
+              "property",
+              Array [
+                "b",
+                null,
+              ],
+            ],
             2,
           ],
           null,
@@ -6559,12 +6571,24 @@ Object {
         Array [
           Array [
             Array [
-              "a",
+              Array [
+                "property",
+                Array [
+                  "a",
+                  null,
+                ],
+              ],
               1,
             ],
             Array [
               Array [
-                "b",
+                Array [
+                  "property",
+                  Array [
+                    "b",
+                    null,
+                  ],
+                ],
                 2,
               ],
               null,
@@ -6932,12 +6956,24 @@ Object {
         Array [
           Array [
             Array [
-              "a",
+              Array [
+                "property",
+                Array [
+                  "a",
+                  null,
+                ],
+              ],
               1,
             ],
             Array [
               Array [
-                "b",
+                Array [
+                  "property",
+                  Array [
+                    "b",
+                    null,
+                  ],
+                ],
                 2,
               ],
               null,
@@ -6948,7 +6984,7 @@ Object {
       ],
       Array [
         Array [
-          "name",
+          "property",
           Array [
             "a",
             null,
@@ -7331,7 +7367,7 @@ Object {
       ],
       Array [
         Array [
-          "name",
+          "property",
           Array [
             "a",
             null,
@@ -7775,12 +7811,24 @@ Object {
                 Array [
                   Array [
                     Array [
-                      "a",
+                      Array [
+                        "property",
+                        Array [
+                          "a",
+                          null,
+                        ],
+                      ],
                       1,
                     ],
                     Array [
                       Array [
-                        "b",
+                        Array [
+                          "property",
+                          Array [
+                            "b",
+                            null,
+                          ],
+                        ],
                         2,
                       ],
                       null,
@@ -7791,7 +7839,7 @@ Object {
               ],
               Array [
                 Array [
-                  "name",
+                  "property",
                   Array [
                     "key",
                     null,
@@ -8069,12 +8117,24 @@ Object {
                 Array [
                   Array [
                     Array [
-                      "a",
+                      Array [
+                        "property",
+                        Array [
+                          "a",
+                          null,
+                        ],
+                      ],
                       1,
                     ],
                     Array [
                       Array [
-                        "b",
+                        Array [
+                          "property",
+                          Array [
+                            "b",
+                            null,
+                          ],
+                        ],
                         2,
                       ],
                       null,
@@ -8103,7 +8163,7 @@ Object {
                   ],
                   Array [
                     Array [
-                      "name",
+                      "property",
                       Array [
                         "a",
                         null,
@@ -8334,12 +8394,24 @@ Object {
                 Array [
                   Array [
                     Array [
-                      "a",
+                      Array [
+                        "property",
+                        Array [
+                          "a",
+                          null,
+                        ],
+                      ],
                       1,
                     ],
                     Array [
                       Array [
-                        "b",
+                        Array [
+                          "property",
+                          Array [
+                            "b",
+                            null,
+                          ],
+                        ],
                         2,
                       ],
                       null,
@@ -8594,12 +8666,24 @@ Object {
                 Array [
                   Array [
                     Array [
-                      "a",
+                      Array [
+                        "property",
+                        Array [
+                          "a",
+                          null,
+                        ],
+                      ],
                       1,
                     ],
                     Array [
                       Array [
-                        "b",
+                        Array [
+                          "property",
+                          Array [
+                            "b",
+                            null,
+                          ],
+                        ],
                         2,
                       ],
                       null,
@@ -8645,7 +8729,7 @@ Object {
                     ],
                     Array [
                       Array [
-                        "name",
+                        "property",
                         Array [
                           "key",
                           null,
diff --git a/src/stdlib/__tests__/__snapshots__/parser.ts.snap b/src/stdlib/__tests__/__snapshots__/parser.ts.snap
index a76d364c..0794f29b 100644
--- a/src/stdlib/__tests__/__snapshots__/parser.ts.snap
+++ b/src/stdlib/__tests__/__snapshots__/parser.ts.snap
@@ -1014,9 +1014,13 @@ Object {
   [ \\"x\\",
   null ] ],
 [ [ \\"object_expression\\",
-  [ [ [ \\"a\\",
+  [ [ [ [ \\"property\\",
+        [ \\"a\\",
+        null ] ],
       5 ],
-    [ [ \\"b\\",
+    [ [ [ \\"property\\",
+        [ \\"b\\",
+        null ] ],
       10 ],
     [ [ \\"key\\",
       [ \\"name\\",
@@ -1042,7 +1046,7 @@ Object {
     [ [ \\"name\\",
       [ \\"a\\",
       null ] ],
-    [ [ \\"name\\",
+    [ [ \\"property\\",
       [ \\"b\\",
       null ] ],
     null ] ] ],
@@ -1050,7 +1054,7 @@ Object {
     [ [ \\"name\\",
       [ \\"a\\",
       null ] ],
-    [ [ \\"name\\",
+    [ [ \\"property\\",
       [ \\"b\\",
       null ] ],
     null ] ] ],
@@ -1086,7 +1090,7 @@ Object {
       [ [ \\"name\\",
         [ \\"a\\",
         null ] ],
-      [ [ \\"name\\",
+      [ [ \\"property\\",
         [ \\"b\\",
         null ] ],
       null ] ] ],
@@ -1097,7 +1101,7 @@ Object {
       [ [ \\"name\\",
         [ \\"a\\",
         null ] ],
-      [ [ \\"name\\",
+      [ [ \\"property\\",
         [ \\"b\\",
         null ] ],
       null ] ] ],