Wire in existing langs

This commit is contained in:
Clément Fournier
2022-07-20 18:18:40 +02:00
parent 517fef6f83
commit 10ff8f6f0e
34 changed files with 483 additions and 216 deletions

View File

@ -1,24 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ecmascript;
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptParser;
class EcmascriptHandler extends AbstractPmdLanguageVersionHandler {
private final int rhinoVersion;
EcmascriptHandler(int rhinoVersion) {
this.rhinoVersion = rhinoVersion;
}
@Override
public Parser getParser() {
return new EcmascriptParser(rhinoVersion);
}
}

View File

@ -4,9 +4,11 @@
package net.sourceforge.pmd.lang.ecmascript;
import org.mozilla.javascript.Context;
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;
/**
* Created by christoferdutz on 20.09.14.
@ -18,6 +20,10 @@ public class EcmascriptLanguageModule extends BaseLanguageModule {
public EcmascriptLanguageModule() {
super(NAME, null, TERSE_NAME, "js");
addDefaultVersion("ES6", new EcmascriptHandler(Context.VERSION_ES6));
addDefaultVersion("ES6", new EcmascriptProcessor(new LanguagePropertyBundle(this)));
}
public static Language getInstance() {
return LanguageRegistry.PMD.getLanguageByFullName(NAME);
}
}

View File

@ -20,19 +20,20 @@ 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 int esVersion;
private final EcmascriptProcessor processor;
public EcmascriptParser(int version) {
this.esVersion = version;
public EcmascriptParser(EcmascriptProcessor processor) {
this.processor = processor;
}
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(esVersion);
compilerEnvirons.setLanguageVersion(processor.getRhinoVersion());
// Scope's don't appear to get set right without this
compilerEnvirons.setIdeMode(true);
compilerEnvirons.setWarnTrailingComma(true);
@ -57,7 +58,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 = task.getCommentMarker();
String suppressMarker = processor.getProperties().getSuppressMarker();
Map<Integer, String> suppressMap = new HashMap<>();
if (astRoot.getComments() != null) {
for (Comment comment : astRoot.getComments()) {

View File

@ -0,0 +1,37 @@
/*
* 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.lang.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.EcmascriptLanguageModule;
import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptParser;
public class EcmascriptProcessor extends BatchLanguageProcessor<LanguagePropertyBundle>
implements LanguageVersionHandler {
public EcmascriptProcessor(LanguagePropertyBundle properties) {
super(EcmascriptLanguageModule.getInstance(), properties);
}
public int getRhinoVersion() {
return Context.VERSION_ES6;
}
@Override
public LanguagePropertyBundle getProperties() {
return super.getProperties();
}
@Override
public Parser getParser() {
return new EcmascriptParser(this);
}
}