Uploaded image for project: 'Minimal Marketable Features'
  1. Minimal Marketable Features
  2. MMF-1416

SonarApex provides support for the Apex language

    Details

    • Type: MMF
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Labels:

      Description

      WHY

      See the WHY section in MMF-1214

      WHAT

      The Apex Language

      https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_what_is_apex.htm#!

      https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf

      Slang AST

      We need to find a parser for Apex to get an AST on which we can run rules. We should think about the following criteria when looking for a parser:

      • license
      • features: does it provide precise location of tokens? comments?
      • ease of integration

      Parser used in Force.com IDE Core

      SalesForce is using an Apex parser to build Force.com IDE Core (based on Eclipse). This parser (Jorje) is provided as a dependency available here: https://github.com/forcedotcom/idecore/blob/master/com.salesforce.ide.apex.core/lib/apex-jorje-parser-1.0-sfdc-224-SNAPSHOT.jar.
      It is not open source and was updated in 2016. No update will be provided because SF is targeting VS Code instead of Eclipse for their IDE.
      There is a great chance apex-jorje-parser-1.0-sfdc-224-SNAPSHOT.ja can't parse recent version of Apex.
      The license of apex-jorje-parser-1.0-sfdc-224-SNAPSHOT.jar is not specified in the MANIFEST. We don't know if we can rely on it.

      Parser used in Apex Code Editor for Visual Studio Code

      According to https://github.com/pmd/pmd/issues/677, Apex Code Editor for Visual Studio Code:

      apex-jorje-lsp.jar looks a good candidate because it is updated frequently so it should be able to parse all the Apex versions. It has the same problem as apex-jorje-parser-1.0-sfdc-224-SNAPSHOT.jar: we don't know from a license point of view if we can rely on it.

      PMD-APEX

      PMD-APEX is an extension of PMD. It was relying on apex-jorje-parser-1.0-sfdc-224-SNAPSHOT.jar and moved recently on apex-jorje-lsp-minimized-2017-11-17.jar that is built from apex-jorje-lsp.jar (see: https://github.com/pmd/pmd/tree/master/pmd-apex-jorje).
      apex-jorje-lsp-minimized-2017-11-17.jar is a minimized version of apex-jorje-lsp.jar containing only the Apex parser. There is no license associated to it.

      Reference: https://github.com/forcedotcom/idecore/issues/167

      ANTLR Grammar

      There is an Apex ANTLR V4 Grammar available here: https://github.com/antlr/grammars-v4/tree/master/apex
      This could be a good starting point to generate a parser if relying on apex-jorje-lsp.jar is not an option from a license point of view.

      Atom and TextMate Grammars

      Available here: https://github.com/forcedotcom/apex-tmLanguage
      Is there a way to generate a Java parser from these grammars? I doubt about it, to be checked if the previous options are not retained.

      Conclusion

      Following MMF-1481, apex-jorje-lsp.jar is matching our needs, we can use it to make SonarApex.

      Duplication and Syntax Highlighting

      Detection of duplications and syntax highlighting should work out of the box on the Slang AST. We just need to validate that it's working fine and handle at best the corner cases.

      Test Coverage

      Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

      Tests are executed against a SalesForce organization. There is a way to export the coverage data into a JSON file thanks to https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_apex.htm#cli_reference_test_run

      Users interested to see coverage data in SonarQube would have to use a SalesForce API to extract coverage data or use "apex:test:run" and then create a file compliant with the SonarQube Test Data format.

      TBD: can we natively support the output of ""apex:test:run"?

      SonarLint for VS Code

      As SalesForce IDE is VS Code (https://github.com/forcedotcom/salesforcedx-vscode), SonarAPEX should be out of the box compatible with SonarLint for VS Code.

      External Analyzers

      PMD is supporting Apex and provides 35+ rules. SonarApex should allow to load issues generated by PMD-APEX.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alexandre.gigleux Alexandre Gigleux
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                3 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: