Fix Version/s: None
See the WHY section in
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:
- features: does it provide precise location of tokens? comments?
- ease of integration
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.
According to https://github.com/pmd/pmd/issues/677, Apex Code Editor for Visual Studio Code:
- uses Jorje underneath. The version of Jorje that is packaged with Force.com IDE will not be updated. But, the one for VS Code is constantly updated at https://github.com/forcedotcom/salesforcedx-vscode/tree/develop/packages/salesforcedx-vscode-apex/out. This is because, moving forward, our flagship IDE is shifting from Eclipse to VS Code.
- The jar at https://github.com/forcedotcom/salesforcedx-vscode/tree/develop/packages/salesforcedx-vscode-apex/out is a über jar with all the dependencies packaged.
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 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.
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.
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.
MMF-1481, apex-jorje-lsp.jar is matching our needs, we can use it to make SonarApex.
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.
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"?
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.
PMD is supporting Apex and provides 35+ rules. SonarApex should allow to load issues generated by PMD-APEX.