From 0a98c85b8d0dce3523387d04080e5adc7d31f4bf Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Tue, 16 Nov 2021 00:08:34 +0100 Subject: [PATCH] added CommentedOutCode check (#512) * added CommentedOutCode check * removed 4.0.5 build jobs * fixed missing quote in file tag of XMLReporter * updated haxeparser lib * removed COC --- .github/workflows/checkstyle-linux.yml | 8 +- .haxerc | 2 +- .travis.yml | 4 +- CHANGELOG.md | 6 +- CODE_OF_CONDUCT.md | 74 -------- checkstyle.json | 3 + haxe_libraries/haxeparser.hxml | 4 +- resources/checkstyle-excludes-schema.json | 142 +++++++------- resources/checkstyle-schema.json | 177 +++++++++++------- resources/default-config.json | 4 + src/checkstyle/Checker.hx | 1 - .../checks/comments/CommentedOutCodeCheck.hx | 134 +++++++++++++ .../checks/whitespace/IndentationCheck.hx | 8 - src/checkstyle/reporter/XMLReporter.hx | 2 +- test/TestMain.hx | 1 - test/checkstyle/checks/CheckTestCase.hx | 3 +- .../comments/CommentedOutCodeCheckTest.hx | 86 +++++++++ test/checkstyle/config/ConfigParserTest.hx | 2 +- .../detect/DetectCodingStyleTest.hx | 8 + 19 files changed, 431 insertions(+), 238 deletions(-) delete mode 100644 CODE_OF_CONDUCT.md create mode 100644 src/checkstyle/checks/comments/CommentedOutCodeCheck.hx create mode 100644 test/checkstyle/checks/comments/CommentedOutCodeCheckTest.hx diff --git a/.github/workflows/checkstyle-linux.yml b/.github/workflows/checkstyle-linux.yml index e08110d5..f416e97d 100644 --- a/.github/workflows/checkstyle-linux.yml +++ b/.github/workflows/checkstyle-linux.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - haxe-version: ['4.0.5', '4.1.5', '4.2.1', 'nightly'] + haxe-version: ['4.1.5', '4.2.4', 'nightly'] env: CC_TEST_REPORTER_ID: c4eda639526d39fbcab7ab9fc68c4046d4e597df56dbcb552b42d27b3580b758 CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} @@ -26,7 +26,7 @@ jobs: with: node-version: 10 - name: Installing codeclimate client - if: matrix.haxe-version == '4.2.1' + if: matrix.haxe-version == '4.2.4' run: | curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter chmod +x ./cc-test-reporter @@ -60,7 +60,7 @@ jobs: - name: Run Java tests run: npx haxe testJava.hxml - name: Format and upload codeclimate coverage - if: success() && matrix.haxe-version == '4.2.1' + if: success() && matrix.haxe-version == '4.2.4' run: | ( \ cd src; \ @@ -68,5 +68,5 @@ jobs: ../cc-test-reporter upload-coverage; \ ) - name: Upload results to codecov - if: success() && (matrix.haxe-version == '4.2.1') + if: success() && (matrix.haxe-version == '4.2.4') run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports" diff --git a/.haxerc b/.haxerc index 2d173453..f383ce48 100644 --- a/.haxerc +++ b/.haxerc @@ -1,4 +1,4 @@ { - "version": "4.2.1", + "version": "4.2.4", "resolveLibs": "scoped" } \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 620d3e73..47d3c993 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ dist: xenial env: matrix: - - HAXE_VERSION=haxe4 - HAXE_VERSION=haxe41 - HAXE_VERSION=haxe42 - HAXE_VERSION=nightly @@ -14,9 +13,8 @@ env: install: - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter; chmod +x ./cc-test-reporter - npm install - - if [[ "$HAXE_VERSION" == "haxe4" ]]; then npx lix download haxe 4.0.5; npx lix use haxe 4.0.5; fi - if [[ "$HAXE_VERSION" == "haxe41" ]]; then npx lix download haxe 4.1.5; npx lix use haxe 4.1.5; fi - - if [[ "$HAXE_VERSION" == "haxe42" ]]; then npx lix download haxe 4.2.1; npx lix use haxe 4.2.1; fi + - if [[ "$HAXE_VERSION" == "haxe42" ]]; then npx lix download haxe 4.2.4; npx lix use haxe 4.2.4; fi - if [[ "$HAXE_VERSION" == "nightly" ]]; then npx lix download haxe nightly; npx lix use haxe nightly; fi - npx lix download - npx haxe -version diff --git a/CHANGELOG.md b/CHANGELOG.md index 14611da6..d71a609a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,12 @@ ## dev branch / next version (2.x.x) -- Added related messages to reporters for `CodeSimilarity` check ([#506](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/506)) +- New check `CommentedOutCode` to check comments for commented out code fragments ([#512](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/512)) - Fixed Haxe nightly compilation ([#505](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/505)) +- Added related messages to reporters for `CodeSimilarity` check ([#506](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/506)) - Fixed null pointer exception in `UnusedImport` check ([#507](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/507)) -- Updated to Haxe 4.1.5 ([#505](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/505)) +- Updated to Haxe 4.2.4 ([#512](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/512)) +- Retired Haxe 4.0.5 compile support ([#512](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/512)) ## version 2.7.0 (2020-12-23) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md deleted file mode 100644 index e2f9928d..00000000 --- a/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,74 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and -our community a harassment-free experience for everyone, regardless of age, body -size, disability, ethnicity, gender identity and expression, level of experience, -nationality, personal appearance, race, religion, or sexual identity and -orientation. - -## Our Standards - -Examples of behavior that contributes to creating a positive environment -include: - -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery and unwelcome sexual attention or - advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic - address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -## Our Responsibilities - -Project maintainers are responsible for clarifying the standards of acceptable -behavior and are expected to take appropriate and fair corrective action in -response to any instances of unacceptable behavior. - -Project maintainers have the right and responsibility to remove, edit, or -reject comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct, or to ban temporarily or -permanently any contributor for other behaviors that they deem inappropriate, -threatening, offensive, or harmful. - -## Scope - -This Code of Conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. Examples of -representing a project or community include using an official project e-mail -address, posting via an official social media account, or acting as an appointed -representative at an online or offline event. Representation of a project may be -further defined and clarified by project maintainers. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at adiinteractive@gmail.com. All -complaints will be reviewed and investigated and will result in a response that -is deemed necessary and appropriate to the circumstances. The project team is -obligated to maintain confidentiality with regard to the reporter of an incident. -Further details of specific enforcement policies may be posted separately. - -Project maintainers who do not follow or enforce the Code of Conduct in good -faith may face temporary or permanent repercussions as determined by other -members of the project's leadership. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, -available at [http://contributor-covenant.org/version/1/4][version] - -[homepage]: http://contributor-covenant.org -[version]: http://contributor-covenant.org/version/1/4/ diff --git a/checkstyle.json b/checkstyle.json index dbebee8f..3821a96c 100644 --- a/checkstyle.json +++ b/checkstyle.json @@ -53,6 +53,9 @@ { "type": "DefaultComesLast" }, + { + "type": "CommentedOutCode" + }, { "type": "DocCommentStyle" }, diff --git a/haxe_libraries/haxeparser.hxml b/haxe_libraries/haxeparser.hxml index c4d19d29..1c2f8cdf 100644 --- a/haxe_libraries/haxeparser.hxml +++ b/haxe_libraries/haxeparser.hxml @@ -1,4 +1,4 @@ -# @install: lix --silent download "gh://github.com/simn/haxeparser#9eee8e0db1dc628d53334e30ebf08e31cb1c92b9" into haxeparser/3.3.0/github/9eee8e0db1dc628d53334e30ebf08e31cb1c92b9 +# @install: lix --silent download "gh://github.com/simn/haxeparser#e4f91798d502cd2811a363c1d2fe1456aa9feb02" into haxeparser/3.3.0/github/e4f91798d502cd2811a363c1d2fe1456aa9feb02 -lib hxparse --cp ${HAXE_LIBCACHE}/haxeparser/3.3.0/github/9eee8e0db1dc628d53334e30ebf08e31cb1c92b9/src +-cp ${HAXE_LIBCACHE}/haxeparser/3.3.0/github/e4f91798d502cd2811a363c1d2fe1456aa9feb02/src -D haxeparser=3.3.0 \ No newline at end of file diff --git a/resources/checkstyle-excludes-schema.json b/resources/checkstyle-excludes-schema.json index f16f5a34..1e79f7b8 100644 --- a/resources/checkstyle-excludes-schema.json +++ b/resources/checkstyle-excludes-schema.json @@ -18,38 +18,38 @@ }, "UnusedImport": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 77 + "propertyOrder": 78 }, "Dynamic": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 19 + "propertyOrder": 20 }, "Final": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 27 + "propertyOrder": 28 }, "MultipleStringLiterals": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 44 + "propertyOrder": 45 }, "NeedBraces": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 46 + "propertyOrder": 47 }, "SeparatorWhitespace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 63 + "propertyOrder": 64 }, "all": { "$ref": "#/definitions/ExcludeFilterList" }, "LocalVariableName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 37 + "propertyOrder": 38 }, "NestedForDepth": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 48 + "propertyOrder": 49 }, "Anonymous": { "$ref": "#/definitions/ExcludeFilterList", @@ -57,11 +57,11 @@ }, "MultipleVariableDeclarations": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 45 + "propertyOrder": 46 }, "NestedTryDepth": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 50 + "propertyOrder": 51 }, "BlockBreakingConditional": { "$ref": "#/definitions/ExcludeFilterList", @@ -73,55 +73,55 @@ }, "EmptyBlock": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 21 + "propertyOrder": 22 }, "ExtendedEmptyLines": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 24 + "propertyOrder": 25 }, "ReturnCount": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 61 + "propertyOrder": 62 }, "DefaultComesLast": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 17 + "propertyOrder": 18 }, "HexadecimalLiteral": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 28 + "propertyOrder": 29 }, "WhitespaceAfter": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 81 + "propertyOrder": 82 }, "ConstantName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 15 + "propertyOrder": 16 }, "EmptyPackage": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 23 + "propertyOrder": 24 }, "Interface": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 33 + "propertyOrder": 34 }, "WhitespaceAround": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 82 + "propertyOrder": 83 }, "NestedIfDepth": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 49 + "propertyOrder": 50 }, "ParameterName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 54 + "propertyOrder": 55 }, "NullableParameter": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 51 + "propertyOrder": 52 }, "AvoidStarImport": { "$ref": "#/definitions/ExcludeFilterList", @@ -129,7 +129,7 @@ }, "SeparatorWrap": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 64 + "propertyOrder": 65 }, "AvoidIdentifier": { "$ref": "#/definitions/ExcludeFilterList", @@ -137,19 +137,19 @@ }, "RedundantAllowMeta": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 58 + "propertyOrder": 59 }, "HiddenField": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 29 + "propertyOrder": 30 }, "UnnecessaryConstructor": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 76 + "propertyOrder": 77 }, "SimplifyBooleanExpression": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 65 + "propertyOrder": 66 }, "path": { "description": "filters excludes relative to\n\t- RELATIVE_TO_PROJECT = use project root\n\t- RELATIVE_TO_SOURCE = use path(s) specified via \"-s \" command line switches", @@ -161,23 +161,23 @@ }, "MethodCount": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 40 + "propertyOrder": 41 }, "UnusedLocalVar": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 78 + "propertyOrder": 79 }, "TabForAligning": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 70 + "propertyOrder": 71 }, "MethodName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 42 + "propertyOrder": 43 }, "RightCurly": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 62 + "propertyOrder": 63 }, "AvoidTernaryOperator": { "$ref": "#/definitions/ExcludeFilterList", @@ -185,7 +185,7 @@ }, "ParameterNumber": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 55 + "propertyOrder": 56 }, "ArrowFunction": { "$ref": "#/definitions/ExcludeFilterList", @@ -193,39 +193,43 @@ }, "MethodLength": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 41 + "propertyOrder": 42 }, "ConditionalCompilation": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 14 + "propertyOrder": 15 }, "EmptyLines": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 22 + "propertyOrder": 23 }, "MemberName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 39 + "propertyOrder": 40 }, "ERegLiteral": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 20 + "propertyOrder": 21 }, "RedundantModifier": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 59 + "propertyOrder": 60 }, "Type": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 73 + "propertyOrder": 74 }, "TypeName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 75 + "propertyOrder": 76 }, "Indentation": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 30 + "propertyOrder": 31 + }, + "CommentedOutCode": { + "$ref": "#/definitions/ExcludeFilterList", + "propertyOrder": 14 }, "version": { "maximum": 1, @@ -234,39 +238,39 @@ }, "Return": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 60 + "propertyOrder": 61 }, "SimplifyBooleanReturn": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 66 + "propertyOrder": 67 }, "TODOComment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 69 + "propertyOrder": 70 }, "VarTypeHint": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 79 + "propertyOrder": 80 }, "NestedControlFlow": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 47 + "propertyOrder": 48 }, "OperatorWhitespace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 52 + "propertyOrder": 53 }, "Spacing": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 67 + "propertyOrder": 68 }, "IndentationCharacter": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 31 + "propertyOrder": 32 }, "MagicNumber": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 38 + "propertyOrder": 39 }, "ArrayLiteral": { "$ref": "#/definitions/ExcludeFilterList", @@ -274,35 +278,35 @@ }, "TrailingWhitespace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 72 + "propertyOrder": 73 }, "VariableInitialisation": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 80 + "propertyOrder": 81 }, "LineLength": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 35 + "propertyOrder": 36 }, "ListenerName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 36 + "propertyOrder": 37 }, "FileLength": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 26 + "propertyOrder": 27 }, "CyclomaticComplexity": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 16 + "propertyOrder": 17 }, "StringLiteral": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 68 + "propertyOrder": 69 }, "Trace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 71 + "propertyOrder": 72 }, "ArrayAccess": { "$ref": "#/definitions/ExcludeFilterList", @@ -310,19 +314,19 @@ }, "PublicAccessor": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 56 + "propertyOrder": 57 }, "DocCommentStyle": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 18 + "propertyOrder": 19 }, "InnerAssignment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 32 + "propertyOrder": 33 }, "RedundantAccessMeta": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 57 + "propertyOrder": 58 }, "CodeSimilarity": { "$ref": "#/definitions/ExcludeFilterList", @@ -330,23 +334,23 @@ }, "OperatorWrap": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 53 + "propertyOrder": 54 }, "TypeDocComment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 74 + "propertyOrder": 75 }, "LeftCurly": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 34 + "propertyOrder": 35 }, "ModifierOrder": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 43 + "propertyOrder": 44 }, "FieldDocComment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 25 + "propertyOrder": 26 } }, "type": "object" diff --git a/resources/checkstyle-schema.json b/resources/checkstyle-schema.json index 2bb8b7bd..ae757662 100644 --- a/resources/checkstyle-schema.json +++ b/resources/checkstyle-schema.json @@ -2206,6 +2206,38 @@ }, "type": "object" }, + "CommentedOutCodeCheck": { + "description": "Checks sections of commented out code", + "additionalProperties": false, + "properties": { + "type": { + "description": "Checks sections of commented out code", + "type": "string", + "enum": [ + "CommentedOutCode" + ] + }, + "props": { + "description": "Checks sections of commented out code", + "additionalProperties": false, + "properties": { + "severity": { + "description": "sets gravity of reported violations:\n\t- IGNORE = do not report violations, violations do not appear anywhere in output\n\t- INFO = all violations have info / lowest priority\n\t- WARNING = all violations have warning / medium priority\n\t- ERROR = all violations have error / highest priority", + "type": "string", + "enum": [ + "INFO", + "WARNING", + "ERROR", + "IGNORE" + ], + "propertyOrder": 0 + } + }, + "type": "object" + } + }, + "type": "object" + }, "MultipleVariableDeclarationsCheck": { "description": "Checks that each variable declaration is in its own statement and on its own line.", "additionalProperties": false, @@ -2986,38 +3018,38 @@ }, "UnusedImport": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 77 + "propertyOrder": 78 }, "Dynamic": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 19 + "propertyOrder": 20 }, "Final": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 27 + "propertyOrder": 28 }, "MultipleStringLiterals": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 44 + "propertyOrder": 45 }, "NeedBraces": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 46 + "propertyOrder": 47 }, "SeparatorWhitespace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 63 + "propertyOrder": 64 }, "all": { "$ref": "#/definitions/ExcludeFilterList" }, "LocalVariableName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 37 + "propertyOrder": 38 }, "NestedForDepth": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 48 + "propertyOrder": 49 }, "Anonymous": { "$ref": "#/definitions/ExcludeFilterList", @@ -3025,11 +3057,11 @@ }, "MultipleVariableDeclarations": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 45 + "propertyOrder": 46 }, "NestedTryDepth": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 50 + "propertyOrder": 51 }, "BlockBreakingConditional": { "$ref": "#/definitions/ExcludeFilterList", @@ -3041,55 +3073,55 @@ }, "EmptyBlock": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 21 + "propertyOrder": 22 }, "ExtendedEmptyLines": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 24 + "propertyOrder": 25 }, "ReturnCount": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 61 + "propertyOrder": 62 }, "DefaultComesLast": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 17 + "propertyOrder": 18 }, "HexadecimalLiteral": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 28 + "propertyOrder": 29 }, "WhitespaceAfter": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 81 + "propertyOrder": 82 }, "ConstantName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 15 + "propertyOrder": 16 }, "EmptyPackage": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 23 + "propertyOrder": 24 }, "Interface": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 33 + "propertyOrder": 34 }, "WhitespaceAround": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 82 + "propertyOrder": 83 }, "NestedIfDepth": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 49 + "propertyOrder": 50 }, "ParameterName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 54 + "propertyOrder": 55 }, "NullableParameter": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 51 + "propertyOrder": 52 }, "AvoidStarImport": { "$ref": "#/definitions/ExcludeFilterList", @@ -3097,7 +3129,7 @@ }, "SeparatorWrap": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 64 + "propertyOrder": 65 }, "AvoidIdentifier": { "$ref": "#/definitions/ExcludeFilterList", @@ -3105,19 +3137,19 @@ }, "RedundantAllowMeta": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 58 + "propertyOrder": 59 }, "HiddenField": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 29 + "propertyOrder": 30 }, "UnnecessaryConstructor": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 76 + "propertyOrder": 77 }, "SimplifyBooleanExpression": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 65 + "propertyOrder": 66 }, "path": { "description": "filters excludes relative to\n\t- RELATIVE_TO_PROJECT = use project root\n\t- RELATIVE_TO_SOURCE = use path(s) specified via \"-s \" command line switches", @@ -3129,23 +3161,23 @@ }, "MethodCount": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 40 + "propertyOrder": 41 }, "UnusedLocalVar": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 78 + "propertyOrder": 79 }, "TabForAligning": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 70 + "propertyOrder": 71 }, "MethodName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 42 + "propertyOrder": 43 }, "RightCurly": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 62 + "propertyOrder": 63 }, "AvoidTernaryOperator": { "$ref": "#/definitions/ExcludeFilterList", @@ -3153,7 +3185,7 @@ }, "ParameterNumber": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 55 + "propertyOrder": 56 }, "ArrowFunction": { "$ref": "#/definitions/ExcludeFilterList", @@ -3161,39 +3193,43 @@ }, "MethodLength": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 41 + "propertyOrder": 42 }, "ConditionalCompilation": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 14 + "propertyOrder": 15 }, "EmptyLines": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 22 + "propertyOrder": 23 }, "MemberName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 39 + "propertyOrder": 40 }, "ERegLiteral": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 20 + "propertyOrder": 21 }, "RedundantModifier": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 59 + "propertyOrder": 60 }, "Type": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 73 + "propertyOrder": 74 }, "TypeName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 75 + "propertyOrder": 76 }, "Indentation": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 30 + "propertyOrder": 31 + }, + "CommentedOutCode": { + "$ref": "#/definitions/ExcludeFilterList", + "propertyOrder": 14 }, "version": { "maximum": 1, @@ -3202,39 +3238,39 @@ }, "Return": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 60 + "propertyOrder": 61 }, "SimplifyBooleanReturn": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 66 + "propertyOrder": 67 }, "TODOComment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 69 + "propertyOrder": 70 }, "VarTypeHint": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 79 + "propertyOrder": 80 }, "NestedControlFlow": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 47 + "propertyOrder": 48 }, "OperatorWhitespace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 52 + "propertyOrder": 53 }, "Spacing": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 67 + "propertyOrder": 68 }, "IndentationCharacter": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 31 + "propertyOrder": 32 }, "MagicNumber": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 38 + "propertyOrder": 39 }, "ArrayLiteral": { "$ref": "#/definitions/ExcludeFilterList", @@ -3242,35 +3278,35 @@ }, "TrailingWhitespace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 72 + "propertyOrder": 73 }, "VariableInitialisation": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 80 + "propertyOrder": 81 }, "LineLength": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 35 + "propertyOrder": 36 }, "ListenerName": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 36 + "propertyOrder": 37 }, "FileLength": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 26 + "propertyOrder": 27 }, "CyclomaticComplexity": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 16 + "propertyOrder": 17 }, "StringLiteral": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 68 + "propertyOrder": 69 }, "Trace": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 71 + "propertyOrder": 72 }, "ArrayAccess": { "$ref": "#/definitions/ExcludeFilterList", @@ -3278,19 +3314,19 @@ }, "PublicAccessor": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 56 + "propertyOrder": 57 }, "DocCommentStyle": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 18 + "propertyOrder": 19 }, "InnerAssignment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 32 + "propertyOrder": 33 }, "RedundantAccessMeta": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 57 + "propertyOrder": 58 }, "CodeSimilarity": { "$ref": "#/definitions/ExcludeFilterList", @@ -3298,23 +3334,23 @@ }, "OperatorWrap": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 53 + "propertyOrder": 54 }, "TypeDocComment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 74 + "propertyOrder": 75 }, "LeftCurly": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 34 + "propertyOrder": 35 }, "ModifierOrder": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 43 + "propertyOrder": 44 }, "FieldDocComment": { "$ref": "#/definitions/ExcludeFilterList", - "propertyOrder": 25 + "propertyOrder": 26 } }, "type": "object" @@ -4274,6 +4310,9 @@ { "$ref": "#/definitions/CodeSimilarityCheck" }, + { + "$ref": "#/definitions/CommentedOutCodeCheck" + }, { "$ref": "#/definitions/ConditionalCompilationCheck" }, diff --git a/resources/default-config.json b/resources/default-config.json index da14a7e7..4e044d3b 100644 --- a/resources/default-config.json +++ b/resources/default-config.json @@ -61,6 +61,10 @@ "thresholdSimilar": 120 } }, + { + "type": "CommentedOutCode", + "props": {} + }, { "type": "ConditionalCompilation", "props": { diff --git a/src/checkstyle/Checker.hx b/src/checkstyle/Checker.hx index 3f4f2caf..0a1096bc 100644 --- a/src/checkstyle/Checker.hx +++ b/src/checkstyle/Checker.hx @@ -208,7 +208,6 @@ class Checker { makePosIndices(); makeTokens(); makeASTs(); - // if (asts.length <= 0) return false; getTokenTree(); excludesRanges = ExcludeManager.INSTANCE.getPosExcludes(this); } diff --git a/src/checkstyle/checks/comments/CommentedOutCodeCheck.hx b/src/checkstyle/checks/comments/CommentedOutCodeCheck.hx new file mode 100644 index 00000000..5728dbf6 --- /dev/null +++ b/src/checkstyle/checks/comments/CommentedOutCodeCheck.hx @@ -0,0 +1,134 @@ +package checkstyle.checks.comments; + +import byte.ByteData; +import haxe.Exception; +import haxeparser.HaxeParser; + +/** + Checks sections of commented out code +**/ +@name("CommentedOutCode") +@desc("Checks sections of commented out code") +class CommentedOutCodeCheck extends Check { + static inline final COMMENTED_OUT_CODE:String = "This block of commented-out lines of code should be removed"; + + public function new() { + super(TOKEN); + } + + override function actualRun() { + var root:TokenTree = checker.getTokenTree(); + var comments = root.filterCallback(function(token:TokenTree, depth:Int):FilterResult { + return switch (token.tok) { + case CommentLine(_): + FoundSkipSubtree; + case Comment(_): + FoundSkipSubtree; + default: + GoDeeper; + } + }); + + var currentRun:Array = []; + var lastIndex:Int = -1; + for (token in comments) { + if (isPosSuppressed(token.pos)) continue; + + switch (token.tok) { + case Comment(text): + while (text.startsWith("*")) { + text = text.substr(1); + } + while (text.endsWith("*")) { + text = text.substr(0, text.length - 1); + } + checkForCommentedCode(text, token.pos); + case CommentLine(_): + if ((lastIndex == -1) || (lastIndex + 1 == token.index)) { + currentRun.push(token); + lastIndex = token.index; + continue; + } + checkTokenRunIterations(currentRun); + currentRun = [token]; + lastIndex = token.index; + default: + } + } + if (currentRun.length > 0) { + checkTokenRunIterations(currentRun); + } + } + + function checkTokenRunIterations(run:Array) { + if (run.length <= 0) return; + var runCopy:Array = run.copy(); + var removedTokens:Array = []; + + while (runCopy.length > 0) { + if (checkTokenRun(runCopy)) { + checkTokenRunIterations(removedTokens); + return; + } + var lastToken:TokenTree = runCopy.pop(); + if (runCopy.length > 1) { + removedTokens.unshift(lastToken); + } + } + checkTokenRunIterations(removedTokens); + } + + function checkTokenRun(run:Array):Bool { + if (run.length <= 0) return false; + var text:String = run.map(token -> switch (token.tok) { + case CommentLine(text): + text; + default: + ""; + }).join("\n"); + var first:TokenTree = run[0]; + var last:TokenTree = run[run.length - 1]; + var pos:Position = { + file: first.pos.file, + min: first.pos.min, + max: last.pos.max + }; + return checkForCommentedCode(text, pos); + } + + function checkForCommentedCode(text:String, pos:Position):Bool { + if (text.trim().length <= 0) return false; + try { + var parser = new HaxeParser(ByteData.ofString(text), checker.file.name); + parser.parse(); + logPos(COMMENTED_OUT_CODE, pos, "CommentedOutCode"); + return true; + } + catch (e:Exception) {} + try { + var parser = new HaxeParser(ByteData.ofString("function code () {" + text + "}"), checker.file.name); + parser.parse(); + logPos(COMMENTED_OUT_CODE, pos, "CommentedOutCode"); + return true; + } + catch (e:Exception) {} + try { + var parser = new HaxeParser(ByteData.ofString("class Code {" + text + "}"), checker.file.name); + parser.parse(); + logPos(COMMENTED_OUT_CODE, pos, "CommentedOutCode"); + return true; + } + catch (e:Exception) {} + return false; + } + + override public function detectableInstances():DetectableInstances { + return [{ + fixed: [], + properties: [{ + propertyName: "severity", + values: [SeverityLevel.INFO] + }] + }]; + } +} \ No newline at end of file diff --git a/src/checkstyle/checks/whitespace/IndentationCheck.hx b/src/checkstyle/checks/whitespace/IndentationCheck.hx index 950bd8fb..ac8b7d13 100644 --- a/src/checkstyle/checks/whitespace/IndentationCheck.hx +++ b/src/checkstyle/checks/whitespace/IndentationCheck.hx @@ -148,14 +148,6 @@ class IndentationCheck extends Check { continue; default: } - // var type:BrOpenType = TokenTreeCheckUtils.getBrOpenType(token); - // switch (type) { - // case BLOCK: - // case TYPEDEFDECL: - // case OBJECTDECL: - // case ANONTYPE: - // case UNKNOWN: - // } case Kwd(KwdFunction): calcLineIndentationFunction(token, lineIndentation); case Kwd(KwdIf), Kwd(KwdElse): diff --git a/src/checkstyle/reporter/XMLReporter.hx b/src/checkstyle/reporter/XMLReporter.hx index 0fe34bab..fadc89f0 100644 --- a/src/checkstyle/reporter/XMLReporter.hx +++ b/src/checkstyle/reporter/XMLReporter.hx @@ -52,7 +52,7 @@ class XMLReporter extends BaseReporter { } function makeFileTag(sb:StringBuf, file:String) { - sb.add('\t\n'); for (message in messages) { if (file == message.fileName) { sb.add(formatMessage(message, message)); diff --git a/test/TestMain.hx b/test/TestMain.hx index fb7d9fd3..0c4c38cf 100644 --- a/test/TestMain.hx +++ b/test/TestMain.hx @@ -31,7 +31,6 @@ class TestMain { } function completionHandler(success:Bool) { - // setupCoverageReport(); #if eval if (!success) { Sys.exit(1); diff --git a/test/checkstyle/checks/CheckTestCase.hx b/test/checkstyle/checks/CheckTestCase.hx index e02b1816..0e07cd22 100644 --- a/test/checkstyle/checks/CheckTestCase.hx +++ b/test/checkstyle/checks/CheckTestCase.hx @@ -2,8 +2,8 @@ package checkstyle.checks; import byte.ByteData; import checkstyle.CheckFile; -import checkstyle.Message; import checkstyle.Checker; +import checkstyle.Message; import checkstyle.reporter.IReporter; import checkstyle.reporter.ReporterManager; @@ -43,7 +43,6 @@ class CheckTestCase { Assert.areEqual(expected[0], messages[i].message, pos); } } - // trace(messages.map(m -> m.message)); Assert.areEqual(expected.length, messages.length, pos); for (i in 0...expected.length) { Assert.areEqual(expected[i], messages[i].message, pos); diff --git a/test/checkstyle/checks/comments/CommentedOutCodeCheckTest.hx b/test/checkstyle/checks/comments/CommentedOutCodeCheckTest.hx new file mode 100644 index 00000000..b02e1187 --- /dev/null +++ b/test/checkstyle/checks/comments/CommentedOutCodeCheckTest.hx @@ -0,0 +1,86 @@ +package checkstyle.checks.comments; + +class CommentedOutCodeCheckTest extends CheckTestCase { + static inline var MSG_REMOVE_COMMENTED_CODE:String = "This block of commented-out lines of code should be removed"; + + @Test + public function testComments() { + var check = new CommentedOutCodeCheck(); + assertMsg(check, BLOCK_COMMENT_VAR, MSG_REMOVE_COMMENTED_CODE); + assertMsg(check, BLOCK_COMMENT_STAR_VAR, MSG_REMOVE_COMMENTED_CODE); + assertMsg(check, BLOCK_COMMENT_MULTI_STAR_VAR, MSG_REMOVE_COMMENTED_CODE); + assertMsg(check, LINE_COMMENT_VAR, MSG_REMOVE_COMMENTED_CODE); + assertMsg(check, LINE_COMMENT_VAR2, MSG_REMOVE_COMMENTED_CODE); + assertMsg(check, LINE_COMMENT_SWITCH, MSG_REMOVE_COMMENTED_CODE); + assertMsg(check, LINE_COMMENT_SWITCH2, MSG_REMOVE_COMMENTED_CODE); + assertNoMsg(check, LINE_COMMENT_EMPTY); + assertNoMsg(check, LINE_COMMENT_SUPRESSED); + // xx assertMessages(check, ONE_STAR_ONE_STAR_ONE_STAR, [MSG_SHOULD_USE_TWO_STARS, MSG_SHOULD_NOT_START_WITH_STAR]); + } +} + +enum abstract CommentedOutCodeCheckTests(String) to String { + var BLOCK_COMMENT_VAR = " + /* + var test:String = ''; + */ + class Test {} + "; + var BLOCK_COMMENT_STAR_VAR = " + /** + var test:String = ''; + **/ + class Test {} + "; + var BLOCK_COMMENT_MULTI_STAR_VAR = " + /********************************************** + var test:String = ''; + **********************************************/ + class Test {} + "; + var LINE_COMMENT_VAR = " + // var test:String = ''; + class Test {} + "; + var LINE_COMMENT_VAR2 = " + // var test:String = ''; + // var test2:String = ''; + // var test3:String = ''; + class Test {} + "; + var LINE_COMMENT_SWITCH = " + // switch(test) { + // case ValueA: + // var a = ''; + // call(a); + // case ValueB: + // case ValueC: + // default: + // } + class Test {} + "; + var LINE_COMMENT_SWITCH2 = " + // text comment + // switch(test) { + // case ValueA: + // var a = ''; + // call(a); + // case ValueB: + // case ValueC: + // default: + // } + class Test {} + "; + var LINE_COMMENT_EMPTY = " + // text comment + // + /* */ + class Test {} + "; + var LINE_COMMENT_SUPRESSED = " + @SuppressWarnings('checkstyle:CommentedOutCode') + class Test { + // var test:String = ''; + } + "; +} \ No newline at end of file diff --git a/test/checkstyle/config/ConfigParserTest.hx b/test/checkstyle/config/ConfigParserTest.hx index c9c0ed71..cc6dbcd2 100644 --- a/test/checkstyle/config/ConfigParserTest.hx +++ b/test/checkstyle/config/ConfigParserTest.hx @@ -4,7 +4,7 @@ import checkstyle.utils.ConfigUtils; class ConfigParserTest { static inline var LOCAL_PATH:String = "./"; - static inline var TEST_COUNT:Int = 79; + static inline var TEST_COUNT:Int = 80; @Test public function testCheckstyleConfig() { diff --git a/test/checkstyle/detect/DetectCodingStyleTest.hx b/test/checkstyle/detect/DetectCodingStyleTest.hx index 7efcd4fd..828f0be4 100644 --- a/test/checkstyle/detect/DetectCodingStyleTest.hx +++ b/test/checkstyle/detect/DetectCodingStyleTest.hx @@ -19,6 +19,7 @@ import checkstyle.checks.coding.NullableParameterCheck; import checkstyle.checks.coding.ReturnCountCheck; import checkstyle.checks.coding.TraceCheck; import checkstyle.checks.coding.UnusedLocalVarCheck; +import checkstyle.checks.comments.CommentedOutCodeCheck; import checkstyle.checks.comments.DocCommentStyleCheck; import checkstyle.checks.comments.FieldDocCommentCheck; import checkstyle.checks.comments.TODOCommentCheck; @@ -215,6 +216,13 @@ class DetectCodingStyleTest { } // checkstyle.checks.comments + @Test + public function testCommentedOutCode() { + var detectedChecks:Array = DetectCodingStyle.detectCodingStyle([new CommentedOutCodeCheck()], [buildCheckFile(SAMPLE_CODING_STYLE)]); + Assert.areEqual(1, detectedChecks.length); + Assert.areEqual("CommentedOutCode", detectedChecks[0].type); + } + @Test public function testDetectDocCommentStyle() { var detectedChecks:Array = DetectCodingStyle.detectCodingStyle([new DocCommentStyleCheck()], [buildCheckFile(SAMPLE_CODING_STYLE)]);