From b9a5d07ed979e58aa804b2ae86ef6d19ab9ecdf9 Mon Sep 17 00:00:00 2001 From: liyue Date: Thu, 25 Jan 2024 02:40:31 +0000 Subject: [PATCH] fix non null assertion in left value assignment Signed-off-by: liyue Change-Id: I719569912f7519aeb7d508932845414397be5b88 --- es2panda/compiler/base/lreference.cpp | 4 + es2panda/parser/parserImpl.cpp | 5 + .../test-ts-non-null-assertion-expected.txt | 1 + .../compiler/test-ts-non-null-assertion.ts | 20 ++ .../ts/test-non-null-assertion-expected.txt | 226 ++++++++++++++++++ .../test/parser/ts/test-non-null-assertion.ts | 20 ++ 6 files changed, 276 insertions(+) create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion.ts create mode 100644 es2panda/test/parser/ts/test-non-null-assertion-expected.txt create mode 100644 es2panda/test/parser/ts/test-non-null-assertion.ts diff --git a/es2panda/compiler/base/lreference.cpp b/es2panda/compiler/base/lreference.cpp index 94cfa3bf19..6e409c5564 100644 --- a/es2panda/compiler/base/lreference.cpp +++ b/es2panda/compiler/base/lreference.cpp @@ -28,6 +28,7 @@ #include #include #include +#include namespace panda::es2panda::compiler { @@ -201,6 +202,9 @@ LReference LReference::CreateLRef(PandaGen *pg, const ir::AstNode *node, bool is case ir::AstNodeType::TS_TYPE_ASSERTION: { return LReference::CreateLRef(pg, node->AsTSTypeAssertion()->GetExpression(), isDeclaration); } + case ir::AstNodeType::TS_NON_NULL_EXPRESSION: { + return LReference::CreateLRef(pg, node->AsTSNonNullExpression()->Expr(), isDeclaration); + } default: { UNREACHABLE(); } diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 768252217f..3185b350f2 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -105,6 +105,7 @@ #include #include #include +#include #include #include #include @@ -4092,6 +4093,10 @@ void ParserImpl::ValidateLvalueAssignmentTarget(ir::Expression *node) const ValidateLvalueAssignmentTarget(node->AsTSTypeAssertion()->GetExpression()); break; } + case ir::AstNodeType::TS_NON_NULL_EXPRESSION: { + ValidateLvalueAssignmentTarget(node->AsTSNonNullExpression()->Expr()); + break; + } default: { ThrowSyntaxError("Invalid left-hand side in assignment expression"); } diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion-expected.txt new file mode 100644 index 0000000000..ce01362503 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion-expected.txt @@ -0,0 +1 @@ +hello diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion.ts new file mode 100644 index 0000000000..d033ff522a --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-assertion.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +let a:number = 1 +if (a ! = 2) { + print("hello") +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test-non-null-assertion-expected.txt b/es2panda/test/parser/ts/test-non-null-assertion-expected.txt new file mode 100644 index 0000000000..d3c8918537 --- /dev/null +++ b/es2panda/test/parser/ts/test-non-null-assertion-expected.txt @@ -0,0 +1,226 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 17 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + { + "type": "IfStatement", + "test": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "TSNonNullExpression", + "expression": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "print", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "hello", + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 18 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } +} diff --git a/es2panda/test/parser/ts/test-non-null-assertion.ts b/es2panda/test/parser/ts/test-non-null-assertion.ts new file mode 100644 index 0000000000..d033ff522a --- /dev/null +++ b/es2panda/test/parser/ts/test-non-null-assertion.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +let a:number = 1 +if (a ! = 2) { + print("hello") +} \ No newline at end of file -- Gitee