From 1c657e1e149e3473da614eba03ce9d255663cf01 Mon Sep 17 00:00:00 2001 From: imteekay Date: Fri, 21 Jul 2023 22:54:43 -0300 Subject: [PATCH 1/8] Add example --- tests/varWithMultipleDeclarations.ts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/varWithMultipleDeclarations.ts diff --git a/tests/varWithMultipleDeclarations.ts b/tests/varWithMultipleDeclarations.ts new file mode 100644 index 0000000..48e9251 --- /dev/null +++ b/tests/varWithMultipleDeclarations.ts @@ -0,0 +1,3 @@ +var var1: string = 'test'; +var var1: string = 'what, another one?' +var var1 = 'no type annotation needed' \ No newline at end of file From 34092168a892973491d053c85a227009a9352007 Mon Sep 17 00:00:00 2001 From: imteekay Date: Fri, 21 Jul 2023 23:06:23 -0300 Subject: [PATCH 2/8] Add baselines --- ...arWithMultipleDeclarations.errors.baseline | 1 + .../varWithMultipleDeclarations.js.baseline | 1 + .../varWithMultipleDeclarations.tree.baseline | 91 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 baselines/reference/varWithMultipleDeclarations.errors.baseline create mode 100644 baselines/reference/varWithMultipleDeclarations.js.baseline create mode 100644 baselines/reference/varWithMultipleDeclarations.tree.baseline diff --git a/baselines/reference/varWithMultipleDeclarations.errors.baseline b/baselines/reference/varWithMultipleDeclarations.errors.baseline new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/baselines/reference/varWithMultipleDeclarations.errors.baseline @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/baselines/reference/varWithMultipleDeclarations.js.baseline b/baselines/reference/varWithMultipleDeclarations.js.baseline new file mode 100644 index 0000000..aac1ee7 --- /dev/null +++ b/baselines/reference/varWithMultipleDeclarations.js.baseline @@ -0,0 +1 @@ +"var var1 = 'test';\nvar var1 = 'var';\nvar var1 = 'no type annotation needed';\n" \ No newline at end of file diff --git a/baselines/reference/varWithMultipleDeclarations.tree.baseline b/baselines/reference/varWithMultipleDeclarations.tree.baseline new file mode 100644 index 0000000..380bd5d --- /dev/null +++ b/baselines/reference/varWithMultipleDeclarations.tree.baseline @@ -0,0 +1,91 @@ +{ + "locals": { + "var1": [ + { + "kind": "VariableDeclaration", + "pos": 26 + }, + { + "kind": "VariableDeclaration", + "pos": 70 + }, + { + "kind": "VariableDeclaration", + "pos": 105 + } + ] + }, + "statements": [ + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var1" + }, + "typename": { + "kind": "Identifier", + "text": "string" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var1" + }, + "typename": { + "kind": "Identifier", + "text": "string" + }, + "init": { + "kind": "StringLiteral", + "value": "var", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var1" + }, + "init": { + "kind": "StringLiteral", + "value": "no type annotation needed", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + } + ] +} \ No newline at end of file From fbb1aad0a0fc4b2ba4e3a4bf88e8ec0857a7a0c4 Mon Sep 17 00:00:00 2001 From: imteekay Date: Sat, 22 Jul 2023 12:09:05 -0300 Subject: [PATCH 3/8] `valueDeclaration` should store the first variable declaration --- src/bind.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bind.ts b/src/bind.ts index 3e802d6..b782ec9 100644 --- a/src/bind.ts +++ b/src/bind.ts @@ -69,7 +69,7 @@ export function bind(m: Module) { ); } else { symbol.declarations.push(declaration); - symbol.valueDeclaration = declaration; + symbol.valueDeclaration ||= declaration; symbol.flags |= isLet ? SymbolFlags.BlockScopedVariable : SymbolFlags.FunctionScopedVariable; From c3568b1643f83a4ce072f49cf1eedf18a292242c Mon Sep 17 00:00:00 2001 From: imteekay Date: Sat, 22 Jul 2023 12:12:20 -0300 Subject: [PATCH 4/8] Handle multiple declarations with different types --- ...arationsWithDifferentTypes.errors.baseline | 14 + ...DeclarationsWithDifferentTypes.js.baseline | 1 + ...clarationsWithDifferentTypes.tree.baseline | 343 ++++++++++++++++++ src/check.ts | 49 +++ .../multipleDeclarationsWithDifferentTypes.ts | 17 + 5 files changed, 424 insertions(+) create mode 100644 baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline create mode 100644 baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline create mode 100644 baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline create mode 100644 tests/multipleDeclarationsWithDifferentTypes.ts diff --git a/baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline b/baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline new file mode 100644 index 0000000..457878f --- /dev/null +++ b/baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline @@ -0,0 +1,14 @@ +[ + { + "pos": 87, + "message": "Subsequent variable declarations must have the same type. Variable 'var2' must be of type 'number', but here has type 'string'." + }, + { + "pos": 137, + "message": "Subsequent variable declarations must have the same type. Variable 'var3' must be of type 'string', but here has type 'number'." + }, + { + "pos": 198, + "message": "Subsequent variable declarations must have the same type. Variable 'var4' must be of type 'string', but here has type 'number'." + } +] \ No newline at end of file diff --git a/baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline b/baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline new file mode 100644 index 0000000..89a04d5 --- /dev/null +++ b/baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline @@ -0,0 +1 @@ +"var var1 = 'test';\nvar var1 = 'test';\nvar var2 = 2;\nvar var2 = 'test';\nvar var3 = 'test';\nvar var3 = 2;\nvar var3 = 'test';\nvar var4 = 'test';\nvar var4 = 2;\nvar var4 = 'test';\nvar var5 = 'test';\nvar var6 = 'test';\n" \ No newline at end of file diff --git a/baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline b/baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline new file mode 100644 index 0000000..e1c55fa --- /dev/null +++ b/baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline @@ -0,0 +1,343 @@ +{ + "locals": { + "var1": [ + { + "kind": "VariableDeclaration", + "pos": 18 + }, + { + "kind": "VariableDeclaration", + "pos": 45 + } + ], + "var2": [ + { + "kind": "VariableDeclaration", + "pos": 68 + }, + { + "kind": "VariableDeclaration", + "pos": 87 + } + ], + "var3": [ + { + "kind": "VariableDeclaration", + "pos": 115 + }, + { + "kind": "VariableDeclaration", + "pos": 137 + }, + { + "kind": "VariableDeclaration", + "pos": 156 + } + ], + "var4": [ + { + "kind": "VariableDeclaration", + "pos": 176 + }, + { + "kind": "VariableDeclaration", + "pos": 198 + }, + { + "kind": "VariableDeclaration", + "pos": 225 + } + ], + "var5": [ + { + "kind": "VariableDeclaration", + "pos": 245 + } + ], + "var6": [ + { + "kind": "VariableDeclaration", + "pos": 273 + } + ] + }, + "statements": [ + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var1" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var1" + }, + "typename": { + "kind": "Identifier", + "text": "string" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var2" + }, + "typename": { + "kind": "Identifier", + "text": "number" + }, + "init": { + "kind": "NumericLiteral", + "value": 2 + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var2" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var3" + }, + "typename": { + "kind": "Identifier", + "text": "string" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var3" + }, + "typename": { + "kind": "Identifier", + "text": "number" + }, + "init": { + "kind": "NumericLiteral", + "value": 2 + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var3" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var4" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var4" + }, + "typename": { + "kind": "Identifier", + "text": "number" + }, + "init": { + "kind": "NumericLiteral", + "value": 2 + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var4" + }, + "typename": { + "kind": "Identifier", + "text": "string" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var5" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + }, + { + "kind": "VariableStatement", + "declarationList": { + "kind": "VariableDeclarationList", + "declarations": { + "0": { + "kind": "VariableDeclaration", + "name": { + "kind": "Identifier", + "text": "var6" + }, + "typename": { + "kind": "Identifier", + "text": "string" + }, + "init": { + "kind": "StringLiteral", + "value": "test", + "isSingleQuote": true + } + } + }, + "flags": 0 + } + } + ] +} \ No newline at end of file diff --git a/src/check.ts b/src/check.ts index 157cffa..70eaf02 100644 --- a/src/check.ts +++ b/src/check.ts @@ -92,9 +92,52 @@ export function check(module: Module) { function checkVariableDeclaration(declaration: VariableDeclaration) { const initType = checkExpression(declaration.init); + const symbol = resolve( + module.locals, + declaration.name.text, + SymbolFlags.FunctionScopedVariable, + ); + + if (symbol && declaration !== symbol.valueDeclaration) { + const valueDeclarationType = checkVariableDeclarationType( + symbol.valueDeclaration!, + ); + + if (!declaration.typename) { + if (valueDeclarationType !== initType) { + error( + declaration.pos, + `Subsequent variable declarations must have the same type. Variable '${ + declaration.name.text + }' must be of type '${typeToString( + valueDeclarationType, + )}', but here has type '${typeToString(initType)}'.`, + ); + } + + return initType; + } + + const type = checkType(declaration.typename); + + if (valueDeclarationType !== type) { + error( + declaration.pos, + `Subsequent variable declarations must have the same type. Variable '${ + declaration.name.text + }' must be of type '${typeToString( + valueDeclarationType, + )}', but here has type '${typeToString(type)}'.`, + ); + } + + return type; + } + if (!declaration.typename) { return initType; } + const type = checkType(declaration.typename); if (type !== initType && type !== errorType) error( @@ -106,6 +149,12 @@ export function check(module: Module) { return type; } + function checkVariableDeclarationType(declaration: VariableDeclaration) { + return declaration.typename + ? checkType(declaration.typename) + : checkExpression(declaration.init); + } + function checkType(name: Identifier): Type { switch (name.text) { case 'string': diff --git a/tests/multipleDeclarationsWithDifferentTypes.ts b/tests/multipleDeclarationsWithDifferentTypes.ts new file mode 100644 index 0000000..b2c9f78 --- /dev/null +++ b/tests/multipleDeclarationsWithDifferentTypes.ts @@ -0,0 +1,17 @@ +var var1 = 'test'; +var var1: string = 'test'; + +var var2: number = 2; +var var2 = 'test'; + +var var3: string = 'test'; +var var3: number = 2; +var var3 = 'test'; + +var var4 = 'test'; +var var4: number = 2; +var var4: string = 'test'; + +var var5 = 'test'; + +var var6: string = 'test'; \ No newline at end of file From 435a29869994710275de7dae4c828765468e35a1 Mon Sep 17 00:00:00 2001 From: imteekay Date: Sat, 22 Jul 2023 12:44:26 -0300 Subject: [PATCH 5/8] Refactor to handle subsequent var types errors --- src/check.ts | 53 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/check.ts b/src/check.ts index 70eaf02..97b052b 100644 --- a/src/check.ts +++ b/src/check.ts @@ -103,33 +103,15 @@ export function check(module: Module) { symbol.valueDeclaration!, ); - if (!declaration.typename) { - if (valueDeclarationType !== initType) { - error( - declaration.pos, - `Subsequent variable declarations must have the same type. Variable '${ - declaration.name.text - }' must be of type '${typeToString( - valueDeclarationType, - )}', but here has type '${typeToString(initType)}'.`, - ); - } - - return initType; - } - - const type = checkType(declaration.typename); - - if (valueDeclarationType !== type) { - error( - declaration.pos, - `Subsequent variable declarations must have the same type. Variable '${ - declaration.name.text - }' must be of type '${typeToString( - valueDeclarationType, - )}', but here has type '${typeToString(type)}'.`, - ); - } + const type = declaration.typename + ? checkType(declaration.typename) + : initType; + + handleSubsequentVariableDeclarationsTypes( + declaration, + valueDeclarationType, + type, + ); return type; } @@ -176,4 +158,21 @@ export function check(module: Module) { return errorType; } } + + function handleSubsequentVariableDeclarationsTypes( + declaration: VariableDeclaration, + valueDeclarationType: Type, + declarationType: Type, + ) { + if (valueDeclarationType !== declarationType) { + error( + declaration.pos, + `Subsequent variable declarations must have the same type. Variable '${ + declaration.name.text + }' must be of type '${typeToString( + valueDeclarationType, + )}', but here has type '${typeToString(declarationType)}'.`, + ); + } + } } From 650af4e5f386d4627126aea425f9883321dda112 Mon Sep 17 00:00:00 2001 From: imteekay Date: Sun, 23 Jul 2023 09:40:15 -0300 Subject: [PATCH 6/8] Handling when the second variable declaration has a mismatch between its typename and its expression type --- ...arationsWithDifferentTypes.errors.baseline | 12 +- ...DeclarationsWithDifferentTypes.js.baseline | 2 +- ...clarationsWithDifferentTypes.tree.baseline | 103 +++++++----------- src/check.ts | 2 - .../multipleDeclarationsWithDifferentTypes.ts | 23 ++-- 5 files changed, 55 insertions(+), 87 deletions(-) diff --git a/baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline b/baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline index 457878f..15fec64 100644 --- a/baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline +++ b/baselines/reference/multipleDeclarationsWithDifferentTypes.errors.baseline @@ -1,14 +1,14 @@ [ { - "pos": 87, - "message": "Subsequent variable declarations must have the same type. Variable 'var2' must be of type 'number', but here has type 'string'." + "pos": 122, + "message": "Subsequent variable declarations must have the same type. Variable 's3' must be of type 'number', but here has type 'string'." }, { - "pos": 137, - "message": "Subsequent variable declarations must have the same type. Variable 'var3' must be of type 'string', but here has type 'number'." + "pos": 168, + "message": "Subsequent variable declarations must have the same type. Variable 's4' must be of type 'string', but here has type 'number'." }, { - "pos": 198, - "message": "Subsequent variable declarations must have the same type. Variable 'var4' must be of type 'string', but here has type 'number'." + "pos": 222, + "message": "Cannot assign initialiser of type 'number' to variable with declared type 'string'." } ] \ No newline at end of file diff --git a/baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline b/baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline index 89a04d5..5f4b99c 100644 --- a/baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline +++ b/baselines/reference/multipleDeclarationsWithDifferentTypes.js.baseline @@ -1 +1 @@ -"var var1 = 'test';\nvar var1 = 'test';\nvar var2 = 2;\nvar var2 = 'test';\nvar var3 = 'test';\nvar var3 = 2;\nvar var3 = 'test';\nvar var4 = 'test';\nvar var4 = 2;\nvar var4 = 'test';\nvar var5 = 'test';\nvar var6 = 'test';\n" \ No newline at end of file +"var s = 'test';\nvar s1 = 'test';\nvar s2 = 'test';\nvar s2 = 'test';\nvar s3 = 2;\nvar s3 = 'test';\nvar s4 = 'test';\nvar s4 = 2;\nvar s4 = 'test';\nvar s5 = 'test';\nvar s5 = 2;\n" \ No newline at end of file diff --git a/baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline b/baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline index e1c55fa..71c8a01 100644 --- a/baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline +++ b/baselines/reference/multipleDeclarationsWithDifferentTypes.tree.baseline @@ -1,63 +1,59 @@ { "locals": { - "var1": [ + "s": [ { "kind": "VariableDeclaration", - "pos": 18 - }, + "pos": 15 + } + ], + "s1": [ { "kind": "VariableDeclaration", - "pos": 45 + "pos": 41 } ], - "var2": [ + "s2": [ { "kind": "VariableDeclaration", - "pos": 68 + "pos": 59 }, { "kind": "VariableDeclaration", - "pos": 87 + "pos": 84 } ], - "var3": [ - { - "kind": "VariableDeclaration", - "pos": 115 - }, + "s3": [ { "kind": "VariableDeclaration", - "pos": 137 + "pos": 105 }, { "kind": "VariableDeclaration", - "pos": 156 + "pos": 122 } ], - "var4": [ + "s4": [ { "kind": "VariableDeclaration", - "pos": 176 + "pos": 148 }, { "kind": "VariableDeclaration", - "pos": 198 + "pos": 168 }, { "kind": "VariableDeclaration", - "pos": 225 + "pos": 185 } ], - "var5": [ + "s5": [ { "kind": "VariableDeclaration", - "pos": 245 - } - ], - "var6": [ + "pos": 203 + }, { "kind": "VariableDeclaration", - "pos": 273 + "pos": 223 } ] }, @@ -71,7 +67,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var1" + "text": "s" }, "init": { "kind": "StringLiteral", @@ -92,7 +88,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var1" + "text": "s1" }, "typename": { "kind": "Identifier", @@ -117,31 +113,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var2" - }, - "typename": { - "kind": "Identifier", - "text": "number" - }, - "init": { - "kind": "NumericLiteral", - "value": 2 - } - } - }, - "flags": 0 - } - }, - { - "kind": "VariableStatement", - "declarationList": { - "kind": "VariableDeclarationList", - "declarations": { - "0": { - "kind": "VariableDeclaration", - "name": { - "kind": "Identifier", - "text": "var2" + "text": "s2" }, "init": { "kind": "StringLiteral", @@ -162,7 +134,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var3" + "text": "s2" }, "typename": { "kind": "Identifier", @@ -187,7 +159,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var3" + "text": "s3" }, "typename": { "kind": "Identifier", @@ -211,7 +183,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var3" + "text": "s3" }, "init": { "kind": "StringLiteral", @@ -232,7 +204,11 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var4" + "text": "s4" + }, + "typename": { + "kind": "Identifier", + "text": "string" }, "init": { "kind": "StringLiteral", @@ -253,7 +229,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var4" + "text": "s4" }, "typename": { "kind": "Identifier", @@ -277,11 +253,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var4" - }, - "typename": { - "kind": "Identifier", - "text": "string" + "text": "s4" }, "init": { "kind": "StringLiteral", @@ -302,7 +274,7 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var5" + "text": "s5" }, "init": { "kind": "StringLiteral", @@ -323,16 +295,15 @@ "kind": "VariableDeclaration", "name": { "kind": "Identifier", - "text": "var6" + "text": "s5" }, "typename": { "kind": "Identifier", "text": "string" }, "init": { - "kind": "StringLiteral", - "value": "test", - "isSingleQuote": true + "kind": "NumericLiteral", + "value": 2 } } }, diff --git a/src/check.ts b/src/check.ts index 97b052b..afb14e8 100644 --- a/src/check.ts +++ b/src/check.ts @@ -112,8 +112,6 @@ export function check(module: Module) { valueDeclarationType, type, ); - - return type; } if (!declaration.typename) { diff --git a/tests/multipleDeclarationsWithDifferentTypes.ts b/tests/multipleDeclarationsWithDifferentTypes.ts index b2c9f78..4ccf529 100644 --- a/tests/multipleDeclarationsWithDifferentTypes.ts +++ b/tests/multipleDeclarationsWithDifferentTypes.ts @@ -1,17 +1,16 @@ -var var1 = 'test'; -var var1: string = 'test'; +var s = 'test'; -var var2: number = 2; -var var2 = 'test'; +var s1: string = 'test'; -var var3: string = 'test'; -var var3: number = 2; -var var3 = 'test'; +var s2 = 'test'; +var s2: string = 'test'; -var var4 = 'test'; -var var4: number = 2; -var var4: string = 'test'; +var s3: number = 2; +var s3 = 'test'; -var var5 = 'test'; +var s4: string = 'test'; +var s4: number = 2; +var s4 = 'test'; -var var6: string = 'test'; \ No newline at end of file +var s5 = 'test'; +var s5: string = 2; \ No newline at end of file From afe6c0fc0a4b298a59b9ac331155acb03111e3f6 Mon Sep 17 00:00:00 2001 From: imteekay Date: Tue, 1 Aug 2023 08:19:28 -0300 Subject: [PATCH 7/8] Check exercise --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0386e40..df83d87 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ npm run mtsc ./tests/singleVar.ts - [x] Add use-before-declaration errors in the checker. - [x] Finally, add an ES2015 -> ES5 transform that transforms `let` to `var`. - [x] Allow var statements to declare multiple symbols (https://github.com/imteekay/mini-typescript-fork/pull/9). -- [ ] Allow var to have multiple declarations. +- [x] Allow var to have multiple declarations. - You'll need to convert a Symbol's declaration into a list. - Check that all declarations have the same type. - [ ] Add objects and object types. From f371f0a46ad1bd394aee8313609707cba71aa9ed Mon Sep 17 00:00:00 2001 From: imteekay Date: Tue, 1 Aug 2023 08:24:36 -0300 Subject: [PATCH 8/8] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index df83d87..b89a4a9 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The purpose of this repo is to practice the exercises and learn more about compi ### To get set up -```sh +```bash # Get set up npm i npm run build