From 03feac1df3be7be24eae2ed9085bce2be0d6059e Mon Sep 17 00:00:00 2001 From: Alan Huang Date: Wed, 10 Jul 2024 15:54:34 +0800 Subject: [PATCH 1/5] LPD-30242 Add IllegalHTMLTagsCheck --- .../formatter/check/IllegalHTMLTagsCheck.java | 66 +++++++++++++++++++ .../src/main/resources/sourcechecks.xml | 12 ++++ source-formatter.properties | 5 ++ 3 files changed, 83 insertions(+) create mode 100644 modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/IllegalHTMLTagsCheck.java diff --git a/modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/IllegalHTMLTagsCheck.java b/modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/IllegalHTMLTagsCheck.java new file mode 100644 index 00000000000000..c0dba69b41a897 --- /dev/null +++ b/modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/IllegalHTMLTagsCheck.java @@ -0,0 +1,66 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.source.formatter.check; + +import com.liferay.petra.string.StringBundler; +import com.liferay.portal.kernel.util.StringUtil; +import com.liferay.portal.tools.ToolsUtil; + +import java.util.List; + +/** + * @author Alan Huang + */ +public class IllegalHTMLTagsCheck extends BaseFileCheck { + + @Override + public boolean isLiferaySourceCheck() { + return true; + } + + @Override + protected String doProcess( + String fileName, String absolutePath, String content) { + + List avoidHtmlHeaderNames = getAttributeValues( + _AVOID_HTML_HEADER_NAMES_KEY, absolutePath); + + for (String avoidHtmlHeaderName : avoidHtmlHeaderNames) { + int x = -1; + + while (true) { + x = StringUtil.indexOfAny( + content, + new String[] { + "<" + avoidHtmlHeaderName + " ", + "<" + avoidHtmlHeaderName + ">" + }, + x + 1); + + if (x == -1) { + break; + } + + if (ToolsUtil.isInsideQuotes(content, x)) { + continue; + } + + addMessage( + fileName, + StringBundler.concat( + "Do not use '", avoidHtmlHeaderName, + "', use 'div' instead"), + getLineNumber(content, x)); + } + } + + return content; + } + + private static final String _AVOID_HTML_HEADER_NAMES_KEY = + "avoidHtmlHeaderNames"; + +} \ No newline at end of file diff --git a/modules/util/source-formatter/src/main/resources/sourcechecks.xml b/modules/util/source-formatter/src/main/resources/sourcechecks.xml index 0000e9a87b6efe..c8508586f4ab79 100644 --- a/modules/util/source-formatter/src/main/resources/sourcechecks.xml +++ b/modules/util/source-formatter/src/main/resources/sourcechecks.xml @@ -241,6 +241,10 @@ + + + + @@ -374,6 +378,10 @@ + + + + @@ -1226,6 +1234,10 @@ + + + + diff --git a/source-formatter.properties b/source-formatter.properties index f4abae261ba3df..3de4ebecc8e421 100644 --- a/source-formatter.properties +++ b/source-formatter.properties @@ -747,6 +747,11 @@ source.check.IfStatementCheck.enabled=true + source.check.IllegalHTMLTagsCheck.avoidHtmlHeaderNames=\ + h4,\ + h5,\ + h6 + source.check.IllegalImportsCheck.allowedOptionalFileNames=\ modules/apps/portal-vulcan/portal-vulcan-impl/src/main/java/com/liferay/portal/vulcan/internal/resource/OpenAPIResourceImpl.java,\ modules/apps/push-notifications/push-notifications-sender/push-notifications-sender-apple/src/main/java/com/liferay/push/notifications/sender/apple/internal/ApplePushNotificationsSender.java,\ From edf9a1dbf82bba3b7e995cabe7b4bdd78a8dd388 Mon Sep 17 00:00:00 2001 From: Alan Huang Date: Wed, 10 Jul 2024 16:40:46 +0800 Subject: [PATCH 2/5] LPD-30242 Generated --- .../src/main/resources/documentation/all_checks.markdown | 1 + .../main/resources/documentation/bug_prevention_checks.markdown | 1 + .../resources/documentation/ftl_source_processor_checks.markdown | 1 + .../documentation/html_source_processor_checks.markdown | 1 + .../resources/documentation/jsp_source_processor_checks.markdown | 1 + 5 files changed, 5 insertions(+) diff --git a/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown index a85cff28e35c28..6748654fa7752d 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown @@ -118,6 +118,7 @@ GroovyImportsCheck | [Styling](styling_checks.markdown#styling-checks) | .groovy HTMLEmptyLinesCheck | [Styling](styling_checks.markdown#styling-checks) | .html or .path | Finds missing and unnecessary empty lines. | HTMLWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | .html or .path | Finds missing and unnecessary whitespace in `.html` files. | [IfStatementCheck](check/if_statement_check.markdown#ifstatementcheck) | [Styling](styling_checks.markdown#styling-checks) | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds empty if-statements and consecutive if-statements with identical bodies. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .ftl, .html, .jsp, .jspf, .jspx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .ftl, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain deprecated taglibs in modules. | [IncorrectFileLocationCheck](check/incorrect_file_location_check.markdown#incorrectfilelocationcheck) | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | | Checks that `/src/*/java/` only contains `.java` files. | diff --git a/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown index f31d6ec48a4f00..7fc21db649fe67 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown @@ -54,6 +54,7 @@ GradleProvidedDependenciesCheck | .gradle | Validates the scope of dependencies GradleRestClientDependenciesCheck | .gradle | Validates the project dependencies `.*-rest-client` can only be used for `testIntegrationImplementation`. | GradleTestDependencyVersionCheck | .gradle | Checks the version for dependencies in gradle build files. | GradleTestUtilDeployDirCheck | .gradle | Checks for incorrect use of `deployDir`. | +IllegalHTMLTagsCheck | .ftl, .html, .jsp, .jspf, .jspx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | .ftl, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain deprecated taglibs in modules. | [IncorrectFileLocationCheck](check/incorrect_file_location_check.markdown#incorrectfilelocationcheck) | | Checks that `/src/*/java/` only contains `.java` files. | diff --git a/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown index 262a740b3913d3..4b81a84e523a27 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown @@ -11,4 +11,5 @@ FTLStylingCheck | [Styling](styling_checks.markdown#styling-checks) | Applies ru FTLTagAttributesCheck | [Styling](styling_checks.markdown#styling-checks) | Sorts and formats attributes values in tags. | FTLTagCheck | [Styling](styling_checks.markdown#styling-checks) | Finds cases where consecutive `#assign` can be combined. | FTLWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary whitespace in `.ftl` files. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | IllegalTaglibsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of certain deprecated taglibs in modules. | \ No newline at end of file diff --git a/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown index 616108aca8a80b..fb6005cefa96b8 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown @@ -4,4 +4,5 @@ Check | Category | Description ----- | -------- | ----------- HTMLEmptyLinesCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary empty lines. | HTMLWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary whitespace in `.html` files. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | XMLTagAttributesCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Performs several checks on tag attributes. | \ No newline at end of file diff --git a/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown index 34432b5f68d2d2..776f2abe2b2c16 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown @@ -21,6 +21,7 @@ FactoryCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-ch [GenericTypeCheck](check/generic_type_check.markdown#generictypecheck) | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Checks that generics are always specified to provide compile-time checking and removing the risk of `ClassCastException` during runtime. | [GetterUtilCheck](check/getter_util_check.markdown#getterutilcheck) | [Styling](styling_checks.markdown#styling-checks) | Finds cases where the default value is passed to `GetterUtil.get*` or `ParamUtil.get*`. | [IfStatementCheck](check/if_statement_check.markdown#ifstatementcheck) | [Styling](styling_checks.markdown#styling-checks) | Finds empty if-statements and consecutive if-statements with identical bodies. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of certain deprecated taglibs in modules. | InstanceofOrderCheck | [Styling](styling_checks.markdown#styling-checks) | Check the order of `instanceof` calls. | From 711a962fd65ede3b10b679a64921038a1da4154a Mon Sep 17 00:00:00 2001 From: Alan Huang Date: Thu, 11 Jul 2024 00:07:47 +0800 Subject: [PATCH 3/5] LPD-30242 Apply to *.js --- .../util/source-formatter/src/main/resources/sourcechecks.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/util/source-formatter/src/main/resources/sourcechecks.xml b/modules/util/source-formatter/src/main/resources/sourcechecks.xml index c8508586f4ab79..4bda5ccbb4d63d 100644 --- a/modules/util/source-formatter/src/main/resources/sourcechecks.xml +++ b/modules/util/source-formatter/src/main/resources/sourcechecks.xml @@ -1335,6 +1335,10 @@ + + + + From dc35db4f66c730d9e63ac2cebd0a987a16365aa9 Mon Sep 17 00:00:00 2001 From: Alan Huang Date: Thu, 11 Jul 2024 00:21:21 +0800 Subject: [PATCH 4/5] LPD-30242 Only enable for master --- .../util/source-formatter/src/main/resources/sourcechecks.xml | 4 ++++ source-formatter.properties | 2 ++ 2 files changed, 6 insertions(+) diff --git a/modules/util/source-formatter/src/main/resources/sourcechecks.xml b/modules/util/source-formatter/src/main/resources/sourcechecks.xml index 4bda5ccbb4d63d..bbbb7a9973434f 100644 --- a/modules/util/source-formatter/src/main/resources/sourcechecks.xml +++ b/modules/util/source-formatter/src/main/resources/sourcechecks.xml @@ -244,6 +244,7 @@ + @@ -381,6 +382,7 @@ + @@ -1237,6 +1239,7 @@ + @@ -1338,6 +1341,7 @@ + diff --git a/source-formatter.properties b/source-formatter.properties index 3de4ebecc8e421..a5bc62a9cb31c4 100644 --- a/source-formatter.properties +++ b/source-formatter.properties @@ -752,6 +752,8 @@ h5,\ h6 + source.check.IllegalHTMLTagsCheck.enabled=true + source.check.IllegalImportsCheck.allowedOptionalFileNames=\ modules/apps/portal-vulcan/portal-vulcan-impl/src/main/java/com/liferay/portal/vulcan/internal/resource/OpenAPIResourceImpl.java,\ modules/apps/push-notifications/push-notifications-sender/push-notifications-sender-apple/src/main/java/com/liferay/push/notifications/sender/apple/internal/ApplePushNotificationsSender.java,\ From 14cb49f2de6b953a21f3829483ddc3c861ae3775 Mon Sep 17 00:00:00 2001 From: Alan Huang Date: Thu, 11 Jul 2024 01:45:20 +0800 Subject: [PATCH 5/5] LPD-30242 Generated --- .../src/main/resources/documentation/all_checks.markdown | 2 +- .../main/resources/documentation/bug_prevention_checks.markdown | 2 +- .../resources/documentation/js_source_processor_checks.markdown | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown index 6748654fa7752d..33ace39c777374 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown @@ -118,7 +118,7 @@ GroovyImportsCheck | [Styling](styling_checks.markdown#styling-checks) | .groovy HTMLEmptyLinesCheck | [Styling](styling_checks.markdown#styling-checks) | .html or .path | Finds missing and unnecessary empty lines. | HTMLWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | .html or .path | Finds missing and unnecessary whitespace in `.html` files. | [IfStatementCheck](check/if_statement_check.markdown#ifstatementcheck) | [Styling](styling_checks.markdown#styling-checks) | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds empty if-statements and consecutive if-statements with identical bodies. | -IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .ftl, .html, .jsp, .jspf, .jspx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .ftl, .html, .js, .jsp, .jspf, .jspx, .jsx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .ftl, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain deprecated taglibs in modules. | [IncorrectFileLocationCheck](check/incorrect_file_location_check.markdown#incorrectfilelocationcheck) | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | | Checks that `/src/*/java/` only contains `.java` files. | diff --git a/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown index 7fc21db649fe67..81d38086b3d2e0 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown @@ -54,7 +54,7 @@ GradleProvidedDependenciesCheck | .gradle | Validates the scope of dependencies GradleRestClientDependenciesCheck | .gradle | Validates the project dependencies `.*-rest-client` can only be used for `testIntegrationImplementation`. | GradleTestDependencyVersionCheck | .gradle | Checks the version for dependencies in gradle build files. | GradleTestUtilDeployDirCheck | .gradle | Checks for incorrect use of `deployDir`. | -IllegalHTMLTagsCheck | .ftl, .html, .jsp, .jspf, .jspx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | +IllegalHTMLTagsCheck | .ftl, .html, .js, .jsp, .jspf, .jspx, .jsx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | .ftl, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain deprecated taglibs in modules. | [IncorrectFileLocationCheck](check/incorrect_file_location_check.markdown#incorrectfilelocationcheck) | | Checks that `/src/*/java/` only contains `.java` files. | diff --git a/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown index cb84f2f6fb414d..5e3bc34de61f3c 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown @@ -2,6 +2,7 @@ Check | Category | Description ----- | -------- | ----------- +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | [JSLodashDependencyCheck](check/js_lodash_dependency_check.markdown#jslodashdependencycheck) | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds incorrect use of `AUI._`. | JSStylingCheck | [Styling](styling_checks.markdown#styling-checks) | Applies rules to enforce consistency in code style. | JSWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary whitespace in `.js` files. |