Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-379 Lazily perform full file traversal if necessary #4887

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -43,7 +43,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