mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
JS: Separate JSDoc qualified names into individual identifiers
This commit is contained in:
@@ -2,12 +2,12 @@ package com.semmle.js.ast.jsdoc;
|
||||
|
||||
import com.semmle.js.ast.SourceLocation;
|
||||
|
||||
/** A named JSDoc type. */
|
||||
public class NameExpression extends JSDocTypeExpression {
|
||||
/** An identifier in a JSDoc type. */
|
||||
public class Identifier extends JSDocTypeExpression {
|
||||
private final String name;
|
||||
|
||||
public NameExpression(SourceLocation loc, String name) {
|
||||
super(loc, "NameExpression");
|
||||
public Identifier(SourceLocation loc, String name) {
|
||||
super(loc, "Identifier");
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.semmle.js.ast.jsdoc;
|
||||
|
||||
import com.semmle.js.ast.SourceLocation;
|
||||
|
||||
/** A qualified name in a JSDoc type. */
|
||||
public class QualifiedNameExpression extends JSDocTypeExpression {
|
||||
private final JSDocTypeExpression base;
|
||||
private final Identifier name;
|
||||
|
||||
public QualifiedNameExpression(SourceLocation loc, JSDocTypeExpression base, Identifier name) {
|
||||
super(loc, "QualifiedNameExpression");
|
||||
this.base = base;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Visitor v) {
|
||||
v.visit(this);
|
||||
}
|
||||
|
||||
/** Returns the expression on the left side of the dot character. */
|
||||
public JSDocTypeExpression getBase() {
|
||||
return base;
|
||||
}
|
||||
|
||||
/** Returns the identifier on the right-hand side of the dot character. */
|
||||
public Identifier getNameNode() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String pp() {
|
||||
return base.pp() + "." + name.pp();
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,9 @@ public interface Visitor {
|
||||
|
||||
public void visit(JSDocTag nd);
|
||||
|
||||
public void visit(NameExpression nd);
|
||||
public void visit(Identifier nd);
|
||||
|
||||
public void visit(QualifiedNameExpression nd);
|
||||
|
||||
public void visit(NullableLiteral nd);
|
||||
|
||||
|
||||
@@ -9,13 +9,14 @@ import com.semmle.js.ast.jsdoc.JSDocComment;
|
||||
import com.semmle.js.ast.jsdoc.JSDocElement;
|
||||
import com.semmle.js.ast.jsdoc.JSDocTag;
|
||||
import com.semmle.js.ast.jsdoc.JSDocTypeExpression;
|
||||
import com.semmle.js.ast.jsdoc.NameExpression;
|
||||
import com.semmle.js.ast.jsdoc.Identifier;
|
||||
import com.semmle.js.ast.jsdoc.NonNullableType;
|
||||
import com.semmle.js.ast.jsdoc.NullLiteral;
|
||||
import com.semmle.js.ast.jsdoc.NullableLiteral;
|
||||
import com.semmle.js.ast.jsdoc.NullableType;
|
||||
import com.semmle.js.ast.jsdoc.OptionalType;
|
||||
import com.semmle.js.ast.jsdoc.ParameterType;
|
||||
import com.semmle.js.ast.jsdoc.QualifiedNameExpression;
|
||||
import com.semmle.js.ast.jsdoc.RecordType;
|
||||
import com.semmle.js.ast.jsdoc.RestType;
|
||||
import com.semmle.js.ast.jsdoc.TypeApplication;
|
||||
@@ -42,7 +43,7 @@ public class JSDocExtractor {
|
||||
jsdocTypeExprKinds.put("UndefinedLiteral", 2);
|
||||
jsdocTypeExprKinds.put("NullableLiteral", 3);
|
||||
jsdocTypeExprKinds.put("VoidLiteral", 4);
|
||||
jsdocTypeExprKinds.put("NameExpression", 5);
|
||||
jsdocTypeExprKinds.put("Identifier", 5);
|
||||
jsdocTypeExprKinds.put("TypeApplication", 6);
|
||||
jsdocTypeExprKinds.put("NullableType", 7);
|
||||
jsdocTypeExprKinds.put("NonNullableType", 8);
|
||||
@@ -52,6 +53,7 @@ public class JSDocExtractor {
|
||||
jsdocTypeExprKinds.put("FunctionType", 12);
|
||||
jsdocTypeExprKinds.put("OptionalType", 13);
|
||||
jsdocTypeExprKinds.put("RestType", 14);
|
||||
jsdocTypeExprKinds.put("QualifiedNameExpression", 15);
|
||||
}
|
||||
|
||||
private final TrapWriter trapwriter;
|
||||
@@ -122,10 +124,17 @@ public class JSDocExtractor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NameExpression nd) {
|
||||
public void visit(Identifier nd) {
|
||||
visit((JSDocTypeExpression) nd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(QualifiedNameExpression nd) {
|
||||
Label label = visit((JSDocTypeExpression) nd);
|
||||
visit(nd.getBase(), label, 0);
|
||||
visit(nd.getNameNode(), label, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NullableLiteral nd) {
|
||||
visit((JSDocTypeExpression) nd);
|
||||
|
||||
@@ -10,13 +10,14 @@ import com.semmle.js.ast.jsdoc.FunctionType;
|
||||
import com.semmle.js.ast.jsdoc.JSDocComment;
|
||||
import com.semmle.js.ast.jsdoc.JSDocTag;
|
||||
import com.semmle.js.ast.jsdoc.JSDocTypeExpression;
|
||||
import com.semmle.js.ast.jsdoc.NameExpression;
|
||||
import com.semmle.js.ast.jsdoc.Identifier;
|
||||
import com.semmle.js.ast.jsdoc.NonNullableType;
|
||||
import com.semmle.js.ast.jsdoc.NullLiteral;
|
||||
import com.semmle.js.ast.jsdoc.NullableLiteral;
|
||||
import com.semmle.js.ast.jsdoc.NullableType;
|
||||
import com.semmle.js.ast.jsdoc.OptionalType;
|
||||
import com.semmle.js.ast.jsdoc.ParameterType;
|
||||
import com.semmle.js.ast.jsdoc.QualifiedNameExpression;
|
||||
import com.semmle.js.ast.jsdoc.RecordType;
|
||||
import com.semmle.js.ast.jsdoc.RestType;
|
||||
import com.semmle.js.ast.jsdoc.TypeApplication;
|
||||
@@ -827,10 +828,16 @@ public class JSDocParser {
|
||||
}
|
||||
|
||||
private JSDocTypeExpression parseNameExpression() throws ParseError {
|
||||
Object name = value;
|
||||
SourceLocation loc = loc();
|
||||
expect(Token.NAME);
|
||||
return finishNode(new NameExpression(loc, name.toString()));
|
||||
// Hacky initial implementation with wrong locations
|
||||
String[] parts = value.toString().split("\\.");
|
||||
JSDocTypeExpression node = finishNode(new Identifier(loc, parts[0]));
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
Identifier memberName = finishNode(new Identifier(loc, parts[i]));
|
||||
node = finishNode(new QualifiedNameExpression(loc, node, memberName));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// TypeExpressionList :=
|
||||
@@ -923,14 +930,14 @@ public class JSDocParser {
|
||||
|
||||
SourceLocation loc = loc();
|
||||
expr = parseTypeExpression();
|
||||
if (expr instanceof NameExpression && token == Token.COLON) {
|
||||
if (expr instanceof Identifier && token == Token.COLON) {
|
||||
// Identifier ':' TypeExpression
|
||||
consume(Token.COLON);
|
||||
expr =
|
||||
finishNode(
|
||||
new ParameterType(
|
||||
new SourceLocation(loc),
|
||||
((NameExpression) expr).getName(),
|
||||
((Identifier) expr).getName(),
|
||||
parseTypeExpression()));
|
||||
}
|
||||
if (token == Token.EQUAL) {
|
||||
@@ -1106,7 +1113,7 @@ public class JSDocParser {
|
||||
consume(Token.RBRACK, "expected an array-style type declaration (' + value + '[])");
|
||||
List<JSDocTypeExpression> expressions = new ArrayList<>();
|
||||
expressions.add(expr);
|
||||
NameExpression nameExpr = finishNode(new NameExpression(new SourceLocation(loc), "Array"));
|
||||
Identifier nameExpr = finishNode(new Identifier(new SourceLocation(loc), "Array"));
|
||||
return finishNode(new TypeApplication(loc, nameExpr, expressions));
|
||||
}
|
||||
|
||||
@@ -1527,9 +1534,9 @@ public class JSDocParser {
|
||||
// fixed at the end
|
||||
if (isParamTitle(this._title)
|
||||
&& this._tag.type != null
|
||||
&& this._tag.type instanceof NameExpression) {
|
||||
this._extra_name = ((NameExpression) this._tag.type).getName();
|
||||
this._tag.name = ((NameExpression) this._tag.type).getName();
|
||||
&& this._tag.type instanceof Identifier) {
|
||||
this._extra_name = ((Identifier) this._tag.type).getName();
|
||||
this._tag.name = ((Identifier) this._tag.type).getName();
|
||||
this._tag.type = null;
|
||||
} else {
|
||||
if (!this.addError("Missing or invalid tag name")) {
|
||||
@@ -1645,7 +1652,7 @@ public class JSDocParser {
|
||||
Position start = new Position(_tag.startLine, _tag.startColumn, _tag.startColumn);
|
||||
Position end = new Position(_tag.startLine, _tag.startColumn, _tag.startColumn);
|
||||
SourceLocation loc = new SourceLocation(_extra_name, start, end);
|
||||
this._tag.type = new NameExpression(loc, _extra_name);
|
||||
this._tag.type = new Identifier(loc, _extra_name);
|
||||
}
|
||||
this._tag.name = null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user