From 9e0b14119a5bf3fa019a5eb14a2525bf9eab35df Mon Sep 17 00:00:00 2001 From: Michal Zgliczynski Date: Tue, 29 Oct 2024 17:25:16 +0100 Subject: [PATCH] Only perform full file traversal if no tsconfig --- .../sonar/plugins/javascript/analysis/JsTsSensor.java | 1 - .../plugins/javascript/analysis/TsConfigProvider.java | 5 +++-- .../sonarlint/SonarLintTypeCheckingChecker.java | 8 +------- .../sonarlint/SonarLintTypeCheckingCheckerImpl.java | 7 ++++++- .../javascript/analysis/TsConfigProviderTest.java | 10 +++++----- .../sonarlint/SonarLintTypeCheckingCheckerTest.java | 7 ++++--- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/JsTsSensor.java b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/JsTsSensor.java index 5105e9737a2..afd7bd0ec30 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/JsTsSensor.java +++ b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/JsTsSensor.java @@ -113,7 +113,6 @@ protected void analyzeFiles(List inputFiles) throws IOException { exclusions ); - SonarLintTypeCheckingChecker.checkOnce(javaScriptProjectChecker, context); var tsConfigs = getTsConfigs( contextUtils, javaScriptProjectChecker, diff --git a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/TsConfigProvider.java b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/TsConfigProvider.java index 1f8739a0be0..e2709c0e4d7 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/TsConfigProvider.java +++ b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/TsConfigProvider.java @@ -317,7 +317,7 @@ private static String getProjectRoot(SensorContext context) { new ConcurrentHashMap<>(); final TsConfigFileCreator tsConfigFileCreator; - private final boolean deactivated; + SonarLintTypeCheckingChecker checker; WildcardTsConfigProvider( @Nullable SonarLintTypeCheckingChecker checker, @@ -325,11 +325,12 @@ private static String getProjectRoot(SensorContext context) { ) { super(SonarProduct.SONARLINT); this.tsConfigFileCreator = tsConfigFileCreator; - deactivated = checker == null || checker.isBeyondLimit(); + this.checker = checker; } @Override List getDefaultTsConfigs(SensorContext context) { + boolean deactivated = checker == null || checker.isBeyondLimit(context); if (deactivated) { return emptyList(); } else { diff --git a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingChecker.java b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingChecker.java index 2e32676b29c..bf1d120fb36 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingChecker.java +++ b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingChecker.java @@ -23,12 +23,6 @@ import org.sonar.api.batch.sensor.SensorContext; public interface SonarLintTypeCheckingChecker { - static void checkOnce(@Nullable SonarLintTypeCheckingChecker checker, SensorContext context) { - if (checker != null) { - checker.checkOnce(context); - } - } - void checkOnce(SensorContext context); - boolean isBeyondLimit(); + boolean isBeyondLimit(SensorContext context); } diff --git a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerImpl.java b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerImpl.java index 62ff6a71b2d..28e7bc1b834 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerImpl.java +++ b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerImpl.java @@ -28,6 +28,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.plugins.javascript.analysis.TsConfigCache; import org.sonar.plugins.javascript.utils.PathWalker; import org.sonarsource.api.sonarlint.SonarLintSide; @@ -43,7 +44,11 @@ public class SonarLintTypeCheckingCheckerImpl implements SonarLintTypeCheckingCh private boolean shouldCheck = true; - public boolean isBeyondLimit() { + public boolean isBeyondLimit(SensorContext context) { + if (shouldCheck) { + checkLimit(context); + shouldCheck = false; + } return beyondLimit; } diff --git a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/TsConfigProviderTest.java b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/TsConfigProviderTest.java index 91422122e3e..428aeb8f018 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/TsConfigProviderTest.java +++ b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/TsConfigProviderTest.java @@ -271,7 +271,7 @@ void should_create_wildcard_tsconfig() throws Exception { createInputFile(ctx, "file2.js"); var checker = mock(SonarLintTypeCheckingChecker.class); - when(checker.isBeyondLimit()).thenReturn(false); + when(checker.isBeyondLimit(ctx)).thenReturn(false); var provider = new WildcardTsConfigProvider( checker, @@ -288,7 +288,7 @@ void should_create_wildcard_tsconfig() throws Exception { ) ); - when(checker.isBeyondLimit()).thenReturn(true); + when(checker.isBeyondLimit(ctx)).thenReturn(true); provider = new WildcardTsConfigProvider( checker, @@ -297,7 +297,7 @@ void should_create_wildcard_tsconfig() throws Exception { assertThat(provider.tsconfigs(ctx)).isEmpty(); provider = - new WildcardTsConfigProvider(null, TsConfigProviderTest::createTsConfigFile); + new WildcardTsConfigProvider(checker, TsConfigProviderTest::createTsConfigFile); assertThat(provider.tsconfigs(ctx)).isEmpty(); } @@ -310,7 +310,7 @@ void should_not_recreate_wildcart_tsconfig_in_sonarlint() throws Exception { ctx.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(4, 4))); var checker = mock(SonarLintTypeCheckingChecker.class); - when(checker.isBeyondLimit()).thenReturn(false); + when(checker.isBeyondLimit(ctx)).thenReturn(false); tsconfigs = new WildcardTsConfigProvider( @@ -331,7 +331,7 @@ void should_not_fail_on_exception() throws Exception { createInputFile(ctx, "file.js"); var checker = mock(SonarLintTypeCheckingChecker.class); - when(checker.isBeyondLimit()).thenReturn(false); + when(checker.isBeyondLimit(ctx)).thenReturn(false); var fileWriter = mock(TsConfigFileCreator.class); when(fileWriter.createTsConfigFile(anyString())).thenThrow(IOException.class); diff --git a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerTest.java b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerTest.java index d7e477afd71..9d7721e2992 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerTest.java +++ b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/sonarlint/SonarLintTypeCheckingCheckerTest.java @@ -34,6 +34,7 @@ import org.junit.jupiter.api.io.TempDir; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.config.Configuration; import org.sonar.api.testfixtures.log.LogTesterJUnit5; import org.sonar.api.utils.log.LoggerLevel; @@ -55,7 +56,7 @@ void should_check_javascript_files() throws IOException { inputFile("node_modules", "dep.js"); var checker = sonarLintTypeCheckingChecker(2); - assertThat(checker.isBeyondLimit()).isFalse(); + assertThat(checker.isBeyondLimit(SensorContextTester.create(baseDir))).isFalse(); assertThat(logTester.logs()).contains("Turning on type-checking of JavaScript files"); } @@ -68,7 +69,7 @@ void should_detect_projects_with_too_many_files() throws IOException { inputFile("file4.cts"); var checker = sonarLintTypeCheckingChecker(3); - assertThat(checker.isBeyondLimit()).isTrue(); + assertThat(checker.isBeyondLimit(SensorContextTester.create(baseDir))).isTrue(); assertThat(logTester.logs()) .contains( "Turning off type-checking of JavaScript files due to the project size exceeding the limit (3 files)", @@ -84,7 +85,7 @@ void should_detect_errors() { logTester.setLevel(LoggerLevel.WARN); var checker = sonarLintTypeCheckingChecker(new IllegalArgumentException()); - assertThat(checker.isBeyondLimit()).isTrue(); + assertThat(checker.isBeyondLimit(SensorContextTester.create(baseDir))).isTrue(); assertThat(logTester.logs()) .containsExactly("Turning off type-checking of JavaScript files due to unexpected error"); }