From 8fd0db3336c7be8b6f8342425abc28c0671edb35 Mon Sep 17 00:00:00 2001 From: xucheng46 Date: Thu, 13 Oct 2022 22:44:36 +0800 Subject: [PATCH] Enable TypePredicate in class method and definite in variable Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I5VMHG Test: parser tests, compiler tests, test262 Signed-off-by: xucheng46 Change-Id: I836794ccc40b6438c57f4297ce942309b306f037 --- es2panda/ir/statements/variableDeclarator.cpp | 5 +- es2panda/ir/statements/variableDeclarator.h | 12 + es2panda/parser/expressionParser.cpp | 8 +- es2panda/parser/statementParser.cpp | 7 + .../ts/test-class-definiton20-expected.txt | 665 ++++++++++++++++++ .../test/parser/ts/test-class-definiton20.ts | 29 + .../ts/test-definite-variable-expected.txt | 74 ++ .../test/parser/ts/test-definite-variable.ts | 17 + es2panda/test/test_tsc_ignore_list.txt | 4 - 9 files changed, 815 insertions(+), 6 deletions(-) create mode 100644 es2panda/test/parser/ts/test-class-definiton20-expected.txt create mode 100644 es2panda/test/parser/ts/test-class-definiton20.ts create mode 100644 es2panda/test/parser/ts/test-definite-variable-expected.txt create mode 100644 es2panda/test/parser/ts/test-definite-variable.ts diff --git a/es2panda/ir/statements/variableDeclarator.cpp b/es2panda/ir/statements/variableDeclarator.cpp index ef3fb7d5bc..1c4d57bae7 100644 --- a/es2panda/ir/statements/variableDeclarator.cpp +++ b/es2panda/ir/statements/variableDeclarator.cpp @@ -40,7 +40,10 @@ void VariableDeclarator::Iterate(const NodeTraverser &cb) const void VariableDeclarator::Dump(ir::AstDumper *dumper) const { - dumper->Add({{"type", "VariableDeclarator"}, {"id", id_}, {"init", AstDumper::Nullable(init_)}}); + dumper->Add({{"type", "VariableDeclarator"}, + {"id", id_}, + {"definite", AstDumper::Optional(definite_)}, + {"init", AstDumper::Nullable(init_)}}); } void VariableDeclarator::Compile(compiler::PandaGen *pg) const diff --git a/es2panda/ir/statements/variableDeclarator.h b/es2panda/ir/statements/variableDeclarator.h index 4c695dfbf2..02d8cc2933 100644 --- a/es2panda/ir/statements/variableDeclarator.h +++ b/es2panda/ir/statements/variableDeclarator.h @@ -64,6 +64,16 @@ public: return id_; } + bool Definite() const + { + return definite_; + } + + void SetDefinite(bool definite) + { + definite_ = definite; + } + void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; @@ -73,6 +83,8 @@ public: private: Expression *id_; Expression *init_; + // TODO(xucheng): don't use now, it will be used in type checker later + bool definite_; }; } // namespace panda::es2panda::ir diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index cb6d065fdf..5ac12ec0d8 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -564,7 +564,9 @@ ir::Expression *ParserImpl::ParseCoverParenthesizedExpressionAndArrowParameterLi ir::Expression *returnTypeAnnotation = nullptr; if (Extension() == ScriptExtension::TS && lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COLON) { lexer_->NextToken(); // eat ':' + options |= TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; returnTypeAnnotation = ParseTsTypeAnnotation(&options); + options &= ~TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; } if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_ARROW) { @@ -580,7 +582,9 @@ ir::Expression *ParserImpl::ParseCoverParenthesizedExpressionAndArrowParameterLi ir::Expression *returnTypeAnnotation = nullptr; if (Extension() == ScriptExtension::TS && lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COLON) { lexer_->NextToken(); // eat ':' + options |= TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; returnTypeAnnotation = ParseTsTypeAnnotation(&options); + options &= ~TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; } if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_ARROW) { @@ -615,8 +619,10 @@ ir::Expression *ParserImpl::ParseCoverParenthesizedExpressionAndArrowParameterLi if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COLON) { lexer_->NextToken(); // eat ':' - options = ~TypeAnnotationParsingOptions::THROW_ERROR; + options &= ~TypeAnnotationParsingOptions::THROW_ERROR; + options |= TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; returnTypeAnnotation = ParseTsTypeAnnotation(&options); + options &= ~TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; } if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_ARROW) { diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 80cbcb8567..6c8250593d 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -1925,7 +1925,12 @@ ir::VariableDeclarator *ParserImpl::ParseVariableDeclarator(VariableParsingFlags } } + bool isDefinite = false; if (Extension() == ScriptExtension::TS) { + if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK) { + lexer_->NextToken(); // eat '!' + isDefinite = true; + } if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COLON) { lexer_->NextToken(); // eat ':' TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::THROW_ERROR; @@ -1953,6 +1958,8 @@ ir::VariableDeclarator *ParserImpl::ParseVariableDeclarator(VariableParsingFlags declarator->SetRange({startLoc, endLoc}); } + declarator->SetDefinite(isDefinite); + std::vector bindings = util::Helpers::CollectBindingNames(init); for (const auto *binding : bindings) { diff --git a/es2panda/test/parser/ts/test-class-definiton20-expected.txt b/es2panda/test/parser/ts/test-class-definiton20-expected.txt new file mode 100644 index 0000000000..d2b8028ba7 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton20-expected.txt @@ -0,0 +1,665 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "value": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypePredicate", + "parameterName": { + "type": "TSThisType", + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "asserts": false, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 19, + "column": 16 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "value": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 21 + } + } + } + ], + "returnType": { + "type": "TSTypePredicate", + "parameterName": { + "type": "TSThisType", + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 28 + } + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "asserts": false, + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 42 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "c", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "value": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "RestElement", + "argument": { + "type": "Identifier", + "name": "c", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 13 + }, + "end": { + "line": 26, + "column": 14 + } + } + }, + "typeAnnotation": { + "type": "TSArrayType", + "elementType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 26, + "column": 17 + }, + "end": { + "line": 26, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 17 + }, + "end": { + "line": 26, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 9 + }, + "end": { + "line": 26, + "column": 25 + } + } + } + ], + "returnType": { + "type": "TSTypePredicate", + "parameterName": { + "type": "TSThisType", + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 26, + "column": 33 + } + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 26, + "column": 37 + }, + "end": { + "line": 26, + "column": 43 + } + } + }, + "asserts": false, + "loc": { + "start": { + "line": 26, + "column": 29 + }, + "end": { + "line": 26, + "column": 43 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 27, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 9 + }, + "end": { + "line": 27, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 47 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 9 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 9 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 28, + "column": 6 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 29, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 30, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-class-definiton20.ts b/es2panda/test/parser/ts/test-class-definiton20.ts new file mode 100644 index 0000000000..cc1c9ec683 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton20.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 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. + */ + + +class C { + a = () : this is number => { + return false; + } + + b = (b : number) : this is number => { + return false; + } + + c = (...c : number[]) : this is number => { + return false; + } +} diff --git a/es2panda/test/parser/ts/test-definite-variable-expected.txt b/es2panda/test/parser/ts/test-definite-variable-expected.txt new file mode 100644 index 0000000000..f3f69098b6 --- /dev/null +++ b/es2panda/test/parser/ts/test-definite-variable-expected.txt @@ -0,0 +1,74 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "definite": true, + "init": null, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 18, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-definite-variable.ts b/es2panda/test/parser/ts/test-definite-variable.ts new file mode 100644 index 0000000000..409503959d --- /dev/null +++ b/es2panda/test/parser/ts/test-definite-variable.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022 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!:number; diff --git a/es2panda/test/test_tsc_ignore_list.txt b/es2panda/test/test_tsc_ignore_list.txt index 84265f688b..7212fc2461 100644 --- a/es2panda/test/test_tsc_ignore_list.txt +++ b/es2panda/test/test_tsc_ignore_list.txt @@ -8,7 +8,6 @@ es2panda/test/TypeScript/tests/cases/compiler/constructorOverloads5.ts es2panda/test/TypeScript/tests/cases/compiler/continueTarget3.ts es2panda/test/TypeScript/tests/cases/compiler/controlFlowCaching.ts es2panda/test/TypeScript/tests/cases/compiler/declarationEmitDestructuringOptionalBindingParametersInOverloads.ts -es2panda/test/TypeScript/tests/cases/compiler/discriminantsAndPrimitives.ts es2panda/test/TypeScript/tests/cases/compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts es2panda/test/TypeScript/tests/cases/compiler/emitBundleWithShebang1.ts es2panda/test/TypeScript/tests/cases/compiler/exportAsNamespace.d.ts @@ -37,7 +36,6 @@ es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringFo es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringForArrayBindingPatternDefaultValues2.ts es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringForObjectBindingPattern2.ts es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringForObjectBindingPatternDefaultValues2.ts -es2panda/test/TypeScript/tests/cases/compiler/taggedTemplateStringWithSymbolExpression01.ts es2panda/test/TypeScript/tests/cases/compiler/withStatementInternalComments.ts es2panda/test/TypeScript/tests/cases/conformance/async/es2017/asyncAwait_es2017.ts es2panda/test/TypeScript/tests/cases/conformance/classes/members/privateNames/privateNameComputedPropertyName1.ts @@ -78,9 +76,7 @@ es2panda/test/TypeScript/tests/cases/conformance/es6/unicodeExtendedEscapes/unic es2panda/test/TypeScript/tests/cases/conformance/es6/unicodeExtendedEscapes/unicodeExtendedEscapesInRegularExpressions19_ES6.ts es2panda/test/TypeScript/tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck38.ts es2panda/test/TypeScript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts -es2panda/test/TypeScript/tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator_es2020.ts es2panda/test/TypeScript/tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts -es2panda/test/TypeScript/tests/cases/conformance/expressions/typeGuards/typeGuardFunctionOfFormThis.ts es2panda/test/TypeScript/tests/cases/conformance/functions/functionWithUseStrictAndSimpleParameterList.ts es2panda/test/TypeScript/tests/cases/conformance/interfaces/interfaceDeclarations/asiPreventsParsingAsInterface01.ts es2panda/test/TypeScript/tests/cases/conformance/interfaces/interfaceDeclarations/asiPreventsParsingAsInterface03.ts -- Gitee