OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1504 | 1504 |
1505 enum ParserFlag { | 1505 enum ParserFlag { |
1506 kAllowLazy, | 1506 kAllowLazy, |
1507 kAllowNatives, | 1507 kAllowNatives, |
1508 kAllowHarmonyFunctionSent, | 1508 kAllowHarmonyFunctionSent, |
1509 kAllowHarmonyRestrictiveDeclarations, | 1509 kAllowHarmonyRestrictiveDeclarations, |
1510 kAllowHarmonyExponentiationOperator, | 1510 kAllowHarmonyExponentiationOperator, |
1511 kAllowHarmonyForIn, | 1511 kAllowHarmonyForIn, |
1512 kAllowHarmonyAsyncAwait, | 1512 kAllowHarmonyAsyncAwait, |
1513 kAllowHarmonyRestrictiveGenerators, | 1513 kAllowHarmonyRestrictiveGenerators, |
| 1514 kAllowHarmonyTrailingCommas, |
1514 }; | 1515 }; |
1515 | 1516 |
1516 enum ParserSyncTestResult { | 1517 enum ParserSyncTestResult { |
1517 kSuccessOrError, | 1518 kSuccessOrError, |
1518 kSuccess, | 1519 kSuccess, |
1519 kError | 1520 kError |
1520 }; | 1521 }; |
1521 | 1522 |
1522 template <typename Traits> | 1523 template <typename Traits> |
1523 void SetParserFlags(i::ParserBase<Traits>* parser, | 1524 void SetParserFlags(i::ParserBase<Traits>* parser, |
1524 i::EnumSet<ParserFlag> flags) { | 1525 i::EnumSet<ParserFlag> flags) { |
1525 parser->set_allow_lazy(flags.Contains(kAllowLazy)); | 1526 parser->set_allow_lazy(flags.Contains(kAllowLazy)); |
1526 parser->set_allow_natives(flags.Contains(kAllowNatives)); | 1527 parser->set_allow_natives(flags.Contains(kAllowNatives)); |
1527 parser->set_allow_harmony_function_sent( | 1528 parser->set_allow_harmony_function_sent( |
1528 flags.Contains(kAllowHarmonyFunctionSent)); | 1529 flags.Contains(kAllowHarmonyFunctionSent)); |
1529 parser->set_allow_harmony_restrictive_declarations( | 1530 parser->set_allow_harmony_restrictive_declarations( |
1530 flags.Contains(kAllowHarmonyRestrictiveDeclarations)); | 1531 flags.Contains(kAllowHarmonyRestrictiveDeclarations)); |
1531 parser->set_allow_harmony_exponentiation_operator( | 1532 parser->set_allow_harmony_exponentiation_operator( |
1532 flags.Contains(kAllowHarmonyExponentiationOperator)); | 1533 flags.Contains(kAllowHarmonyExponentiationOperator)); |
1533 parser->set_allow_harmony_for_in(flags.Contains(kAllowHarmonyForIn)); | 1534 parser->set_allow_harmony_for_in(flags.Contains(kAllowHarmonyForIn)); |
1534 parser->set_allow_harmony_async_await( | 1535 parser->set_allow_harmony_async_await( |
1535 flags.Contains(kAllowHarmonyAsyncAwait)); | 1536 flags.Contains(kAllowHarmonyAsyncAwait)); |
1536 parser->set_allow_harmony_restrictive_generators( | 1537 parser->set_allow_harmony_restrictive_generators( |
1537 flags.Contains(kAllowHarmonyRestrictiveGenerators)); | 1538 flags.Contains(kAllowHarmonyRestrictiveGenerators)); |
| 1539 parser->set_allow_harmony_trailing_commas( |
| 1540 flags.Contains(kAllowHarmonyTrailingCommas)); |
1538 } | 1541 } |
1539 | 1542 |
1540 | 1543 |
1541 void TestParserSyncWithFlags(i::Handle<i::String> source, | 1544 void TestParserSyncWithFlags(i::Handle<i::String> source, |
1542 i::EnumSet<ParserFlag> flags, | 1545 i::EnumSet<ParserFlag> flags, |
1543 ParserSyncTestResult result, | 1546 ParserSyncTestResult result, |
1544 bool is_module = false, | 1547 bool is_module = false, |
1545 bool test_preparser = true) { | 1548 bool test_preparser = true) { |
1546 i::Isolate* isolate = CcTest::i_isolate(); | 1549 i::Isolate* isolate = CcTest::i_isolate(); |
1547 i::Factory* factory = isolate->factory(); | 1550 i::Factory* factory = isolate->factory(); |
(...skipping 6285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7833 NULL}; | 7836 NULL}; |
7834 static const ParserFlag always_flags[] = {kAllowHarmonyAsyncAwait}; | 7837 static const ParserFlag always_flags[] = {kAllowHarmonyAsyncAwait}; |
7835 // The preparser doesn't enforce the restriction, so turn it off. | 7838 // The preparser doesn't enforce the restriction, so turn it off. |
7836 bool test_preparser = false; | 7839 bool test_preparser = false; |
7837 RunParserSyncTest(block_context_data, error_data, kError, NULL, 0, | 7840 RunParserSyncTest(block_context_data, error_data, kError, NULL, 0, |
7838 always_flags, arraysize(always_flags), NULL, 0, false, | 7841 always_flags, arraysize(always_flags), NULL, 0, false, |
7839 test_preparser); | 7842 test_preparser); |
7840 RunParserSyncTest(top_level_context_data, error_data, kSuccess, NULL, 0, | 7843 RunParserSyncTest(top_level_context_data, error_data, kSuccess, NULL, 0, |
7841 always_flags, arraysize(always_flags)); | 7844 always_flags, arraysize(always_flags)); |
7842 } | 7845 } |
| 7846 |
| 7847 TEST(TrailingCommasInParameters) { |
| 7848 // clang-format off |
| 7849 const char* context_data[][2] = { |
| 7850 { "", "" }, |
| 7851 { "'use strict';", "" }, |
| 7852 { "function foo() {", "}" }, |
| 7853 { "function foo() {'use strict';", "}" }, |
| 7854 { NULL, NULL } |
| 7855 }; |
| 7856 |
| 7857 const char* data[] = { |
| 7858 " function a(b,) {}", |
| 7859 " function* a(b,) {}", |
| 7860 "(function a(b,) {});", |
| 7861 "(function* a(b,) {});", |
| 7862 "(function (b,) {});", |
| 7863 "(function* (b,) {});", |
| 7864 " function a(b,c,d,) {}", |
| 7865 " function* a(b,c,d,) {}", |
| 7866 "(function a(b,c,d,) {});", |
| 7867 "(function* a(b,c,d,) {});", |
| 7868 "(function (b,c,d,) {});", |
| 7869 "(function* (b,c,d,) {});", |
| 7870 "(b,) => {};", |
| 7871 "(b,c,d,) => {};", |
| 7872 "a(1,);", |
| 7873 "a(1,2,3,);", |
| 7874 "a(...[],);", |
| 7875 "a(1, 2, ...[],);", |
| 7876 "a(...[], 2, ...[],);", |
| 7877 NULL |
| 7878 }; |
| 7879 // clang-format on |
| 7880 |
| 7881 static const ParserFlag always_flags[] = {kAllowHarmonyTrailingCommas}; |
| 7882 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags, |
| 7883 arraysize(always_flags)); |
| 7884 } |
| 7885 |
| 7886 TEST(TrailingCommasInParametersErrors) { |
| 7887 // clang-format off |
| 7888 const char* context_data[][2] = { |
| 7889 { "", "" }, |
| 7890 { "'use strict';", "" }, |
| 7891 { "function foo() {", "}" }, |
| 7892 { "function foo() {'use strict';", "}" }, |
| 7893 { NULL, NULL } |
| 7894 }; |
| 7895 |
| 7896 const char* data[] = { |
| 7897 // too many trailing commas |
| 7898 " function a(b,,) {}", |
| 7899 " function* a(b,,) {}", |
| 7900 "(function a(b,,) {});", |
| 7901 "(function* a(b,,) {});", |
| 7902 "(function (b,,) {});", |
| 7903 "(function* (b,,) {});", |
| 7904 " function a(b,c,d,,) {}", |
| 7905 " function* a(b,c,d,,) {}", |
| 7906 "(function a(b,c,d,,) {});", |
| 7907 "(function* a(b,c,d,,) {});", |
| 7908 "(function (b,c,d,,) {});", |
| 7909 "(function* (b,c,d,,) {});", |
| 7910 "(b,,) => {};", |
| 7911 "(b,c,d,,) => {};", |
| 7912 "a(1,,);", |
| 7913 "a(1,2,3,,);", |
| 7914 // only a trailing comma and no parameters |
| 7915 " function a1(,) {}", |
| 7916 " function* a2(,) {}", |
| 7917 "(function a3(,) {});", |
| 7918 "(function* a4(,) {});", |
| 7919 "(function (,) {});", |
| 7920 "(function* (,) {});", |
| 7921 "(,) => {};", |
| 7922 "a1(,);", |
| 7923 // no trailing commas after rest parameter declaration |
| 7924 " function a(...b,) {}", |
| 7925 " function* a(...b,) {}", |
| 7926 "(function a(...b,) {});", |
| 7927 "(function* a(...b,) {});", |
| 7928 "(function (...b,) {});", |
| 7929 "(function* (...b,) {});", |
| 7930 " function a(b, c, ...d,) {}", |
| 7931 " function* a(b, c, ...d,) {}", |
| 7932 "(function a(b, c, ...d,) {});", |
| 7933 "(function* a(b, c, ...d,) {});", |
| 7934 "(function (b, c, ...d,) {});", |
| 7935 "(function* (b, c, ...d,) {});", |
| 7936 "(...b,) => {};", |
| 7937 "(b, c, ...d,) => {};", |
| 7938 // parenthesized trailing comma without arrow is still an error |
| 7939 "(,);", |
| 7940 "(a,);", |
| 7941 "(a,b,c,);", |
| 7942 NULL |
| 7943 }; |
| 7944 // clang-format on |
| 7945 |
| 7946 static const ParserFlag always_flags[] = {kAllowHarmonyTrailingCommas}; |
| 7947 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, |
| 7948 arraysize(always_flags)); |
| 7949 } |
OLD | NEW |