Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- [#84](https://github.com/green-code-initiative/creedengo-javascript/pull/84) Add rule GCI535 "No imported number format library"
- [#118](https://github.com/green-code-initiative/creedengo-javascript/pull/118) Add rule GCI2536 "Move the browserslist configuration to a production target in package.json"

## [3.1.0] - 2026-05-10

Expand Down
2 changes: 1 addition & 1 deletion sonar-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>

<version.creedengo-rules-specifications>3.0.0</version.creedengo-rules-specifications>
<version.creedengo-rules-specifications>2536-JS-SNAPSHOT</version.creedengo-rules-specifications>
<version.sonarqube>13.0.0.3026</version.sonarqube>
<version.sonar-javascript>11.8.0.37897</version.sonar-javascript>
<version.sonar-packaging>1.25.1.3002</version.sonar-packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.greencodeinitiative.creedengo.javascript;

import org.greencodeinitiative.creedengo.javascript.checks.*;
import org.greencodeinitiative.creedengo.javascript.checks.PackageJsonCheck;
import org.sonar.plugins.javascript.api.EslintHook;
import org.sonar.plugins.javascript.api.JavaScriptRule;
import org.sonar.plugins.javascript.api.TypeScriptRule;
Expand All @@ -26,6 +27,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Collections;

public class CheckList {

Expand Down Expand Up @@ -67,4 +69,12 @@ private static List<Class<? extends EslintHook>> filterHooksByAnnotation(Class<?
.collect(Collectors.toList());
}

public static List<PackageJsonCheck> getPackageJsonChecks() {
return Collections.singletonList(new OptimizeBrowserslistTagInPackageJsonRule());
}

public static List<Class<?>> getSensorChecks() {
return Collections.singletonList(OptimizeBrowserslistTagInPackageJsonRule.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.greencodeinitiative.creedengo.javascript;

import org.greencodeinitiative.creedengo.javascript.checks.PackageJsonSensor;
import org.sonar.api.Plugin;

public class JavaScriptPlugin implements Plugin {
Expand All @@ -30,7 +31,8 @@ public void define(Context context) {
JavaScriptRulesDefinition.class,
JavaScriptRuleRepository.class,
TypeScriptRulesDefinition.class,
TypeScriptRuleRepository.class
TypeScriptRuleRepository.class,
PackageJsonSensor.class
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public void define(Context context) {
ruleMetadataLoader.addRulesByAnnotatedClass(repository, checks);
DeprecatedEcoCodeRule.addOnRepository(repository, JavaScriptRuleRepository.OLD_KEY, checks);

ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getSensorChecks());

repository.done();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public void define(Context context) {
ruleMetadataLoader.addRulesByAnnotatedClass(repository, checks);
DeprecatedEcoCodeRule.addOnRepository(repository, TypeScriptRuleRepository.OLD_KEY, checks);

ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getSensorChecks());

repository.done();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.javascript.checks;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.greencodeinitiative.creedengo.javascript.JavaScriptRuleRepository;
import org.greencodeinitiative.creedengo.javascript.TypeScriptRuleRepository;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.rule.RuleKey;
import org.sonar.check.Rule;

@Rule(key = OptimizeBrowserslistTagInPackageJsonRule.KEY)
public final class OptimizeBrowserslistTagInPackageJsonRule implements PackageJsonCheck {

public static final String KEY = "GCI2536";

public static final String ISSUE_MESSAGE = "Move the browserslist configuration to a \"production\" target.";

private static final Pattern BROWSERSLIST_PATTERN = Pattern.compile(
"\"browserslist\"\\s*:\\s*(\\{.*?\\}|\\[.*?\\]|\".*?\")", Pattern.DOTALL);

@Override
public void analyze(SensorContext context, InputFile inputFile, String contents) {
RuleKey jsRuleKey = RuleKey.of(JavaScriptRuleRepository.KEY, KEY);
RuleKey tsRuleKey = RuleKey.of(TypeScriptRuleRepository.KEY, KEY);

RuleKey activeRuleKey = null;
if (context.activeRules().find(jsRuleKey) != null) {
activeRuleKey = jsRuleKey;
} else if (context.activeRules().find(tsRuleKey) != null) {
activeRuleKey = tsRuleKey;
}

if (activeRuleKey == null || !isNonCompliant(contents)) {
return;
}

int line = browserslistLineNumber(contents);
NewIssue issue = context.newIssue().forRule(activeRuleKey);
issue.at(
issue.newLocation()
.on(inputFile)
.at(inputFile.selectLine(line))
.message(ISSUE_MESSAGE)
).save();
}

public static boolean isNonCompliant(String packageJsonContents) {
Matcher matcher = BROWSERSLIST_PATTERN.matcher(packageJsonContents);
if (!matcher.find()) {
return false;
}

String browserslistConfiguration = matcher.group(1).trim();
if (browserslistConfiguration.startsWith("{")) {
return !browserslistConfiguration.contains("\"production\"");
}

return true;
}

public static int browserslistLineNumber(String packageJsonContents) {
Matcher matcher = BROWSERSLIST_PATTERN.matcher(packageJsonContents);
if (!matcher.find()) {
return 1;
}

return lineNumberAt(packageJsonContents, matcher.start());
}

private static int lineNumberAt(String text, int index) {
return 1 + (int) text.substring(0, index).chars().filter(c -> c == '\n').count();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.javascript.checks;

import java.io.IOException;

import org.greencodeinitiative.creedengo.javascript.JavaScriptRuleRepository;
import org.greencodeinitiative.creedengo.javascript.TypeScriptRuleRepository;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.rule.RuleKey;

public class OptimizeBrowserslistTagInPackageJsonSensor implements Sensor {

@Override
public void describe(SensorDescriptor descriptor) {
descriptor
.name("Creedengo - Optimize browserslist tag in package.json")
.createIssuesForRuleRepository(JavaScriptRuleRepository.KEY, TypeScriptRuleRepository.KEY);
}

@Override
public void execute(SensorContext context) {
RuleKey jsRuleKey = RuleKey.of(JavaScriptRuleRepository.KEY, OptimizeBrowserslistTagInPackageJsonRule.KEY);
RuleKey tsRuleKey = RuleKey.of(TypeScriptRuleRepository.KEY, OptimizeBrowserslistTagInPackageJsonRule.KEY);

RuleKey activeRuleKey = null;
if (context.activeRules().find(jsRuleKey) != null) {
activeRuleKey = jsRuleKey;
} else if (context.activeRules().find(tsRuleKey) != null) {
activeRuleKey = tsRuleKey;
}

if (activeRuleKey == null) {
return;
}

FileSystem fs = context.fileSystem();
FilePredicate predicate = fs.predicates().matchesPathPattern("**/package.json");
for (InputFile inputFile : fs.inputFiles(predicate)) {
analyzeFile(context, inputFile, activeRuleKey);
}
}

private void analyzeFile(SensorContext context, InputFile inputFile, RuleKey ruleKey) {
try {
String contents = inputFile.contents();
if (OptimizeBrowserslistTagInPackageJsonRule.isNonCompliant(contents)) {
int line = OptimizeBrowserslistTagInPackageJsonRule.browserslistLineNumber(contents);
NewIssue issue = context.newIssue().forRule(ruleKey);
issue.at(
issue.newLocation()
.on(inputFile)
.at(inputFile.selectLine(line))
.message(OptimizeBrowserslistTagInPackageJsonRule.ISSUE_MESSAGE)
).save();
}
} catch (IOException e) {
// skip unreadable file
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.javascript.checks;

import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;

public interface PackageJsonCheck {

void analyze(SensorContext context, InputFile inputFile, String contents);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.javascript.checks;

import java.io.IOException;

import org.greencodeinitiative.creedengo.javascript.CheckList;
import org.greencodeinitiative.creedengo.javascript.JavaScriptRuleRepository;
import org.greencodeinitiative.creedengo.javascript.TypeScriptRuleRepository;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;

public class PackageJsonSensor implements Sensor {

@Override
public void describe(SensorDescriptor descriptor) {
descriptor
.name("Creedengo - package.json checks")
.createIssuesForRuleRepository(JavaScriptRuleRepository.KEY, TypeScriptRuleRepository.KEY);
}

@Override
public void execute(SensorContext context) {
FileSystem fs = context.fileSystem();
FilePredicate predicate = fs.predicates().matchesPathPattern("**/package.json");
for (InputFile inputFile : fs.inputFiles(predicate)) {
try {
String contents = inputFile.contents();
for (PackageJsonCheck check : CheckList.getPackageJsonChecks()) {
check.analyze(context, inputFile, contents);
}
} catch (IOException e) {
// skip unreadable file
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"GCI505",
"GCI523",
"GCI530",
"GCI535"
"GCI535",
"GCI2536"
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "Creedengo",
"ruleKeys": ["GCI13"]
"ruleKeys": ["GCI13", "GCI2536"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void extensions() {
SonarRuntime sonarRuntime = mock(SonarRuntime.class);
Plugin.Context context = new Plugin.Context(sonarRuntime);
new JavaScriptPlugin().define(context);
assertThat(context.getExtensions()).hasSize(5);
assertThat(context.getExtensions()).hasSize(6);
}

}
Loading
Loading