From 0e5e923960c342da0ed5d59717b280102024b970 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Fri, 5 Apr 2024 18:58:17 +0200 Subject: [PATCH] Ignore runtime errors when saving CPD tokens (#4654) --- .../javascript/bridge/AnalysisProcessor.java | 8 +++++++- .../bridge/AnalysisProcessorTest.java | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java index d12d77feca5..2a15f8b6f70 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java +++ b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java @@ -281,7 +281,13 @@ private void saveCpd(BridgeServer.CpdToken[] cpdTokens) { } NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); for (BridgeServer.CpdToken cpdToken : cpdTokens) { - newCpdTokens.addToken(cpdToken.location.toTextRange(file), cpdToken.image); + try { + newCpdTokens.addToken(cpdToken.location.toTextRange(file), cpdToken.image); + } catch (IllegalArgumentException e) { + LOG.warn("Failed to save CPD token in {} at {}", file.uri(), cpdToken.location); + LOG.warn("Exception cause", e); + // continue processing other tokens + } } newCpdTokens.save(); } diff --git a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java index 0ff8843673f..8e0782256ef 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java +++ b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java @@ -67,4 +67,23 @@ void should_not_fail_when_invalid_symbol() { assertThat(logTester.logs()) .contains("Failed to create symbol reference in " + file.uri() + " at 2:2-2:1"); } + + @Test + void should_not_fail_when_invalid_cpd() { + var fileLinesContextFactory = mock(FileLinesContextFactory.class); + when(fileLinesContextFactory.createFor(any())).thenReturn(mock(FileLinesContext.class)); + var processor = new AnalysisProcessor(mock(NoSonarFilter.class), fileLinesContextFactory); + var context = SensorContextTester.create(baseDir); + var file = TestInputFileBuilder + .create("moduleKey", "file.js") + .setContents("var x = 1;") + .build(); + var response = new BridgeServer.AnalysisResponse(); + var cpd = new BridgeServer.CpdToken(); + cpd.location = new BridgeServer.Location(1, 2, 1, 1); // invalid range startCol > endCol + response.cpdTokens = new BridgeServer.CpdToken[] { cpd }; + processor.processResponse(context, mock(JsTsChecks.class), file, response); + assertThat(logTester.logs()) + .contains("Failed to save CPD token in " + file.uri() + " at 1:2-1:1"); + } }