diff --git a/es2panda/compiler/base/lreference.cpp b/es2panda/compiler/base/lreference.cpp index 1fdc88a98a1207e653eb58325fc4f64ea69083b8..0f2afaba2b1c8448d228973c0212db851dfadf1d 100644 --- a/es2panda/compiler/base/lreference.cpp +++ b/es2panda/compiler/base/lreference.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include namespace panda::es2panda::compiler { @@ -132,6 +134,12 @@ LReference LReference::CreateLRef(PandaGen *pg, const ir::AstNode *node, bool is return {node, pg, isDeclaration, ReferenceKind::VAR_OR_GLOBAL, res}; } + case ir::AstNodeType::TS_AS_EXPRESSION: { + return LReference::CreateLRef(pg, node->AsTSAsExpression()->Expr(), isDeclaration); + } + case ir::AstNodeType::TS_TYPE_ASSERTION: { + return LReference::CreateLRef(pg, node->AsTSTypeAssertion()->GetExpression(), isDeclaration); + } default: { UNREACHABLE(); } diff --git a/es2panda/ir/ts/tsAsExpression.h b/es2panda/ir/ts/tsAsExpression.h index 10a18df7d8040d1d6ef384a0bbba74b77ea18d57..c48120e72359f465e917b492ca2ac175bde139f1 100644 --- a/es2panda/ir/ts/tsAsExpression.h +++ b/es2panda/ir/ts/tsAsExpression.h @@ -45,6 +45,11 @@ public: return expression_; } + Expression *Expr() + { + return expression_; + } + const Expression *TypeAnnotation() const { return typeAnnotation_; diff --git a/es2panda/ir/ts/tsTypeAssertion.h b/es2panda/ir/ts/tsTypeAssertion.h index 583af00c2891a4293b058a58811a18eb26cb5b93..2e1eede000b97e15c18b9df9be77639a414161a1 100644 --- a/es2panda/ir/ts/tsTypeAssertion.h +++ b/es2panda/ir/ts/tsTypeAssertion.h @@ -46,6 +46,11 @@ public: return expression_; } + Expression *GetExpression() + { + return expression_; + } + void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index 9fb0e7cea752d657442b56465b01c5c054928673..63a8035399ea2a01bfe93c0399a05c79b94a87df 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -1626,6 +1626,15 @@ ir::Expression *ParserImpl::ParsePostPrimaryExpression(ir::Expression *primaryEx void ParserImpl::ValidateUpdateExpression(ir::Expression *returnExpression, bool isChainExpression) { + if (returnExpression->IsTSAsExpression()) { + ValidateUpdateExpression(returnExpression->AsTSAsExpression()->Expr(), isChainExpression); + return; + } + if (returnExpression->IsTSTypeAssertion()) { + ValidateUpdateExpression(returnExpression->AsTSTypeAssertion()->GetExpression(), isChainExpression); + return; + } + if ((!returnExpression->IsMemberExpression() && !returnExpression->IsIdentifier() && !returnExpression->IsTSNonNullExpression()) || isChainExpression) { @@ -2221,19 +2230,7 @@ ir::Expression *ParserImpl::ParseUnaryOrPrefixUpdateExpression(ExpressionParseFl ir::Expression *argument = ParseUnaryOrPrefixUpdateExpression(); if (lexer::Token::IsUpdateToken(operatorType)) { - if (!argument->IsIdentifier() && !argument->IsMemberExpression() && !argument->IsTSNonNullExpression()) { - ThrowSyntaxError("Invalid left-hand side in prefix operation"); - } - - if (argument->IsIdentifier()) { - const util::StringView &argumentStr = argument->AsIdentifier()->Name(); - - if (argumentStr.Is("eval")) { - ThrowSyntaxError("Assigning to 'eval' in strict mode is invalid"); - } else if (argumentStr.Is("arguments")) { - ThrowSyntaxError("Assigning to 'arguments' in strict mode is invalid"); - } - } + ValidateUpdateExpression(argument, false); } if (operatorType == lexer::TokenType::KEYW_DELETE && argument->IsIdentifier()) { diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 723076cab39745eeec2bb27a5122e6c8040458ef..32c18e95c4c096d679d968d2ba101ebcff73e05d 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -87,6 +88,7 @@ #include #include #include +#include #include #include #include @@ -3546,6 +3548,14 @@ void ParserImpl::ValidateLvalueAssignmentTarget(ir::Expression *node) const case ir::AstNodeType::MEMBER_EXPRESSION: { break; } + case ir::AstNodeType::TS_AS_EXPRESSION: { + ValidateLvalueAssignmentTarget(node->AsTSAsExpression()->Expr()); + break; + } + case ir::AstNodeType::TS_TYPE_ASSERTION: { + ValidateLvalueAssignmentTarget(node->AsTSTypeAssertion()->GetExpression()); + break; + } default: { ThrowSyntaxError("Invalid left-hand side in assignment expression"); } diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-2-expected.txt b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..573541ac9702dd3969c9bc859d2b91ec1f7e6e56 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-2-expected.txt @@ -0,0 +1 @@ +0 diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-2.ts b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a3f8fa41b9132211f16c1a804812e4062198a60c --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-2.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 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. + */ + + +var a = 1; +--(a as number); +print(a); \ No newline at end of file diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-3-expected.txt b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..0cfbf08886fca9a91cb753ec8734c84fcbe52c9f --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-3-expected.txt @@ -0,0 +1 @@ +2 diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-3.ts b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-3.ts new file mode 100644 index 0000000000000000000000000000000000000000..61a2a5fb001ba8ea31439f62f42eb828f42cd9c6 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-as-expression-3.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 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. + */ + + +var val = 1; +(val as number) = 2; +print(val); diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-type-assertions-2-expected.txt b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-type-assertions-2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..6ac0520a1bce7405f09848144cde8f1a42897aa3 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-type-assertions-2-expected.txt @@ -0,0 +1,2 @@ +str +4 diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-type-assertions-2.ts b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-type-assertions-2.ts new file mode 100644 index 0000000000000000000000000000000000000000..e41c50fca7738a2790f4d8d0b2dc76ecaa3c0084 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-type-assertions-2.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 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. + */ + + +var val: string = ""; +(val) = "str"; +print(val); + +var a: number = 0; +++(a); +(a)++; +(a as number)++; +++(a as number); +print(a); diff --git a/es2panda/test/parser/js/invalid-left-hand-side-in-prefix-operation-expected.txt b/es2panda/test/parser/js/invalid-left-hand-side-in-prefix-operation-expected.txt index 8a186e9fee6bb0efa71660be7e8113884e0a1514..fbb78c9fcb5e1a2508eeefa62f1aea692dd78be1 100644 --- a/es2panda/test/parser/js/invalid-left-hand-side-in-prefix-operation-expected.txt +++ b/es2panda/test/parser/js/invalid-left-hand-side-in-prefix-operation-expected.txt @@ -1 +1 @@ -SyntaxError: Invalid left-hand side in prefix operation [invalid-left-hand-side-in-prefix-operation.js:17:5] +SyntaxError: Invalid left-hand side operator. [invalid-left-hand-side-in-prefix-operation.js:17:5] diff --git a/es2panda/test/parser/ts/test-as-expression7-expected.txt b/es2panda/test/parser/ts/test-as-expression7-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b028b1d3b84c38aa87d1a78f1aee037dc84e22ee --- /dev/null +++ b/es2panda/test/parser/ts/test-as-expression7-expected.txt @@ -0,0 +1 @@ +SyntaxError: Invalid left-hand side operator. [test-as-expression7.ts:17:20] diff --git a/es2panda/test/parser/ts/test-as-expression7.ts b/es2panda/test/parser/ts/test-as-expression7.ts new file mode 100644 index 0000000000000000000000000000000000000000..b6ff7fb657649d5876dd8c7c2a2a979fb6c4b659 --- /dev/null +++ b/es2panda/test/parser/ts/test-as-expression7.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 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. + */ + + +++((a+b) as number);