Skip to content

Commit db10c22

Browse files
authored
Merge pull request github#15663 from asgerf/js/endpoint-naming2
JS: Improvements to endpoint naming
2 parents 339c890 + 29ffeb6 commit db10c22

File tree

21 files changed

+331
-256
lines changed

21 files changed

+331
-256
lines changed

javascript/ql/lib/semmle/javascript/endpoints/EndpointNaming.qll

Lines changed: 213 additions & 213 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
testFailures
22
ambiguousPreferredPredecessor
3+
| pack2/lib.js:1:1:3:1 | def moduleImport("pack2").getMember("exports").getMember("lib").getMember("LibClass").getInstance() |
4+
| pack2/lib.js:8:22:8:34 | def moduleImport("pack2").getMember("exports").getMember("lib").getMember("LibClass").getMember("foo") |
5+
| pack2/main.js:1:1:3:1 | def moduleImport("pack2").getMember("exports").getMember("MainClass").getInstance() |
36
ambiguousSinkName
4-
ambiguousClassObjectName
5-
ambiguousClassInstanceName
67
ambiguousFunctionName
78
failures

javascript/ql/test/library-tests/EndpointNaming/EndpointNaming.ql

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,21 @@ import semmle.javascript.endpoints.EndpointNaming as EndpointNaming
55
import testUtilities.InlineExpectationsTest
66
import EndpointNaming::Debug
77

8+
private predicate isIgnored(DataFlow::FunctionNode function) {
9+
function.getFunction() = any(ConstructorDeclaration decl | decl.isSynthetic()).getBody()
10+
}
11+
812
module TestConfig implements TestSig {
9-
string getARelevantTag() { result = ["instance", "class", "method", "alias"] }
13+
string getARelevantTag() { result = ["name", "alias"] }
1014

1115
predicate hasActualResult(Location location, string element, string tag, string value) {
12-
exists(string package, string name |
13-
element = "" and
16+
element = "" and
17+
tag = "name" and
18+
exists(DataFlow::SourceNode function, string package, string name |
19+
EndpointNaming::functionHasPrimaryName(function, package, name) and
20+
not isIgnored(function) and
21+
location = function.getAstNode().getLocation() and
1422
value = EndpointNaming::renderName(package, name)
15-
|
16-
exists(DataFlow::ClassNode cls | location = cls.getAstNode().getLocation() |
17-
tag = "class" and
18-
EndpointNaming::classObjectHasPrimaryName(cls, package, name)
19-
or
20-
tag = "instance" and
21-
EndpointNaming::classInstanceHasPrimaryName(cls, package, name)
22-
)
23-
or
24-
exists(DataFlow::FunctionNode function |
25-
not function.getFunction() = any(ConstructorDeclaration decl | decl.isSynthetic()).getBody() and
26-
location = function.getFunction().getLocation() and
27-
tag = "method" and
28-
EndpointNaming::functionHasPrimaryName(function, package, name)
29-
)
3023
)
3124
or
3225
element = "" and
@@ -35,7 +28,7 @@ module TestConfig implements TestSig {
3528
API::Node aliasDef, string primaryPackage, string primaryName, string aliasPackage,
3629
string aliasName
3730
|
38-
EndpointNaming::aliasDefinition(primaryPackage, primaryName, aliasPackage, aliasName, aliasDef) and
31+
EndpointNaming::aliasDefinition(aliasPackage, aliasName, primaryPackage, primaryName, aliasDef) and
3932
value =
4033
EndpointNaming::renderName(aliasPackage, aliasName) + "==" +
4134
EndpointNaming::renderName(primaryPackage, primaryName) and
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
export class PublicClass {} // $ class=(pack1).PublicClass instance=(pack1).PublicClass.prototype
1+
export class PublicClass {} // $ name=(pack1).PublicClass
22

33
class PrivateClass {}
44

5-
export const ExportedConst = class ExportedConstClass {} // $ class=(pack1).ExportedConst instance=(pack1).ExportedConst.prototype
5+
export const ExportedConst = class ExportedConstClass {} // $ name=(pack1).ExportedConst
66

7-
class ClassWithEscapingInstance {} // $ instance=(pack1).ClassWithEscapingInstance.prototype
7+
class ClassWithEscapingInstance {
8+
m() {} // $ name=(pack1).ClassWithEscapingInstance.prototype.m
9+
}
810

911
export function getEscapingInstance() {
1012
return new ClassWithEscapingInstance();
11-
} // $ method=(pack1).getEscapingInstance
13+
} // $ name=(pack1).getEscapingInstance
1214

13-
export function publicFunction() {} // $ method=(pack1).publicFunction
15+
export function publicFunction() {} // $ name=(pack1).publicFunction
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default class FooClass {} // $ class=(pack10).Foo instance=(pack10).Foo.prototype
1+
export default class FooClass {} // $ name=(pack10).Foo
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const f1 = {
2+
m() {} // $ name=(pack11).C1.publicField.really.long.name.m
3+
};
4+
5+
export class C1 {
6+
private static privateField = f1;
7+
8+
public static publicField = {
9+
really: {
10+
long: {
11+
name: f1
12+
}
13+
}
14+
}
15+
} // $ name=(pack11).C1
16+
17+
const f2 = {
18+
m() {} // $ name=(pack11).C2.publicField.really.long.name.m
19+
}
20+
21+
export class C2 {
22+
static #privateField = f2;
23+
24+
static publicField = {
25+
really: {
26+
long: {
27+
name: f2
28+
}
29+
}
30+
}
31+
} // $ name=(pack11).C2
32+
33+
function f3() {} // $ name=(pack11).C3.publicField.really.long.name
34+
35+
export class C3 {
36+
private static privateField = f3;
37+
38+
public static publicField = {
39+
really: {
40+
long: {
41+
name: f3
42+
}
43+
}
44+
}
45+
} // $ name=(pack11).C3
46+
47+
48+
const f4 = {
49+
m() {} // $ name=(pack11).C4.really.long.name.m
50+
};
51+
52+
export const C4 = {
53+
[Math.random()]: f4,
54+
really: {
55+
long: {
56+
name: f4
57+
}
58+
}
59+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "pack11",
3+
"main": "./index.js"
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function wrap(fn) {
2+
return x => fn(x);
3+
}
4+
5+
function f() {}
6+
export const f1 = wrap(f); // $ name=(pack12).f1
7+
export const f2 = wrap(f); // $ name=(pack12).f2
8+
9+
function g() {}
10+
export const g1 = wrap(g); // $ name=(pack12).g1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "pack12",
3+
"main": "./index.js"
4+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class AmbiguousClass {
2-
instanceMethod(foo) {} // $ method=(pack2).lib.LibClass.prototype.instanceMethod
3-
} // $ class=(pack2).lib.LibClass instance=(pack2).lib.LibClass.prototype
2+
instanceMethod(foo) {} // $ name=(pack2).lib.LibClass.prototype.instanceMethod
3+
} // $ name=(pack2).lib.LibClass
44

55
export default AmbiguousClass; // $ alias=(pack2).lib.default==(pack2).lib.LibClass
66
export { AmbiguousClass as LibClass }
7+
8+
AmbiguousClass.foo = function() {} // $ name=(pack2).lib.LibClass.foo

0 commit comments

Comments
 (0)