SonarJS exposes entry point to the parser for testing

Description

SonarJS should expose a new entry point to allow other Sonar plugins easily invoke the JS/TS parser for test purposes. The interface should provide a parse function, which takes code parameter and returns the AST in the form of the API defined in https://sonarsource.atlassian.net/browse/JS-158

 

The entry point should be part of the bridge module, or new standalone module. It will be available as a test scope dependency to other Sonar plugins. The suggested interface should be similar to the following

public class Parser implements AutoCloseable { private final BridgeServerImpl bridge; public Parser() { // init bridge and keep the instance } public AST parse(String code) { // parse the code, idempotent } @Override public void close() throws Exception { // shutdown the bridge } }

It’s important to allow the API client to invoke the parse method repeatedly on the already initialized bridge instance so they can instantiate the bridge once per test suite and calls to parse are very fast.

 

There is a prototype in https://github.com/SonarSource/SonarJS/blob/armor_parser_poc/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/Parser.java#L44

Currently BridgeServerImpl depends too much on Sonar API, in particular on SensorContext . We should remove the dependencies on the Sonar API by extracting all needed values to some new BridgeParameters wrapper object. This object will be created in JsTsSensor using data from SonarAPI and passed to BridgeServerImpl methods as parameter (ie. startServerLazily will take BridgeParameters object instead of SensorContext ). This design will allow Parser to directly instantiate BridgeParameters with suitable values without the need to mock whole Sonar API

Activity

Show:

Ilia Kebets June 12, 2024 at 1:05 PM

my notes from discussion with Tibor:

  • bridgeServerImpl needs a few props from SensorContext

    • clean way: refactor the bridge so that it doesnt take the whole context, but only the fields that are necessary

    • in bridgeServerImpl have a createBridgeConfig(context) that retrieves only the necessary props from context

      • we shouldn't have any sonar.api dependencies in bridgeServerImpl

      • in JSSensor, we extract only the bridge parameters

Done
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Sprint

Fix versions

Priority

Sentry

Created May 16, 2024 at 2:39 PM
Updated October 16, 2024 at 12:53 PM
Resolved June 17, 2024 at 6:25 AM