mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
JS: Preserve information about 'defer' keyword
This commit is contained in:
@@ -267,6 +267,7 @@ const astProperties: string[] = [
|
||||
"parameterName",
|
||||
"parameters",
|
||||
"parseDiagnostics",
|
||||
"phaseModifier",
|
||||
"properties",
|
||||
"propertyName",
|
||||
"qualifier",
|
||||
|
||||
@@ -26,11 +26,11 @@ public class ImportDeclaration extends Statement implements INodeWithSymbol {
|
||||
|
||||
private int symbol = -1;
|
||||
|
||||
private boolean hasTypeKeyword;
|
||||
private ImportPhaseModifier phaseModifier;
|
||||
|
||||
public ImportDeclaration(
|
||||
SourceLocation loc, List<ImportSpecifier> specifiers, Literal source, Expression attributes) {
|
||||
this(loc, specifiers, source, attributes, false);
|
||||
this(loc, specifiers, source, attributes, ImportPhaseModifier.NONE);
|
||||
}
|
||||
|
||||
public ImportDeclaration(
|
||||
@@ -38,12 +38,12 @@ public class ImportDeclaration extends Statement implements INodeWithSymbol {
|
||||
List<ImportSpecifier> specifiers,
|
||||
Literal source,
|
||||
Expression attributes,
|
||||
boolean hasTypeKeyword) {
|
||||
ImportPhaseModifier phaseModifier) {
|
||||
super("ImportDeclaration", loc);
|
||||
this.specifiers = specifiers;
|
||||
this.source = source;
|
||||
this.attributes = attributes;
|
||||
this.hasTypeKeyword = hasTypeKeyword;
|
||||
this.phaseModifier = phaseModifier;
|
||||
}
|
||||
|
||||
public Literal getSource() {
|
||||
@@ -79,6 +79,15 @@ public class ImportDeclaration extends Statement implements INodeWithSymbol {
|
||||
|
||||
/** Returns true if this is an <code>import type</code> declaration. */
|
||||
public boolean hasTypeKeyword() {
|
||||
return hasTypeKeyword;
|
||||
return phaseModifier == ImportPhaseModifier.TYPE;
|
||||
}
|
||||
|
||||
/** Returns true if this is an <code>import defer</code> declaration. */
|
||||
public boolean hasDeferKeyword() {
|
||||
return phaseModifier == ImportPhaseModifier.DEFER;
|
||||
}
|
||||
|
||||
public ImportPhaseModifier getPhaseModifier() {
|
||||
return phaseModifier;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.semmle.js.ast;
|
||||
|
||||
/**
|
||||
* A keyword that may appear on import declarations.
|
||||
*/
|
||||
public enum ImportPhaseModifier {
|
||||
NONE,
|
||||
DEFER,
|
||||
TYPE,
|
||||
}
|
||||
@@ -564,7 +564,7 @@ public class NodeCopier implements Visitor<Void, INode> {
|
||||
copy(nd.getSpecifiers()),
|
||||
copy(nd.getSource()),
|
||||
copy(nd.getAttributes()),
|
||||
nd.hasTypeKeyword());
|
||||
nd.getPhaseModifier());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1833,6 +1833,9 @@ public class ASTExtractor {
|
||||
if (nd.hasTypeKeyword()) {
|
||||
trapwriter.addTuple("has_type_keyword", lbl);
|
||||
}
|
||||
if (nd.hasDeferKeyword()) {
|
||||
trapwriter.addTuple("has_defer_keyword", lbl);
|
||||
}
|
||||
return lbl;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ import com.semmle.js.ast.IfStatement;
|
||||
import com.semmle.js.ast.ImportDeclaration;
|
||||
import com.semmle.js.ast.ImportDefaultSpecifier;
|
||||
import com.semmle.js.ast.ImportNamespaceSpecifier;
|
||||
import com.semmle.js.ast.ImportPhaseModifier;
|
||||
import com.semmle.js.ast.ImportSpecifier;
|
||||
import com.semmle.js.ast.InvokeExpression;
|
||||
import com.semmle.js.ast.LabeledStatement;
|
||||
@@ -1404,7 +1405,7 @@ public class TypeScriptASTConverter {
|
||||
Literal src = tryConvertChild(node, "moduleSpecifier", Literal.class);
|
||||
Expression attributes = convertChild(node, "attributes");
|
||||
List<ImportSpecifier> specifiers = new ArrayList<>();
|
||||
boolean hasTypeKeyword = false;
|
||||
ImportPhaseModifier phaseModifier = ImportPhaseModifier.NONE;
|
||||
if (hasChild(node, "importClause")) {
|
||||
JsonObject importClause = node.get("importClause").getAsJsonObject();
|
||||
if (hasChild(importClause, "name")) {
|
||||
@@ -1418,10 +1419,22 @@ public class TypeScriptASTConverter {
|
||||
specifiers.addAll(convertChildren(namedBindings, "elements"));
|
||||
}
|
||||
}
|
||||
hasTypeKeyword = importClause.get("isTypeOnly").getAsBoolean();
|
||||
if (hasChild(importClause, "phaseModifier")) {
|
||||
String name = metadata.getSyntaxKindName(importClause.get("phaseModifier").getAsInt());
|
||||
switch (name) {
|
||||
case "DeferKeyword": {
|
||||
phaseModifier = ImportPhaseModifier.DEFER;
|
||||
break;
|
||||
}
|
||||
case "TypeKeyword": {
|
||||
phaseModifier = ImportPhaseModifier.TYPE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ImportDeclaration importDecl =
|
||||
new ImportDeclaration(loc, specifiers, src, attributes, hasTypeKeyword);
|
||||
new ImportDeclaration(loc, specifiers, src, attributes, phaseModifier);
|
||||
attachSymbolInformation(importDecl, node);
|
||||
return importDecl;
|
||||
}
|
||||
|
||||
@@ -151,6 +151,14 @@ class ImportDeclaration extends Stmt, Import, @import_declaration {
|
||||
/** Holds if this is declared with the `type` keyword, so it only imports types. */
|
||||
predicate isTypeOnly() { has_type_keyword(this) }
|
||||
|
||||
/**
|
||||
* Holds if this is declared with the `defer` keyword, for example:
|
||||
* ```ts
|
||||
* import defer * as f from "somewhere";
|
||||
* ```
|
||||
*/
|
||||
predicate isDeferredImport() { has_defer_keyword(this) }
|
||||
|
||||
override string getAPrimaryQlClass() { result = "ImportDeclaration" }
|
||||
}
|
||||
|
||||
|
||||
@@ -516,6 +516,7 @@ has_private_keyword (int id: @property ref);
|
||||
has_protected_keyword (int id: @property ref);
|
||||
has_readonly_keyword (int id: @property ref);
|
||||
has_type_keyword (int id: @type_keyword_operand ref);
|
||||
has_defer_keyword (int id: @import_declaration ref);
|
||||
is_optional_member (int id: @property ref);
|
||||
has_definite_assignment_assertion (int id: @field_or_vardeclarator ref);
|
||||
is_optional_parameter_declaration (unique int parameter: @pattern ref);
|
||||
|
||||
@@ -4319,6 +4319,17 @@
|
||||
<dependencies/>
|
||||
</relation>
|
||||
<relation>
|
||||
<name>has_defer_keyword</name>
|
||||
<cardinality>1000</cardinality>
|
||||
<columnsizes>
|
||||
<e>
|
||||
<k>id</k>
|
||||
<v>1000</v>
|
||||
</e>
|
||||
</columnsizes>
|
||||
<dependencies/>
|
||||
</relation>
|
||||
<relation>
|
||||
<name>is_optional_member</name>
|
||||
<cardinality>3668</cardinality>
|
||||
<columnsizes>
|
||||
|
||||
@@ -1 +1 @@
|
||||
| tst.ts:1:1:1:31 | import ... m "fs"; |
|
||||
| tst.ts:1:1:1:44 | import ... where"; |
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import javascript
|
||||
|
||||
from ImportDeclaration decl
|
||||
select decl
|
||||
|
||||
query predicate deferredImports(ImportDeclaration decl) { decl.isDeferredImport() }
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
import defer * as fs from "fs";
|
||||
import defer * as deferred from "somewhere";
|
||||
import type * as t from "somewhere";
|
||||
import * as normal from "somewhere";
|
||||
import defer from "somewhere";
|
||||
|
||||
Reference in New Issue
Block a user