convert most lang mods to new base class

This commit is contained in:
Clément Fournier
2022-07-21 02:10:21 +02:00
parent ec081ed2e5
commit 3cecfaf38b
21 changed files with 271 additions and 214 deletions

View File

@ -4,23 +4,22 @@
package net.sourceforge.pmd.lang.ecmascript;
import net.sourceforge.pmd.lang.BaseLanguageModule;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.ecmascript.internal.EcmascriptProcessor;
import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptParser;
import net.sourceforge.pmd.lang.impl.SimpleLanguageModuleBase;
/**
* Created by christoferdutz on 20.09.14.
*/
public class EcmascriptLanguageModule extends BaseLanguageModule {
public class EcmascriptLanguageModule extends SimpleLanguageModuleBase {
public static final String NAME = "Ecmascript";
public static final String TERSE_NAME = "ecmascript";
public EcmascriptLanguageModule() {
super(NAME, null, TERSE_NAME, "js");
addDefaultVersion("ES6", new EcmascriptProcessor(new LanguagePropertyBundle(this)));
super(LanguageMetadata.withId(TERSE_NAME).name(NAME).extensions("js"),
properties -> () -> new EcmascriptParser(properties));
}
public static Language getInstance() {

View File

@ -10,30 +10,31 @@ import java.util.List;
import java.util.Map;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.ast.Comment;
import org.mozilla.javascript.ast.ErrorCollector;
import org.mozilla.javascript.ast.ParseProblem;
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
import net.sourceforge.pmd.lang.ast.AstInfo;
import net.sourceforge.pmd.lang.ast.FileAnalysisException;
import net.sourceforge.pmd.lang.ast.ParseException;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.ecmascript.internal.EcmascriptProcessor;
public final class EcmascriptParser implements net.sourceforge.pmd.lang.ast.Parser {
private final EcmascriptProcessor processor;
private final LanguagePropertyBundle properties;
public EcmascriptParser(EcmascriptProcessor processor) {
this.processor = processor;
public EcmascriptParser(LanguagePropertyBundle properties) {
this.properties = properties;
}
private AstRoot parseEcmascript(final String sourceCode, final List<ParseProblem> parseProblems) throws ParseException {
final CompilerEnvirons compilerEnvirons = new CompilerEnvirons();
compilerEnvirons.setRecordingComments(true);
compilerEnvirons.setRecordingLocalJsDocComments(true);
compilerEnvirons.setLanguageVersion(processor.getRhinoVersion());
compilerEnvirons.setLanguageVersion(Context.VERSION_ES6);
// Scope's don't appear to get set right without this
compilerEnvirons.setIdeMode(true);
compilerEnvirons.setWarnTrailingComma(true);
@ -58,7 +59,7 @@ public final class EcmascriptParser implements net.sourceforge.pmd.lang.ast.Pars
final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(parseProblems);
ASTAstRoot tree = (ASTAstRoot) treeBuilder.build(astRoot);
String suppressMarker = processor.getProperties().getSuppressMarker();
String suppressMarker = properties.getSuppressMarker();
Map<Integer, String> suppressMap = new HashMap<>();
if (astRoot.getComments() != null) {
for (Comment comment : astRoot.getComments()) {

View File

@ -1,35 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ecmascript.internal;
import org.mozilla.javascript.Context;
import net.sourceforge.pmd.processor.BatchLanguageProcessor;
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptParser;
public class EcmascriptProcessor extends BatchLanguageProcessor<LanguagePropertyBundle>
implements LanguageVersionHandler {
public EcmascriptProcessor(LanguagePropertyBundle properties) {
super(properties);
}
public int getRhinoVersion() {
return Context.VERSION_ES6;
}
@Override
public Parser getParser() {
return new EcmascriptParser(this);
}
@Override
public LanguageVersionHandler services() {
return this;
}
}