Skip to content

Commit 872478d

Browse files
authored
JS-539 Revert "Clean-up obsolete config flags for plugins using Java AST API" (#5088)
1 parent b8208d0 commit 872478d

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/AbstractAnalysis.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,17 @@ protected void analyzeFile(
109109
LOG.debug("Analyzing file: {}", file.uri());
110110
progressReport.nextFile(file.toString());
111111
var fileContent = contextUtils.shouldSendFileContent(file) ? file.contents() : null;
112+
var skipAst =
113+
!consumers.hasConsumers() ||
114+
!(contextUtils.isSonarArmorEnabled() ||
115+
contextUtils.isSonarJasminEnabled() ||
116+
contextUtils.isSonarJaredEnabled());
112117
var request = getJsAnalysisRequest(
113118
file,
114119
fileContent,
115120
tsProgram,
116121
tsConfigs,
117-
shouldSkipAstProduction(),
122+
skipAst,
118123
dirtyPackageJSONCache
119124
);
120125

@@ -139,11 +144,6 @@ protected void analyzeFile(
139144
}
140145
}
141146

142-
private boolean shouldSkipAstProduction() {
143-
// we don't need to produce AST if there are no consumers
144-
return !consumers.hasConsumers();
145-
}
146-
147147
private void acceptAstResponse(BridgeServer.AnalysisResponse response, InputFile file) {
148148
Node responseAst = response.ast();
149149
if (responseAst != null) {

sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/ContextUtils.java

+30
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424

2525
public class ContextUtils {
2626

27+
/**
28+
* Internal property to enable SonarArmor (disabled by default), now called Jasmin
29+
* @deprecated Kept for backwards compatibility until SonarArmor has been renamed to Jasmin, to allow for a smooth transition. Roadmap:
30+
* 1. Merge this
31+
* 2. Rename SonarArmor to Jasmin on SonarArmor repository, this includes renaming the flag
32+
* 3. Once Jasmin renaming is on master, change the flags in the Peachee jobs
33+
* 4. Finally, remove this flag here
34+
*/
35+
@Deprecated(forRemoval = true)
36+
private static final String ARMOR_INTERNAL_ENABLED = "sonar.armor.internal.enabled";
37+
38+
/* Internal property to enable Jasmin (disabled by default) */
39+
private static final String JASMIN_INTERNAL_ENABLED = "sonar.jasmin.internal.enabled";
40+
41+
/* Internal property to enable JaRED (disabled by default) */
42+
private static final String JARED_INTERNAL_ENABLED = "sonar.jared.internal.enabled";
43+
2744
private final SensorContext context;
2845

2946
ContextUtils(SensorContext context) {
@@ -56,4 +73,17 @@ boolean failFast() {
5673
SensorContext context() {
5774
return context;
5875
}
76+
77+
@Deprecated(forRemoval = true)
78+
boolean isSonarArmorEnabled() {
79+
return context.config().getBoolean(ARMOR_INTERNAL_ENABLED).orElse(false);
80+
}
81+
82+
boolean isSonarJasminEnabled() {
83+
return context.config().getBoolean(JASMIN_INTERNAL_ENABLED).orElse(false);
84+
}
85+
86+
boolean isSonarJaredEnabled() {
87+
return context.config().getBoolean(JARED_INTERNAL_ENABLED).orElse(false);
88+
}
5989
}

sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/JsTsSensorTest.java

+60
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,66 @@ void should_not_send_the_skipAst_flag_when_there_are_consumers() throws Exceptio
409409
assertThat(captor.getValue().skipAst()).isFalse();
410410
}
411411

412+
@Test
413+
void should_not_send_the_skipAst_flag_when_jared_is_enabled() throws Exception {
414+
var ctx = createSensorContext(baseDir);
415+
ctx.setSettings(new MapSettings().setProperty("sonar.jared.internal.enabled", "true"));
416+
var inputFile = createInputFile(ctx);
417+
var tsProgram = new TsProgram("1", List.of(inputFile.absolutePath()), List.of());
418+
var consumer = createConsumer();
419+
var sensor = createSensorWithConsumer(consumer);
420+
when(bridgeServerMock.createProgram(any())).thenReturn(tsProgram);
421+
when(bridgeServerMock.analyzeTypeScript(any())).thenReturn(new AnalysisResponse());
422+
sensor.execute(ctx);
423+
424+
createSensor().execute(context);
425+
var captor = ArgumentCaptor.forClass(JsAnalysisRequest.class);
426+
verify(bridgeServerMock).analyzeTypeScript(captor.capture());
427+
assertThat(captor.getValue().skipAst()).isFalse();
428+
}
429+
430+
/**
431+
* @deprecated Should be removed when the sonar.armor.internal.enabled is removed, see comments in ContextUtils#ARMOR_INTERNAL_ENABLED
432+
*/
433+
@Deprecated(forRemoval = true)
434+
@Test
435+
void should_send_the_skipAst_flag_when_there_are_consumers_but_armor_is_disabled()
436+
throws Exception {
437+
var ctx = createSensorContext(baseDir);
438+
ctx.setSettings(new MapSettings().setProperty("sonar.armor.internal.enabled", "false"));
439+
var inputFile = createInputFile(ctx);
440+
var tsProgram = new TsProgram("1", List.of(inputFile.absolutePath()), List.of());
441+
var consumer = createConsumer();
442+
var sensor = createSensorWithConsumer(consumer);
443+
when(bridgeServerMock.createProgram(any())).thenReturn(tsProgram);
444+
when(bridgeServerMock.analyzeTypeScript(any())).thenReturn(new AnalysisResponse());
445+
sensor.execute(ctx);
446+
447+
createSensor().execute(context);
448+
var captor = ArgumentCaptor.forClass(JsAnalysisRequest.class);
449+
verify(bridgeServerMock).analyzeTypeScript(captor.capture());
450+
assertThat(captor.getValue().skipAst()).isTrue();
451+
}
452+
453+
@Test
454+
void should_send_the_skipAst_flag_when_there_are_consumers_but_jasmin_is_disabled()
455+
throws Exception {
456+
var ctx = createSensorContext(baseDir);
457+
ctx.setSettings(new MapSettings().setProperty("sonar.jasmin.internal.enabled", "false"));
458+
var inputFile = createInputFile(ctx);
459+
var tsProgram = new TsProgram("1", List.of(inputFile.absolutePath()), List.of());
460+
var consumer = createConsumer();
461+
var sensor = createSensorWithConsumer(consumer);
462+
when(bridgeServerMock.createProgram(any())).thenReturn(tsProgram);
463+
when(bridgeServerMock.analyzeTypeScript(any())).thenReturn(new AnalysisResponse());
464+
sensor.execute(ctx);
465+
466+
createSensor().execute(context);
467+
var captor = ArgumentCaptor.forClass(JsAnalysisRequest.class);
468+
verify(bridgeServerMock).analyzeTypeScript(captor.capture());
469+
assertThat(captor.getValue().skipAst()).isTrue();
470+
}
471+
412472
@Test
413473
void should_send_content_when_not_utf8() throws Exception {
414474
var ctx = createSensorContext(baseDir);

0 commit comments

Comments
 (0)