|
| 1 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 2 | +// See end of file for extended copyright information. |
| 3 | + |
| 4 | +#include <gmock/gmock.h> |
| 5 | +#include <gtest/gtest.h> |
| 6 | +#include <quick-lint-js/diag-matcher.h> |
| 7 | +#include <quick-lint-js/parse-support.h> |
| 8 | +#include <quick-lint-js/port/char8.h> |
| 9 | +#include <string_view> |
| 10 | + |
| 11 | +namespace quick_lint_js { |
| 12 | +namespace { |
| 13 | +class Test_Parse_JSX_React : public Test_Parse_Expression {}; |
| 14 | + |
| 15 | +TEST_F(Test_Parse_JSX_React, correctly_capitalized_attribute) { |
| 16 | + test_parse_and_visit_module(u8R"(c = <td colSpan="2" />;)"_sv, no_diags, |
| 17 | + jsx_options); |
| 18 | + |
| 19 | + test_parse_and_visit_module(u8R"(c = <div onClick={handler} />;)"_sv, |
| 20 | + no_diags, jsx_options); |
| 21 | +} |
| 22 | + |
| 23 | +TEST_F(Test_Parse_JSX_React, event_attributes_should_be_camel_case) { |
| 24 | + test_parse_and_visit_module( |
| 25 | + u8"c = <div onclick={handler} />;"_sv, // |
| 26 | + u8" ^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag |
| 27 | + u8"{.expected_attribute_name=onClick}"_diag, // |
| 28 | + jsx_options); |
| 29 | + |
| 30 | + // TODO(strager): Should we also report that the handler's value is missing? |
| 31 | + test_parse_and_visit_module( |
| 32 | + u8"c = <div onclick />;"_sv, // |
| 33 | + u8" ^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag |
| 34 | + u8"{.expected_attribute_name=onClick}"_diag, // |
| 35 | + jsx_options); |
| 36 | + |
| 37 | + test_parse_and_visit_module( |
| 38 | + u8"c = <div onmouseenter={handler} />;"_sv, // |
| 39 | + u8" ^^^^^^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag |
| 40 | + u8"{.expected_attribute_name=onMouseEnter}"_diag, // |
| 41 | + jsx_options); |
| 42 | + |
| 43 | + test_parse_and_visit_module( |
| 44 | + u8"c = <div oncustomevent={handler} />;"_sv, // |
| 45 | + u8" ^^^^^^^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag |
| 46 | + u8"{.expected_attribute_name=onCustomevent}"_diag, // |
| 47 | + jsx_options); |
| 48 | +} |
| 49 | + |
| 50 | +TEST_F(Test_Parse_JSX_React, miscapitalized_attribute) { |
| 51 | + test_parse_and_visit_module( |
| 52 | + u8"c = <td colspan=\"2\" />;"_sv, // |
| 53 | + u8" ^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag |
| 54 | + u8"{.expected_attribute_name=colSpan}"_diag, // |
| 55 | + jsx_options); |
| 56 | + |
| 57 | + test_parse_and_visit_module( |
| 58 | + u8"c = <div onMouseenter={handler} />;"_sv, // |
| 59 | + u8" ^^^^^^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag |
| 60 | + u8"{.expected_attribute_name=onMouseEnter}"_diag, // |
| 61 | + jsx_options); |
| 62 | + |
| 63 | + test_parse_and_visit_module( |
| 64 | + u8"c = <div onmouseENTER={handler} />;"_sv, // |
| 65 | + u8" ^^^^^^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag |
| 66 | + u8"{.expected_attribute_name=onMouseEnter}"_diag, // |
| 67 | + jsx_options); |
| 68 | +} |
| 69 | + |
| 70 | +TEST_F(Test_Parse_JSX_React, commonly_misspelled_attribute) { |
| 71 | + test_parse_and_visit_module( |
| 72 | + u8"c = <span class=\"item\"></span>;"_sv, // |
| 73 | + u8" ^^^^^ Diag_JSX_Attribute_Renamed_By_React.attribute_name"_diag |
| 74 | + u8"{.react_attribute_name=className}"_diag, // |
| 75 | + jsx_options); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(Test_Parse_JSX_React, attribute_checking_ignores_namespaced_attributes) { |
| 79 | + test_parse_and_visit_module(u8R"(c = <div ns:onmouseenter={handler} />;)"_sv, |
| 80 | + no_diags, jsx_options); |
| 81 | + test_parse_and_visit_module( |
| 82 | + u8R"(c = <div onmouseenter:onmouseenter={handler} />;)"_sv, no_diags, |
| 83 | + jsx_options); |
| 84 | + test_parse_and_visit_module(u8R"(c = <div class:class="my-css-class" />;)"_sv, |
| 85 | + no_diags, jsx_options); |
| 86 | +} |
| 87 | + |
| 88 | +TEST_F(Test_Parse_JSX_React, attribute_checking_ignores_namespaced_elements) { |
| 89 | + test_parse_and_visit_module(u8R"(c = <svg:g onmouseenter={handler} />;)"_sv, |
| 90 | + no_diags, jsx_options); |
| 91 | + test_parse_and_visit_module(u8R"(c = <svg:g class="red" />;)"_sv, no_diags, |
| 92 | + jsx_options); |
| 93 | +} |
| 94 | + |
| 95 | +TEST_F(Test_Parse_JSX_React, attribute_checking_ignores_user_components) { |
| 96 | + test_parse_and_visit_module( |
| 97 | + u8R"(c = <MyComponent onmouseenter={handler} />;)"_sv, no_diags, |
| 98 | + jsx_options); |
| 99 | + |
| 100 | + test_parse_and_visit_module(u8R"(c = <MyComponent class="red" />;)"_sv, |
| 101 | + no_diags, jsx_options); |
| 102 | + |
| 103 | + test_parse_and_visit_module( |
| 104 | + u8R"(c = <mymodule.mycomponent onmouseenter={handler} />;)"_sv, no_diags, |
| 105 | + jsx_options); |
| 106 | + |
| 107 | + test_parse_and_visit_module( |
| 108 | + u8R"(c = <mymodule.mycomponent class="red" />;)"_sv, no_diags, |
| 109 | + jsx_options); |
| 110 | +} |
| 111 | +} |
| 112 | +} |
0 commit comments