Skip to content

Commit

Permalink
Only perform full file traversal if no tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
zglicz committed Oct 29, 2024
1 parent b9cbf21 commit 9e0b141
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ protected void analyzeFiles(List<InputFile> inputFiles) throws IOException {
exclusions
);

SonarLintTypeCheckingChecker.checkOnce(javaScriptProjectChecker, context);
var tsConfigs = getTsConfigs(
contextUtils,
javaScriptProjectChecker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,20 @@ private static String getProjectRoot(SensorContext context) {
new ConcurrentHashMap<>();

final TsConfigFileCreator tsConfigFileCreator;
private final boolean deactivated;
SonarLintTypeCheckingChecker checker;

WildcardTsConfigProvider(
@Nullable SonarLintTypeCheckingChecker checker,
TsConfigFileCreator tsConfigFileCreator
) {
super(SonarProduct.SONARLINT);
this.tsConfigFileCreator = tsConfigFileCreator;
deactivated = checker == null || checker.isBeyondLimit();
this.checker = checker;
}

@Override
List<String> getDefaultTsConfigs(SensorContext context) {
boolean deactivated = checker == null || checker.isBeyondLimit(context);
if (deactivated) {
return emptyList();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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();
}

Expand All @@ -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(
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

Expand All @@ -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)",
Expand All @@ -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");
}
Expand Down

0 comments on commit 9e0b141

Please sign in to comment.