Merge pull request #1221 from asger-semmle/contextual-typing

TS: Extract contextual type for object/array literals
This commit is contained in:
Esben Sparre Andreasen
2019-04-09 10:43:01 +02:00
committed by GitHub
6 changed files with 82 additions and 2 deletions

View File

@@ -176,7 +176,10 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
if (typeChecker != null) {
if (isTypedNode(node)) {
let type = typeChecker.getTypeAtLocation(node);
let contextualType = isContextuallyTypedNode(node)
? typeChecker.getContextualType(node)
: null;
let type = contextualType || typeChecker.getTypeAtLocation(node);
if (type != null) {
let id = typeTable.buildType(type);
if (id != null) {
@@ -326,3 +329,10 @@ function isTypedNode(node: ts.Node): boolean {
return ts.isTypeNode(node);
}
}
type ContextuallyTypedNode = (ts.ArrayLiteralExpression | ts.ObjectLiteralExpression) & AugmentedNode;
function isContextuallyTypedNode(node: ts.Node): node is ContextuallyTypedNode {
let kind = node.kind;
return kind === ts.SyntaxKind.ArrayLiteralExpression || kind === ts.SyntaxKind.ObjectLiteralExpression;
}

View File

@@ -0,0 +1,36 @@
| tst.ts:2:3:2:6 | name | string |
| tst.ts:3:3:3:10 | children | T[] |
| tst.ts:6:5:6:11 | context | T |
| tst.ts:6:18:17:1 | {\\n nam ... }\\n ]\\n} | T |
| tst.ts:7:3:7:6 | name | string |
| tst.ts:7:9:7:11 | 'x' | "x" |
| tst.ts:8:3:8:10 | children | ({ name: string; } \| { name: string; children: ... |
| tst.ts:8:13:16:3 | [\\n { ... }\\n ] | T[] |
| tst.ts:9:5:9:18 | { name: 'x1' } | T |
| tst.ts:9:7:9:10 | name | string |
| tst.ts:9:13:9:16 | 'x1' | "x1" |
| tst.ts:10:5:15:5 | {\\n ... ]\\n } | T |
| tst.ts:11:7:11:10 | name | string |
| tst.ts:11:13:11:16 | 'x2' | "x2" |
| tst.ts:12:7:12:14 | children | { name: string; }[] |
| tst.ts:12:17:14:7 | [\\n ... ] | T[] |
| tst.ts:13:9:13:22 | { name: 'x3' } | T |
| tst.ts:13:11:13:14 | name | string |
| tst.ts:13:17:13:20 | 'x3' | "x3" |
| tst.ts:19:5:19:13 | nocontext | { name: string; children: ({ name: string; chil... |
| tst.ts:19:17:30:1 | {\\n nam ... }\\n ]\\n} | { name: string; children: ({ name: string; } \| ... |
| tst.ts:20:3:20:6 | name | string |
| tst.ts:20:9:20:11 | 'x' | "x" |
| tst.ts:21:3:21:10 | children | ({ name: string; } \| { name: string; children: ... |
| tst.ts:21:13:29:3 | [\\n { ... }\\n ] | ({ name: string; } \| { name: string; children: ... |
| tst.ts:22:5:22:18 | { name: 'x1' } | { name: string; } |
| tst.ts:22:7:22:10 | name | string |
| tst.ts:22:13:22:16 | 'x1' | "x1" |
| tst.ts:23:5:28:5 | {\\n ... ]\\n } | { name: string; children: { name: string; }[]; } |
| tst.ts:24:7:24:10 | name | string |
| tst.ts:24:13:24:16 | 'x2' | "x2" |
| tst.ts:25:7:25:14 | children | { name: string; }[] |
| tst.ts:25:17:27:7 | [\\n ... ] | { name: string; }[] |
| tst.ts:26:9:26:22 | { name: 'x3' } | { name: string; } |
| tst.ts:26:11:26:14 | name | string |
| tst.ts:26:17:26:20 | 'x3' | "x3" |

View File

@@ -0,0 +1,4 @@
import javascript
from Expr e
select e, e.getType()

View File

@@ -0,0 +1 @@
{ "include": ["."] }

View File

@@ -0,0 +1,30 @@
interface T {
name: string;
children?: T[];
}
let context: T = {
name: 'x',
children: [
{ name: 'x1' },
{
name: 'x2',
children: [
{ name: 'x3' }
]
}
]
}
let nocontext = {
name: 'x',
children: [
{ name: 'x1' },
{
name: 'x2',
children: [
{ name: 'x3' }
]
}
]
}

View File

@@ -1,4 +1,3 @@
| in unknown scope |
| Array in global scope |
| Intl in global scope |
| Intl.CollatorOptions in global scope |