Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions change-notes/1.23/extractor-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
* Recognition of CommonJS modules has improved. As a result, some files that were previously extracted as
global scripts are now extracted as modules.
* Top-level `await` is now supported.
* A bug was fixed in how the TypeScript extractor handles default-exported anonymous classes.
17 changes: 8 additions & 9 deletions javascript/extractor/src/com/semmle/js/extractor/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.semmle.js.extractor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import com.semmle.js.extractor.ExtractorConfig.HTMLHandling;
import com.semmle.js.extractor.ExtractorConfig.Platform;
import com.semmle.js.extractor.ExtractorConfig.SourceType;
Expand All @@ -31,14 +23,21 @@
import com.semmle.util.process.ArgsParser;
import com.semmle.util.process.ArgsParser.FileMode;
import com.semmle.util.trap.TrapWriter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/** The main entry point of the JavaScript extractor. */
public class Main {
/**
* A version identifier that should be updated every time the extractor changes in such a way that
* it may produce different tuples for the same file under the same {@link ExtractorConfig}.
*/
public static final String EXTRACTOR_VERSION = "2019-09-18";
public static final String EXTRACTOR_VERSION = "2019-10-07";

public static final Pattern NEWLINE = Pattern.compile("\n");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,13 @@ private Node convertClass(JsonObject node, String kind, SourceLocation loc) thro
SourceLocation bodyLoc = new SourceLocation(loc.getSource(), loc.getStart(), loc.getEnd());
advance(bodyLoc, skip);
ClassBody body = new ClassBody(bodyLoc, convertChildren(node, "members"));
if ("ClassExpression".equals(kind)) {
if ("ClassExpression".equals(kind) || id == null) {
// Note that `export default class {}` is represented as a ClassDeclaration
// in TypeScript but we treat this as a ClassExpression.
ClassExpression classExpr =
new ClassExpression(loc, id, typeParameters, superClass, superInterfaces, body);
attachSymbolInformation(classExpr.getClassDef(), node);
return classExpr;
return fixExports(loc, classExpr);
}
boolean hasDeclareKeyword = hasModifier(node, "DeclareKeyword");
boolean hasAbstractKeyword = hasModifier(node, "AbstractKeyword");
Expand Down Expand Up @@ -1225,6 +1227,11 @@ private Node convertForOfStatement(JsonObject node, SourceLocation loc) throws P
private Node convertFunctionDeclaration(JsonObject node, SourceLocation loc) throws ParseError {
List<Expression> params = convertParameters(node);
Identifier fnId = convertChild(node, "name", "Identifier");
if (fnId == null) {
// Anonymous function declarations may occur as part of default exported functions.
// We represent these as function expressions.
return fixExports(loc, convertFunctionExpression(node, loc));
}
BlockStatement fnbody = convertChild(node, "body");
boolean generator = hasChild(node, "asteriskToken");
boolean async = hasModifier(node, "AsyncKeyword");
Expand Down Expand Up @@ -2305,15 +2312,15 @@ private IJSXName convertJSXName(Expression e) {
* <p>If the declared statement has decorators, the {@code loc} should first be advanced past
* these using {@link #advanceUntilAfter}.
*/
private Node fixExports(SourceLocation loc, Statement decl) {
private Node fixExports(SourceLocation loc, Node decl) {
Matcher m = EXPORT_DECL_START.matcher(loc.getSource());
if (m.find()) {
String skipped = m.group(0);
SourceLocation outerLoc = new SourceLocation(loc.getSource(), loc.getStart(), loc.getEnd());
advance(loc, skipped);
// capture group 1 is `default`, if present
if (m.group(1) == null)
return new ExportNamedDeclaration(outerLoc, decl, new ArrayList<>(), null);
return new ExportNamedDeclaration(outerLoc, (Statement) decl, new ArrayList<>(), null);
return new ExportDefaultDeclaration(outerLoc, decl);
}
return decl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Foo } from "somwhere";

export default class extends Foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Foo } from "somwhere";

export default function(x=Foo) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
classExprs
| exportClass.ts:3:16:3:35 | class extends Foo {} |
functionExprs
| exportClass.ts:3:34:3:33 | (...arg ... rgs); } |
| exportFunction.ts:3:16:3:33 | function(x=Foo) {} |
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import javascript

query ClassExpr classExprs() { any() }

query FunctionExpr functionExprs() { any() }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Foo } from "./node_modules/somwhere";

export default function(x=Foo) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var C1 = global.C1; // OK
var C2 = global.C2; // OK

class C extends C1 {}
export default class extends C2 {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var C1 = global.C1; // OK

export default function(x=C1) {}