|
| 1 | +import * as parser from "@typescript-eslint/parser"; |
| 2 | +import assert from "assert"; |
| 3 | +import * as utils from "../../../src/utils/index.js"; |
| 4 | + |
| 5 | +function parseExpression(input: string) { |
| 6 | + const ast = parser.parse(`async function * fn () { (${input}) }`); |
| 7 | + if (ast.body.length !== 1) { |
| 8 | + throw new Error("Expected a single expression"); |
| 9 | + } |
| 10 | + const fn = ast.body[0]; |
| 11 | + if (fn.type !== "FunctionDeclaration") { |
| 12 | + throw new Error("Expected an expression statement"); |
| 13 | + } |
| 14 | + if (fn.body.body.length !== 1) { |
| 15 | + throw new Error("Expected a single expression in function body"); |
| 16 | + } |
| 17 | + const body = fn.body.body[0]; |
| 18 | + if (body.type !== "ExpressionStatement") { |
| 19 | + throw new Error("Expected an expression statement"); |
| 20 | + } |
| 21 | + return body.expression; |
| 22 | +} |
| 23 | + |
| 24 | +describe("hasTypeInfo (Expression)", () => { |
| 25 | + it("Identifier (no type)", () => { |
| 26 | + const node = parseExpression("a"); |
| 27 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 28 | + }); |
| 29 | + it("Literal", () => { |
| 30 | + const node = parseExpression("42"); |
| 31 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 32 | + }); |
| 33 | + |
| 34 | + it("BinaryExpression (no type)", () => { |
| 35 | + const node = parseExpression("a + b"); |
| 36 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 37 | + }); |
| 38 | + |
| 39 | + it("ArrayExpression", () => { |
| 40 | + const node = parseExpression("[1, 2, 3]"); |
| 41 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 42 | + }); |
| 43 | + |
| 44 | + it("ObjectExpression", () => { |
| 45 | + const node = parseExpression("({a: 1})"); |
| 46 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 47 | + }); |
| 48 | + |
| 49 | + it("FunctionExpression (untyped param)", () => { |
| 50 | + const node = parseExpression("function(a) { return a }"); |
| 51 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 52 | + }); |
| 53 | + |
| 54 | + it("FunctionExpression (typed param)", () => { |
| 55 | + const node = parseExpression("function(a: number) { return a }"); |
| 56 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("FunctionExpression (typed return type)", () => { |
| 60 | + const node = parseExpression("function(): string { return '' }"); |
| 61 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 62 | + }); |
| 63 | + |
| 64 | + it("FunctionExpression (typed param and return type)", () => { |
| 65 | + const node = parseExpression("function(a: number): string { return '' }"); |
| 66 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 67 | + }); |
| 68 | + |
| 69 | + it("FunctionExpression (default param)", () => { |
| 70 | + const node = parseExpression("function(a = 1) { return a }"); |
| 71 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 72 | + }); |
| 73 | + |
| 74 | + it("FunctionExpression (rest param)", () => { |
| 75 | + const node = parseExpression("function(...args) { return args }"); |
| 76 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 77 | + }); |
| 78 | + |
| 79 | + it("FunctionExpression (type parameters)", () => { |
| 80 | + const node = parseExpression("function<T>(a: T) { return a }"); |
| 81 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 82 | + }); |
| 83 | + |
| 84 | + it("ArrowFunctionExpression (with typed param)", () => { |
| 85 | + const node = parseExpression("(a: string) => a"); |
| 86 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 87 | + }); |
| 88 | + |
| 89 | + it("ArrowFunctionExpression (untyped param)", () => { |
| 90 | + const node = parseExpression("(a) => a"); |
| 91 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 92 | + }); |
| 93 | + |
| 94 | + it("ArrowFunctionExpression (typed return type)", () => { |
| 95 | + const node = parseExpression("(): number => 1"); |
| 96 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 97 | + }); |
| 98 | + |
| 99 | + it("ArrowFunctionExpression (typed param and return type)", () => { |
| 100 | + const node = parseExpression("(a: string): number => 1"); |
| 101 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 102 | + }); |
| 103 | + |
| 104 | + it("ArrowFunctionExpression (default param)", () => { |
| 105 | + const node = parseExpression("(a = 1) => a"); |
| 106 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 107 | + }); |
| 108 | + |
| 109 | + it("ArrowFunctionExpression (rest param)", () => { |
| 110 | + const node = parseExpression("(...args) => args"); |
| 111 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 112 | + }); |
| 113 | + |
| 114 | + it("ArrowFunctionExpression (type parameters)", () => { |
| 115 | + const node = parseExpression("<T>(a: T) => a"); |
| 116 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 117 | + }); |
| 118 | + |
| 119 | + it("CallExpression (no type)", () => { |
| 120 | + const node = parseExpression("foo(1)"); |
| 121 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 122 | + }); |
| 123 | + |
| 124 | + it("MemberExpression (no type)", () => { |
| 125 | + const node = parseExpression("obj.prop"); |
| 126 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 127 | + }); |
| 128 | + |
| 129 | + it("TemplateLiteral", () => { |
| 130 | + const node = parseExpression("`hello ${a}`"); |
| 131 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 132 | + }); |
| 133 | + |
| 134 | + it("UnaryExpression", () => { |
| 135 | + const node = parseExpression("!a"); |
| 136 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 137 | + }); |
| 138 | + |
| 139 | + it("UpdateExpression", () => { |
| 140 | + const node = parseExpression("a++"); |
| 141 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 142 | + }); |
| 143 | + |
| 144 | + it("ConditionalExpression", () => { |
| 145 | + const node = parseExpression("a ? 1 : 2"); |
| 146 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 147 | + }); |
| 148 | + |
| 149 | + it("ConditionalExpression (no type)", () => { |
| 150 | + const node = parseExpression("a ? x : 2"); |
| 151 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 152 | + }); |
| 153 | + |
| 154 | + it("AssignmentExpression", () => { |
| 155 | + const node = parseExpression("a = 1"); |
| 156 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 157 | + }); |
| 158 | + |
| 159 | + it("AssignmentExpression (no type at right)", () => { |
| 160 | + const node = parseExpression("a = x"); |
| 161 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 162 | + }); |
| 163 | + |
| 164 | + it("SequenceExpression", () => { |
| 165 | + const node = parseExpression("(a, b, 1)"); |
| 166 | + assert.strictEqual(utils.hasTypeInfo(node), true); |
| 167 | + }); |
| 168 | + |
| 169 | + it("SequenceExpression (no type at last)", () => { |
| 170 | + const node = parseExpression("(a, 1, b)"); |
| 171 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 172 | + }); |
| 173 | + |
| 174 | + it("NewExpression (no type)", () => { |
| 175 | + const node = parseExpression("new Foo(1)"); |
| 176 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 177 | + }); |
| 178 | + |
| 179 | + it("ClassExpression (no type)", () => { |
| 180 | + const node = parseExpression("class A {}"); |
| 181 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 182 | + }); |
| 183 | + |
| 184 | + it("TaggedTemplateExpression (no type)", () => { |
| 185 | + const node = parseExpression("tag`foo`"); |
| 186 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 187 | + }); |
| 188 | + |
| 189 | + it("AwaitExpression (no type)", () => { |
| 190 | + const node = parseExpression("await a"); |
| 191 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 192 | + }); |
| 193 | + |
| 194 | + it("YieldExpression (no type)", () => { |
| 195 | + const node = parseExpression("yield 1"); |
| 196 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 197 | + }); |
| 198 | + |
| 199 | + it("ImportExpression (no type)", () => { |
| 200 | + const node = parseExpression("import('foo')"); |
| 201 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 202 | + }); |
| 203 | + |
| 204 | + it("issue #746", () => { |
| 205 | + const node = parseExpression("e => {e; let b: number;}"); |
| 206 | + assert.strictEqual(utils.hasTypeInfo(node), false); |
| 207 | + }); |
| 208 | +}); |
0 commit comments