add support for type-only import specifiers

This commit is contained in:
Erik Krogh Kristensen
2021-11-23 12:48:55 +01:00
parent 57399b733e
commit d946802057
9 changed files with 57 additions and 3 deletions

View File

@@ -10,15 +10,25 @@ package com.semmle.js.ast;
*/
public class ImportSpecifier extends Expression {
private final Identifier imported, local;
private final boolean isTypeOnly;
public ImportSpecifier(SourceLocation loc, Identifier imported, Identifier local) {
this("ImportSpecifier", loc, imported, local);
this(loc, imported, local, false);
}
public ImportSpecifier(SourceLocation loc, Identifier imported, Identifier local, boolean isTypeOnly) {
this("ImportSpecifier", loc, imported, local, isTypeOnly);
}
public ImportSpecifier(String type, SourceLocation loc, Identifier imported, Identifier local) {
this(type, loc, imported, local, false);
}
private ImportSpecifier(String type, SourceLocation loc, Identifier imported, Identifier local, boolean isTypeOnly) {
super(type, loc);
this.imported = imported;
this.local = local == imported ? new NodeCopier().copy(local) : local;
this.isTypeOnly = isTypeOnly;
}
public Identifier getImported() {
@@ -33,4 +43,8 @@ public class ImportSpecifier extends Expression {
public <C, R> R accept(Visitor<C, R> v, C c) {
return v.visit(this, c);
}
public boolean hasTypeKeyword() {
return isTypeOnly;
}
}

View File

@@ -1806,6 +1806,9 @@ public class ASTExtractor {
Label lbl = super.visit(nd, c);
visit(nd.getImported(), lbl, 0, IdContext.LABEL);
visit(nd.getLocal(), lbl, 1, c.idcontext);
if (nd.hasTypeKeyword()) {
trapwriter.addTuple("has_type_keyword", lbl);
}
return lbl;
}