Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS: Fix in node.js classifier #1934

Merged
merged 5 commits into from Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions change-notes/1.23/extractor-javascript.md
Expand Up @@ -5,3 +5,6 @@
## Changes to code extraction

* Asynchronous generator methods are now parsed correctly and no longer cause a spurious syntax error.

* Recognition of CommonJS modules has improved. As a result, some files that were previously extracted as
global scripts are now extracted as modules.
2 changes: 1 addition & 1 deletion javascript/extractor/src/com/semmle/js/extractor/Main.java
Expand Up @@ -37,7 +37,7 @@ 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-04";
public static final String EXTRACTOR_VERSION = "2019-09-13";

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

Expand Down
Expand Up @@ -97,8 +97,9 @@ private static boolean isRequireCall(Expression e) {
if (e instanceof CallExpression) {
CallExpression call = (CallExpression) e;
Expression callee = call.getCallee();
return (isIdentifier(callee, "require") || isRequireCall(callee))
&& call.getArguments().size() == 1;
if (isIdentifier(callee, "require") && call.getArguments().size() == 1) return true;
if (isRequireCall(callee)) return true;
return false;
} else if (e instanceof MemberExpression) {
return isRequireCall(((MemberExpression) e).getObject());
} else if (e instanceof AssignmentExpression) {
Expand Down
Expand Up @@ -164,9 +164,8 @@ public void testUMD() {

@Test
public void amdefine() {
// not currently detected
isNodeJS(
"if (typeof define !== 'function') define = require('amdefine')(module, require);", false);
"if (typeof define !== 'function') define = require('amdefine')(module, require);", true);
}

@Test
Expand Down Expand Up @@ -202,4 +201,14 @@ public void requireInIf() {
+ "}",
true);
}

@Test
public void requireAndCall() {
isNodeJS("var foo = require('foo')();", true);
}

@Test
public void requireAndCallMethod() {
isNodeJS("var foo = require('foo').bar();", true);
}
}
14 changes: 14 additions & 0 deletions javascript/ql/src/meta/analysis-quality/NumModules.ql
@@ -0,0 +1,14 @@
/**
* @name Modules
* @description The number of modules in the snapshot.
* @kind metric
* @metricType project
* @metricAggregate sum
* @tags meta
* @id js/meta/modules
*/

import javascript
import CallGraphQuality

select projectRoot(), count(Module mod)