From 2b8248e7ad289d3364118a6c31bca90c262def52 Mon Sep 17 00:00:00 2001 From: xingshunxiang Date: Mon, 14 Jul 2025 22:15:09 +0800 Subject: [PATCH] Jsdoc refactor Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICM9F7?from=project-issue Description: 1. support c-api to get jsdoc, and do not store the jsdoc info on the ast. 2. support c-api to get license string of files 3. supprot c-api to clarify the export and export type 4. revert the old jsdoc implementation Reason: Jsdoc refactor Tests: ninja tests passed tests/tests-u-runner/runner.sh --ets-cts --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-func-tests --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --astchecker --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-runtime --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --parser --no-js --show-progress --build-dir x64.release --processes=all passed Signed-off-by: xingshunxiang --- ets2panda/BUILD.gn | 4 +- ets2panda/CMakeLists.txt | 2 +- .../compiler/lowering/ets/enumLowering.cpp | 2 +- .../ets/topLevelStmts/globalClassHandler.cpp | 1 + ets2panda/compiler/lowering/util.cpp | 15 + ets2panda/compiler/lowering/util.h | 3 + ets2panda/ir/base/classDefinition.cpp | 2 +- ets2panda/ir/base/classDefinition.h | 14 +- ets2panda/ir/base/classProperty.cpp | 2 +- ets2panda/ir/base/classProperty.h | 6 +- ets2panda/ir/base/scriptFunction.cpp | 6 +- ets2panda/ir/base/scriptFunction.h | 4 +- ets2panda/ir/ets/etsModule.cpp | 2 +- ets2panda/ir/ets/etsModule.h | 7 +- .../expressions/arrowFunctionExpression.cpp | 2 +- .../ir/expressions/arrowFunctionExpression.h | 5 +- ets2panda/ir/jsDocAllowed.h | 134 ----- .../ir/statements/functionDeclaration.cpp | 2 +- ets2panda/ir/statements/functionDeclaration.h | 10 +- .../ir/statements/variableDeclaration.cpp | 8 +- ets2panda/ir/statements/variableDeclaration.h | 7 +- ets2panda/ir/ts/tsEnumMember.h | 1 - ets2panda/ir/ts/tsInterfaceDeclaration.cpp | 2 +- ets2panda/ir/ts/tsInterfaceDeclaration.h | 9 +- ets2panda/ir/ts/tsTypeAliasDeclaration.cpp | 2 +- ets2panda/ir/ts/tsTypeAliasDeclaration.h | 7 +- ets2panda/lexer/ETSLexer.cpp | 22 +- ets2panda/lexer/lexer.cpp | 91 +-- ets2panda/lexer/lexer.h | 17 +- ets2panda/lexer/templates/token.inl.erb | 4 - ets2panda/lexer/templates/tokenType.h.erb | 2 - ets2panda/lexer/token/letters.h | 7 - ets2panda/parser/ETSparser.cpp | 32 +- ets2panda/parser/ETSparser.h | 14 - ets2panda/parser/ETSparserClasses.cpp | 123 +--- ets2panda/parser/ETSparserEnums.cpp | 1 - ets2panda/parser/ETSparserJsDocInfo.cpp | 277 --------- ets2panda/parser/ETSparserNamespaces.cpp | 4 +- ets2panda/parser/ETSparserStatements.cpp | 15 - ets2panda/parser/JsdocHelper.cpp | 199 +++++++ ets2panda/parser/JsdocHelper.h | 190 ++++++ ets2panda/parser/TypedParser.cpp | 8 - ets2panda/parser/context/parserContext.cpp | 5 - ets2panda/parser/context/parserContext.h | 12 +- ets2panda/parser/parserImpl.cpp | 9 +- ets2panda/parser/parserImpl.h | 1 + ets2panda/parser/statementParser.cpp | 5 - ets2panda/public/CMakeLists.txt | 2 - ets2panda/public/cppToCTypes.yaml | 22 - ets2panda/public/es2panda_lib.cpp | 68 ++- ets2panda/public/es2panda_lib.h | 15 +- ets2panda/public/es2panda_lib.idl.erb | 3 + ets2panda/public/es2panda_lib.rb | 4 +- ets2panda/public/ignoredAllowed.yaml | 1 - ...as_class_member_getValue_call-expected.txt | 20 +- .../ets/import_tests/enum_export-expected.txt | 36 +- .../test/parser/ets/declare_enum-expected.txt | 12 +- .../ets/switch_enum_string_case-expected.txt | 10 +- ets2panda/test/runtime/ets/JsDocInfoTest.ets | 194 ------ ets2panda/test/unit/plugin/CMakeLists.txt | 2 +- ...roceed_to_state_ast_node_type_analyzer.cpp | 2 +- ..._proceed_to_state_check_jsdoc-expected.txt | 211 +++++++ .../plugin_proceed_to_state_check_jsdoc.cpp | 556 ++++++++++++++++-- ets2panda/test/unit/sizeof_node_test.cpp | 105 +--- ets2panda/util/options.cpp | 2 - ets2panda/util/options.h | 6 - ets2panda/util/options.yaml | 5 - 67 files changed, 1304 insertions(+), 1269 deletions(-) delete mode 100644 ets2panda/ir/jsDocAllowed.h delete mode 100644 ets2panda/parser/ETSparserJsDocInfo.cpp create mode 100644 ets2panda/parser/JsdocHelper.cpp create mode 100644 ets2panda/parser/JsdocHelper.h delete mode 100644 ets2panda/test/runtime/ets/JsDocInfoTest.ets create mode 100644 ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc-expected.txt diff --git a/ets2panda/BUILD.gn b/ets2panda/BUILD.gn index 60c32ea888..f479f53a89 100644 --- a/ets2panda/BUILD.gn +++ b/ets2panda/BUILD.gn @@ -483,11 +483,11 @@ libes2panda_sources = [ "parser/ETSparserClasses.cpp", "parser/ETSparserEnums.cpp", "parser/ETSparserExpressions.cpp", - "parser/ETSparserJsDocInfo.cpp", "parser/ETSparserNamespaces.cpp", "parser/ETSparserStatements.cpp", "parser/ETSparserTypes.cpp", "parser/JSparser.cpp", + "parser/JsdocHelper.cpp", "parser/TSparser.cpp", "parser/ThrowingTypedParser.cpp", "parser/TypedParser.cpp", @@ -776,7 +776,6 @@ HEADERS_TO_BE_PARSED = [ "ir/ets/etsParameterExpression.h", "ir/ts/tsTypeQuery.h", "ir/expressions/importExpression.h", - "ir/jsDocAllowed.h", "varbinder/variable.h", "varbinder/scope.h", "varbinder/varbinder.h", @@ -806,7 +805,6 @@ ES2PANDA_API_GENERATED = [ "$LIBGEN_DIR/gen/headers/varbinder/variableFlags.yaml", "$LIBGEN_DIR/gen/headers/ir/typed.yaml", "$LIBGEN_DIR/gen/headers/ir/annotationAllowed.yaml", - "$LIBGEN_DIR/gen/headers/ir/jsDocAllowed.yaml", "$LIBGEN_DIR/gen/headers/es2panda.yaml", "$LIBGEN_DIR/gen/headers/ir/statements/labelledStatement.yaml", "$LIBGEN_DIR/gen/headers/checker/types/ts/unknownType.yaml", diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index 73d465ae36..cee988b68d 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -516,6 +516,7 @@ set(ES2PANDA_LIB_SRC parser/expressionTSParser.cpp parser/ASparser.cpp parser/JSparser.cpp + parser/JsdocHelper.cpp parser/parserImpl.cpp parser/ETSFormattedParser.cpp parser/ETSparser.cpp @@ -523,7 +524,6 @@ set(ES2PANDA_LIB_SRC parser/ETSparserClasses.cpp parser/ETSparserEnums.cpp parser/ETSparserExpressions.cpp - parser/ETSparserJsDocInfo.cpp parser/ETSparserNamespaces.cpp parser/ETSparserStatements.cpp parser/ETSparserTypes.cpp diff --git a/ets2panda/compiler/lowering/ets/enumLowering.cpp b/ets2panda/compiler/lowering/ets/enumLowering.cpp index 7e515ade28..600b13a35c 100644 --- a/ets2panda/compiler/lowering/ets/enumLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumLowering.cpp @@ -328,6 +328,7 @@ ir::ClassDeclaration *EnumLoweringPhase::CreateClass(ir::TSEnumDeclaration *cons CreateCCtorForEnumClass(classDef); } CreateCtorForEnumClass(classDef, enumType); + classDecl->SetRange(enumDecl->Range()); return classDecl; } @@ -603,7 +604,6 @@ ir::ClassDeclaration *EnumLoweringPhase::CreateEnumStringClassFromEnumDeclaratio enumClassDecl->SetParent(enumDecl->Parent()); ProcessEnumClassDeclaration(enumDecl, flags, enumClassDecl); - return enumClassDecl; } diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp index 857b07c9a0..7beb4a70d5 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/globalClassHandler.cpp @@ -180,6 +180,7 @@ ir::ClassDeclaration *GlobalClassHandler::CreateTransformedClass(ir::ETSModule * } classDef->SetAnnotations(std::move(annotations)); + classDecl->SetRange(ns->Range()); return classDecl; } diff --git a/ets2panda/compiler/lowering/util.cpp b/ets2panda/compiler/lowering/util.cpp index 865e9d1827..bed24872a8 100644 --- a/ets2panda/compiler/lowering/util.cpp +++ b/ets2panda/compiler/lowering/util.cpp @@ -20,6 +20,7 @@ #include "checker/checker.h" #include "checker/ETSAnalyzer.h" #include "checker/types/gradualType.h" +#include "parser/JsdocHelper.h" namespace ark::es2panda::compiler { @@ -336,6 +337,20 @@ ir::AstNode *DeclarationFromIdentifier(const ir::Identifier *node) return decl->Node(); } +// NOTE: used to get the license string from the input root node. +util::StringView GetLicenseFromRootNode(const ir::AstNode *node) +{ + std::unique_ptr jsdocGetter = std::make_unique(node); + return jsdocGetter->GetLicenseStringFromStart(); +} + +// NOTE: used to get the jsdoc string from the input node. +util::StringView JsdocStringFromDeclaration(const ir::AstNode *node) +{ + std::unique_ptr jsdocGetter = std::make_unique(node); + return jsdocGetter->GetJsdocBackward(); +} + // Note: run varbinder on the new node generated in lowering phases (without ClearTypesVariablesAndScopes) void BindLoweredNode(varbinder::ETSBinder *varBinder, ir::AstNode *node) { diff --git a/ets2panda/compiler/lowering/util.h b/ets2panda/compiler/lowering/util.h index 8f5202f4aa..4e84d94298 100644 --- a/ets2panda/compiler/lowering/util.h +++ b/ets2panda/compiler/lowering/util.h @@ -45,6 +45,9 @@ void Recheck(PhaseManager *phaseManager, varbinder::ETSBinder *varBinder, checke ir::AstNode *DeclarationFromIdentifier(const ir::Identifier *node); // NOTE: used to get the declaration name in Plugin API and LSP std::optional GetNameOfDeclaration(const ir::AstNode *node); +// NOTE: used to get the license string from the input root node. +util::StringView GetLicenseFromRootNode(const ir::AstNode *node); +util::StringView JsdocStringFromDeclaration(const ir::AstNode *node); // Note: run varbinder on the new node generated in lowering phases void BindLoweredNode(varbinder::ETSBinder *varBinder, ir::AstNode *node); diff --git a/ets2panda/ir/base/classDefinition.cpp b/ets2panda/ir/base/classDefinition.cpp index 5fe57979b3..1c322b1984 100644 --- a/ets2panda/ir/base/classDefinition.cpp +++ b/ets2panda/ir/base/classDefinition.cpp @@ -498,7 +498,7 @@ void ClassDefinition::CopyTo(AstNode *other) const otherImpl->functionalReferenceReferencedMethod_ = functionalReferenceReferencedMethod_; otherImpl->exportedClasses_ = exportedClasses_; - JsDocAllowed>::CopyTo(other); + AnnotationAllowed::CopyTo(other); } std::atomic ClassDefinition::classCounter_ = 0; diff --git a/ets2panda/ir/base/classDefinition.h b/ets2panda/ir/base/classDefinition.h index a4017bf927..869939f869 100644 --- a/ets2panda/ir/base/classDefinition.h +++ b/ets2panda/ir/base/classDefinition.h @@ -23,7 +23,6 @@ #include "ir/astNode.h" #include "ir/astNodeHistory.h" #include "ir/expressions/identifier.h" -#include "ir/jsDocAllowed.h" #include "ir/statements/annotationUsage.h" #include "ir/statements/classDeclaration.h" #include "util/language.h" @@ -72,7 +71,7 @@ struct enumbitops::IsAllowedType : namespace ark::es2panda::ir { -class ClassDefinition : public JsDocAllowed> { +class ClassDefinition : public AnnotationAllowed { public: ClassDefinition() = delete; ~ClassDefinition() override = default; @@ -85,9 +84,8 @@ public: ArenaVector &&implements, MethodDefinition *ctor, Expression *superClass, ArenaVector &&body, ClassDefinitionModifiers modifiers, ModifierFlags flags, Language lang) - : JsDocAllowed>(AstNodeType::CLASS_DEFINITION, flags, - ArenaVector(body.get_allocator()), - ArenaVector(body.get_allocator())), + : AnnotationAllowed(AstNodeType::CLASS_DEFINITION, flags, + ArenaVector(body.get_allocator())), ident_(ident), typeParams_(typeParams), superTypeParams_(superTypeParams), @@ -108,7 +106,7 @@ public: // CC-OFFNXT(G.FUN.01-CPP) solid logic explicit ClassDefinition(ArenaAllocator *allocator, Identifier *ident, ArenaVector &&body, ClassDefinitionModifiers modifiers, ModifierFlags flags, Language lang) - : JsDocAllowed>(AstNodeType::CLASS_DEFINITION, flags, allocator), + : AnnotationAllowed(AstNodeType::CLASS_DEFINITION, flags, allocator), ident_(ident), implements_(allocator->Adapter()), body_(std::move(body)), @@ -125,7 +123,7 @@ public: explicit ClassDefinition(ArenaAllocator *allocator, Identifier *ident, ClassDefinitionModifiers modifiers, ModifierFlags flags, Language lang) - : JsDocAllowed>(AstNodeType::CLASS_DEFINITION, flags, allocator), + : AnnotationAllowed(AstNodeType::CLASS_DEFINITION, flags, allocator), ident_(ident), implements_(allocator->Adapter()), body_(allocator->Adapter()), @@ -144,7 +142,7 @@ public: explicit ClassDefinition(ArenaAllocator *allocator, Identifier *ident, ArenaVector &&body, ClassDefinitionModifiers modifiers, ModifierFlags flags, Language lang, AstNodeHistory *history) - : JsDocAllowed>(AstNodeType::CLASS_DEFINITION, flags, allocator), + : AnnotationAllowed(AstNodeType::CLASS_DEFINITION, flags, allocator), ident_(ident), implements_(allocator->Adapter()), body_(std::move(body)), diff --git a/ets2panda/ir/base/classProperty.cpp b/ets2panda/ir/base/classProperty.cpp index 9fb04ee715..7a0a40ed25 100644 --- a/ets2panda/ir/base/classProperty.cpp +++ b/ets2panda/ir/base/classProperty.cpp @@ -339,7 +339,7 @@ void ClassProperty::CopyTo(AstNode *other) const otherImpl->isDefault_ = isDefault_; otherImpl->needInitInStaticBlock_ = needInitInStaticBlock_; - JsDocAllowed>::CopyTo(other); + AnnotationAllowed::CopyTo(other); } } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/base/classProperty.h b/ets2panda/ir/base/classProperty.h index ebdd968dc4..539c0ccdce 100644 --- a/ets2panda/ir/base/classProperty.h +++ b/ets2panda/ir/base/classProperty.h @@ -19,7 +19,6 @@ #include "ir/base/classElement.h" #include "ir/statements/annotationUsage.h" #include "ir/annotationAllowed.h" -#include "ir/jsDocAllowed.h" namespace ark::es2panda::checker { class ETSAnalyzer; @@ -29,7 +28,7 @@ namespace ark::es2panda::ir { class Expression; class TypeNode; -class ClassProperty : public JsDocAllowed> { +class ClassProperty : public AnnotationAllowed { public: ClassProperty() = delete; ~ClassProperty() override = default; @@ -39,8 +38,7 @@ public: // CC-OFFNXT(G.FUN.01-CPP) solid logic explicit ClassProperty(Expression *const key, Expression *const value, TypeNode *const typeAnnotation, ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed) - : JsDocAllowed>(AstNodeType::CLASS_PROPERTY, key, value, modifiers, allocator, - isComputed), + : AnnotationAllowed(AstNodeType::CLASS_PROPERTY, key, value, modifiers, allocator, isComputed), typeAnnotation_(typeAnnotation) { } diff --git a/ets2panda/ir/base/scriptFunction.cpp b/ets2panda/ir/base/scriptFunction.cpp index ce9683e82c..a2bf25c587 100644 --- a/ets2panda/ir/base/scriptFunction.cpp +++ b/ets2panda/ir/base/scriptFunction.cpp @@ -110,7 +110,7 @@ void ScriptFunction::SetValueReturnStatements(ReturnStatement *returnStatements, } ScriptFunction::ScriptFunction(ArenaAllocator *allocator, ScriptFunctionData &&data) - : JsDocAllowed>(AstNodeType::SCRIPT_FUNCTION, data.flags, allocator), + : AnnotationAllowed(AstNodeType::SCRIPT_FUNCTION, data.flags, allocator), irSignature_(std::move(data.signature)), body_(data.body), funcFlags_(data.funcFlags), @@ -132,7 +132,7 @@ ScriptFunction::ScriptFunction(ArenaAllocator *allocator, ScriptFunctionData &&d } ScriptFunction::ScriptFunction(ArenaAllocator *allocator, ScriptFunctionData &&data, AstNodeHistory *history) - : JsDocAllowed>(AstNodeType::SCRIPT_FUNCTION, data.flags, allocator), + : AnnotationAllowed(AstNodeType::SCRIPT_FUNCTION, data.flags, allocator), irSignature_(std::move(data.signature)), body_(data.body), funcFlags_(data.funcFlags), @@ -398,7 +398,7 @@ void ScriptFunction::CopyTo(AstNode *other) const otherImpl->lang_ = lang_; otherImpl->returnStatements_ = returnStatements_; - JsDocAllowed>::CopyTo(other); + AnnotationAllowed::CopyTo(other); } } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/base/scriptFunction.h b/ets2panda/ir/base/scriptFunction.h index ebafe16d94..e0d169804b 100644 --- a/ets2panda/ir/base/scriptFunction.h +++ b/ets2panda/ir/base/scriptFunction.h @@ -18,7 +18,7 @@ #include "checker/types/signature.h" #include "ir/annotationAllowed.h" -#include "ir/jsDocAllowed.h" +#include "ir/statements/annotationUsage.h" #include "ir/statements/returnStatement.h" #include "ir/astNode.h" #include "scriptFunctionSignature.h" @@ -37,7 +37,7 @@ class TypeNode; class AnnotationUsage; class FunctionSignature; -class ScriptFunction : public JsDocAllowed> { +class ScriptFunction : public AnnotationAllowed { public: // Need to reduce the number of constructor parameters to pass OHOS CI code check struct ScriptFunctionData { diff --git a/ets2panda/ir/ets/etsModule.cpp b/ets2panda/ir/ets/etsModule.cpp index 147f0769e2..6e14809184 100644 --- a/ets2panda/ir/ets/etsModule.cpp +++ b/ets2panda/ir/ets/etsModule.cpp @@ -69,7 +69,7 @@ void ETSModule::CopyTo(AstNode *other) const otherImpl->program_ = program_; otherImpl->globalClass_ = globalClass_; - JsDocAllowed>::CopyTo(other); + AnnotationAllowed::CopyTo(other); } } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/ets/etsModule.h b/ets2panda/ir/ets/etsModule.h index cfa7c5a05d..abc2a5eca1 100644 --- a/ets2panda/ir/ets/etsModule.h +++ b/ets2panda/ir/ets/etsModule.h @@ -18,7 +18,6 @@ #include "ir/statements/blockStatement.h" #include "ir/annotationAllowed.h" -#include "ir/jsDocAllowed.h" #include "ir/expressions/identifier.h" #include "ir/srcDump.h" @@ -44,12 +43,12 @@ struct enumbitops::IsAllowedType : std::true_type namespace ark::es2panda::ir { -class ETSModule : public JsDocAllowed> { +class ETSModule : public AnnotationAllowed { public: // CC-OFFNXT(G.FUN.01-CPP) solid logic explicit ETSModule(ArenaAllocator *allocator, ArenaVector &&statementList, Identifier *ident, ModuleFlag flag, Language lang, parser::Program *program) - : JsDocAllowed>(allocator, std::move(statementList)), + : AnnotationAllowed(allocator, std::move(statementList)), ident_(ident), flag_(flag), lang_(lang), @@ -62,7 +61,7 @@ public: // CC-OFFNXT(G.FUN.01-CPP) solid logic explicit ETSModule(ArenaAllocator *allocator, ArenaVector &&statementList, Identifier *ident, ModuleFlag flag, Language lang, parser::Program *program, AstNodeHistory *history) - : JsDocAllowed>(allocator, std::move(statementList)), + : AnnotationAllowed(allocator, std::move(statementList)), ident_(ident), flag_(flag), lang_(lang), diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.cpp b/ets2panda/ir/expressions/arrowFunctionExpression.cpp index 73168b0bd2..e4eb06013e 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.cpp +++ b/ets2panda/ir/expressions/arrowFunctionExpression.cpp @@ -81,7 +81,7 @@ checker::VerifiedType ArrowFunctionExpression::Check(checker::ETSChecker *checke } ArrowFunctionExpression::ArrowFunctionExpression(ArrowFunctionExpression const &other, ArenaAllocator *const allocator) - : JsDocAllowed>(static_cast(other), allocator) + : AnnotationAllowed(static_cast(other), allocator) { ES2PANDA_ASSERT(other.func_->Clone(allocator, this)); func_ = other.func_->Clone(allocator, this)->AsScriptFunction(); diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.h b/ets2panda/ir/expressions/arrowFunctionExpression.h index 4289712ab0..2550cc7e0d 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.h +++ b/ets2panda/ir/expressions/arrowFunctionExpression.h @@ -18,7 +18,6 @@ #include "ir/expression.h" #include "ir/annotationAllowed.h" -#include "ir/jsDocAllowed.h" namespace ark::es2panda::compiler { class ETSCompiler; @@ -27,7 +26,7 @@ class ETSCompiler; namespace ark::es2panda::ir { class ScriptFunction; -class ArrowFunctionExpression : public JsDocAllowed> { +class ArrowFunctionExpression : public AnnotationAllowed { public: ArrowFunctionExpression() = delete; ~ArrowFunctionExpression() override = default; @@ -36,7 +35,7 @@ public: NO_MOVE_SEMANTIC(ArrowFunctionExpression); explicit ArrowFunctionExpression(ScriptFunction *const func, ArenaAllocator *const allocator) - : JsDocAllowed>(AstNodeType::ARROW_FUNCTION_EXPRESSION, allocator), func_(func) + : AnnotationAllowed(AstNodeType::ARROW_FUNCTION_EXPRESSION, allocator), func_(func) { } diff --git a/ets2panda/ir/jsDocAllowed.h b/ets2panda/ir/jsDocAllowed.h deleted file mode 100644 index 626d7bf23d..0000000000 --- a/ets2panda/ir/jsDocAllowed.h +++ /dev/null @@ -1,134 +0,0 @@ -/** - * Copyright (c) 2025 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. - */ - -#ifndef ES2PANDA_IR_JSDOC_ALLOWED_H -#define ES2PANDA_IR_JSDOC_ALLOWED_H - -#include "ir/astNode.h" -#include "ir/statement.h" - -namespace ark::es2panda::ir { - -struct JsDocRecord { - util::StringView name; // NOLINT(misc-non-private-member-variables-in-classes) - util::StringView param; // NOLINT(misc-non-private-member-variables-in-classes) - util::StringView comment; // NOLINT(misc-non-private-member-variables-in-classes) - - JsDocRecord(util::StringView initName, util::StringView initParam, util::StringView initComment) - : name(initName), param(initParam), comment(initComment) - { - } -}; - -using JsDocInfo = ArenaUnorderedMap; - -template -class JsDocAllowed : public T { -public: - JsDocAllowed() = delete; - ~JsDocAllowed() override = default; - - NO_COPY_OPERATOR(JsDocAllowed); - NO_MOVE_SEMANTIC(JsDocAllowed); - - [[nodiscard]] ArenaVector &JsDocInformation() noexcept - { - return jsDocInformation_; - } - - [[nodiscard]] const ArenaVector &JsDocInformation() const noexcept - { - return jsDocInformation_; - } - - void SetJsDocInformation(ArenaVector &&jsDocInformation) - { - jsDocInformation_ = std::move(jsDocInformation); - } - -protected: - explicit JsDocAllowed(Expression const &other, ArenaAllocator *allocator) - : T(other, allocator), jsDocInformation_(allocator->Adapter()) - { - } - - explicit JsDocAllowed(ArenaAllocator *const allocator, AstNodeType const type, - ArenaVector &&annotations) - : T(type, std::move(annotations)), jsDocInformation_(allocator->Adapter()) - { - } - - explicit JsDocAllowed(AstNodeType const type, ModifierFlags const flags, - ArenaVector &&annotations, ArenaVector &&jsDocInformation) - : T(type, flags, std::move(annotations)), jsDocInformation_(std::move(jsDocInformation)) - { - } - - explicit JsDocAllowed(AstNodeType const type, ArenaAllocator *const allocator) - : T(type, allocator), jsDocInformation_(allocator->Adapter()) - { - } - - explicit JsDocAllowed(AstNodeType const type, ModifierFlags const flags, ArenaAllocator *const allocator) - : T(type, flags, allocator), jsDocInformation_(allocator->Adapter()) - { - } - - explicit JsDocAllowed(AstNodeType const type, Expression *const key, Expression *const value, - ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed) - : T(type, key, value, modifiers, allocator, isComputed), jsDocInformation_(allocator->Adapter()) - { - } - - explicit JsDocAllowed(AstNodeType const type, TypeNode *typeAnnotation, - [[maybe_unused]] ArenaAllocator *const allocator) - : T(type, typeAnnotation), jsDocInformation_(allocator->Adapter()) - { - } - - explicit JsDocAllowed(ArenaAllocator *const allocator, AstNodeType const type) - : T(type), jsDocInformation_(allocator->Adapter()) - { - } - - explicit JsDocAllowed(ArenaAllocator *allocator, ArenaVector &&statements) - : T(allocator, std::move(statements)), jsDocInformation_(allocator->Adapter()) - { - } - - JsDocAllowed(const JsDocAllowed &other) - : T(static_cast(other)), jsDocInformation_(other.jsDocInformation_) - { - } - - JsDocAllowed(JsDocAllowed const &other, ArenaAllocator *const allocator) - : T(static_cast(other)), jsDocInformation_(allocator->Adapter()) - { - } - - void CopyTo(AstNode *other) const override - { - auto otherImpl = static_cast *>(other); - otherImpl->jsDocInformation_ = jsDocInformation_; - T::CopyTo(other); - } - -private: - friend class SizeOfNodeTest; - ArenaVector jsDocInformation_; -}; -} // namespace ark::es2panda::ir - -#endif diff --git a/ets2panda/ir/statements/functionDeclaration.cpp b/ets2panda/ir/statements/functionDeclaration.cpp index f4cbcad228..96a4c9feba 100644 --- a/ets2panda/ir/statements/functionDeclaration.cpp +++ b/ets2panda/ir/statements/functionDeclaration.cpp @@ -129,7 +129,7 @@ void FunctionDeclaration::CopyTo(AstNode *other) const otherImpl->func_ = func_; otherImpl->isAnonymous_ = isAnonymous_; - JsDocAllowed>::CopyTo(other); + AnnotationAllowed::CopyTo(other); } void FunctionDeclaration::EmplaceDecorators(Decorator *decorators) diff --git a/ets2panda/ir/statements/functionDeclaration.h b/ets2panda/ir/statements/functionDeclaration.h index 25a69766e6..264207ceba 100644 --- a/ets2panda/ir/statements/functionDeclaration.h +++ b/ets2panda/ir/statements/functionDeclaration.h @@ -17,7 +17,6 @@ #define ES2PANDA_IR_STATEMENT_FUNCTION_DECLARATION_H #include "ir/annotationAllowed.h" -#include "ir/jsDocAllowed.h" #include "ir/statement.h" #include "ir/statements/annotationUsage.h" #include "ir/base/scriptFunction.h" @@ -26,12 +25,11 @@ namespace ark::es2panda::ir { class ScriptFunction; class AnnotationUsage; -class FunctionDeclaration : public JsDocAllowed> { +class FunctionDeclaration : public AnnotationAllowed { public: explicit FunctionDeclaration(ArenaAllocator *allocator, ScriptFunction *func, ArenaVector &&annotations, bool isAnonymous = false) - : JsDocAllowed>(allocator, AstNodeType::FUNCTION_DECLARATION, - std::move(annotations)), + : AnnotationAllowed(AstNodeType::FUNCTION_DECLARATION, std::move(annotations)), decorators_(allocator->Adapter()), func_(func), isAnonymous_(isAnonymous) @@ -43,7 +41,7 @@ public: } explicit FunctionDeclaration(ArenaAllocator *allocator, ScriptFunction *func, bool isAnonymous = false) - : JsDocAllowed>(AstNodeType::FUNCTION_DECLARATION, allocator), + : AnnotationAllowed(AstNodeType::FUNCTION_DECLARATION, allocator), decorators_(allocator->Adapter()), func_(func), isAnonymous_(isAnonymous) @@ -56,7 +54,7 @@ public: explicit FunctionDeclaration(ArenaAllocator *allocator, ScriptFunction *func, bool isAnonymous, AstNodeHistory *history) - : JsDocAllowed>(AstNodeType::FUNCTION_DECLARATION, allocator), + : AnnotationAllowed(AstNodeType::FUNCTION_DECLARATION, allocator), decorators_(allocator->Adapter()), func_(func), isAnonymous_(isAnonymous) diff --git a/ets2panda/ir/statements/variableDeclaration.cpp b/ets2panda/ir/statements/variableDeclaration.cpp index fd09dc88e4..0f752dec94 100644 --- a/ets2panda/ir/statements/variableDeclaration.cpp +++ b/ets2panda/ir/statements/variableDeclaration.cpp @@ -199,8 +199,7 @@ void VariableDeclaration::Dump(ir::SrcDumper *dumper) const VariableDeclaration::VariableDeclaration([[maybe_unused]] Tag const tag, VariableDeclaration const &other, ArenaAllocator *const allocator) - : JsDocAllowed>(static_cast> const &>(other), - allocator), + : AnnotationAllowed(static_cast const &>(other)), kind_(other.kind_), decorators_(allocator->Adapter()), declarators_(allocator->Adapter()) @@ -222,8 +221,7 @@ VariableDeclaration::VariableDeclaration([[maybe_unused]] Tag const tag, Variabl VariableDeclaration::VariableDeclaration([[maybe_unused]] Tag const tag, VariableDeclaration const &other, ArenaAllocator *const allocator, AstNodeHistory *history) - : JsDocAllowed>( - static_cast> const &>(other)), + : AnnotationAllowed(static_cast const &>(other)), kind_(other.kind_), decorators_(allocator->Adapter()), declarators_(allocator->Adapter()) @@ -290,7 +288,7 @@ void VariableDeclaration::CopyTo(AstNode *other) const otherImpl->decorators_ = decorators_; otherImpl->declarators_ = declarators_; - JsDocAllowed>::CopyTo(other); + AnnotationAllowed::CopyTo(other); } } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/statements/variableDeclaration.h b/ets2panda/ir/statements/variableDeclaration.h index cb3703b27d..1e9a54a233 100644 --- a/ets2panda/ir/statements/variableDeclaration.h +++ b/ets2panda/ir/statements/variableDeclaration.h @@ -17,7 +17,6 @@ #define ES2PANDA_IR_STATEMENT_VARIABLE_DECLARATION_H #include "ir/annotationAllowed.h" -#include "ir/jsDocAllowed.h" #include "ir/statement.h" #include "ir/expressions/identifier.h" #include "ir/statements/annotationUsage.h" @@ -26,7 +25,7 @@ namespace ark::es2panda::ir { class VariableDeclarator; -class VariableDeclaration : public JsDocAllowed> { +class VariableDeclaration : public AnnotationAllowed { private: struct Tag {}; @@ -35,7 +34,7 @@ public: explicit VariableDeclaration(VariableDeclarationKind kind, ArenaAllocator *allocator, ArenaVector &&declarators) - : JsDocAllowed>(AstNodeType::VARIABLE_DECLARATION, allocator), + : AnnotationAllowed(AstNodeType::VARIABLE_DECLARATION, allocator), kind_(kind), decorators_(allocator->Adapter()), declarators_(std::move(declarators)) @@ -45,7 +44,7 @@ public: explicit VariableDeclaration(VariableDeclarationKind kind, ArenaAllocator *allocator, ArenaVector &&declarators, AstNodeHistory *history) - : JsDocAllowed>(AstNodeType::VARIABLE_DECLARATION, allocator), + : AnnotationAllowed(AstNodeType::VARIABLE_DECLARATION, allocator), kind_(kind), decorators_(allocator->Adapter()), declarators_(std::move(declarators)) diff --git a/ets2panda/ir/ts/tsEnumMember.h b/ets2panda/ir/ts/tsEnumMember.h index 566cbb1a91..32c9594331 100644 --- a/ets2panda/ir/ts/tsEnumMember.h +++ b/ets2panda/ir/ts/tsEnumMember.h @@ -17,7 +17,6 @@ #define ES2PANDA_IR_TS_ENUM_MEMBER_H #include "ir/statement.h" -#include "ir/jsDocAllowed.h" namespace ark::es2panda::ir { class Expression; diff --git a/ets2panda/ir/ts/tsInterfaceDeclaration.cpp b/ets2panda/ir/ts/tsInterfaceDeclaration.cpp index 4455a8b776..7670586186 100644 --- a/ets2panda/ir/ts/tsInterfaceDeclaration.cpp +++ b/ets2panda/ir/ts/tsInterfaceDeclaration.cpp @@ -317,7 +317,7 @@ void TSInterfaceDeclaration::CopyTo(AstNode *other) const otherImpl->lang_ = lang_; otherImpl->anonClass_ = anonClass_; - JsDocAllowed>::CopyTo(other); + AnnotationAllowed::CopyTo(other); } } // namespace ark::es2panda::ir diff --git a/ets2panda/ir/ts/tsInterfaceDeclaration.h b/ets2panda/ir/ts/tsInterfaceDeclaration.h index 217a31c5b6..327a692a5d 100644 --- a/ets2panda/ir/ts/tsInterfaceDeclaration.h +++ b/ets2panda/ir/ts/tsInterfaceDeclaration.h @@ -18,7 +18,8 @@ #include "varbinder/scope.h" #include "ir/annotationAllowed.h" -#include "ir/jsDocAllowed.h" +#include "ir/statement.h" +#include "ir/statements/annotationUsage.h" namespace ark::es2panda::varbinder { class Variable; @@ -30,7 +31,7 @@ class TSInterfaceBody; class TSInterfaceHeritage; class TSTypeParameterDeclaration; -class TSInterfaceDeclaration : public JsDocAllowed> { +class TSInterfaceDeclaration : public AnnotationAllowed { public: // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init) struct ConstructorData { @@ -45,7 +46,7 @@ public: explicit TSInterfaceDeclaration(ArenaAllocator *allocator, ArenaVector &&extends, ConstructorData &&data) - : JsDocAllowed>(AstNodeType::TS_INTERFACE_DECLARATION, allocator), + : AnnotationAllowed(AstNodeType::TS_INTERFACE_DECLARATION, allocator), decorators_(allocator->Adapter()), id_(data.id), typeParams_(data.typeParams), @@ -63,7 +64,7 @@ public: explicit TSInterfaceDeclaration(ArenaAllocator *allocator, ArenaVector &&extends, ConstructorData &&data, AstNodeHistory *history) - : JsDocAllowed>(AstNodeType::TS_INTERFACE_DECLARATION, allocator), + : AnnotationAllowed(AstNodeType::TS_INTERFACE_DECLARATION, allocator), decorators_(allocator->Adapter()), id_(data.id), typeParams_(data.typeParams), diff --git a/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp b/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp index 75532d6015..5f2276dd48 100644 --- a/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp +++ b/ets2panda/ir/ts/tsTypeAliasDeclaration.cpp @@ -195,7 +195,7 @@ void TSTypeAliasDeclaration::CopyTo(AstNode *other) const otherImpl->typeParams_ = typeParams_; otherImpl->typeParamTypes_ = typeParamTypes_; - JsDocAllowed::CopyTo(other); + AnnotatedStatement::CopyTo(other); } void TSTypeAliasDeclaration::EmplaceDecorators(Decorator *decorators) diff --git a/ets2panda/ir/ts/tsTypeAliasDeclaration.h b/ets2panda/ir/ts/tsTypeAliasDeclaration.h index fb9b8c160d..44022d21ac 100644 --- a/ets2panda/ir/ts/tsTypeAliasDeclaration.h +++ b/ets2panda/ir/ts/tsTypeAliasDeclaration.h @@ -18,7 +18,6 @@ #include "ir/statement.h" #include "ir/typed.h" -#include "ir/jsDocAllowed.h" #include "ir/statements/annotationUsage.h" namespace ark::es2panda::varbinder { @@ -29,11 +28,11 @@ namespace ark::es2panda::ir { class Identifier; class TSTypeParameterDeclaration; -class TSTypeAliasDeclaration : public JsDocAllowed { +class TSTypeAliasDeclaration : public AnnotatedStatement { public: explicit TSTypeAliasDeclaration(ArenaAllocator *allocator, Identifier *id, TSTypeParameterDeclaration *typeParams, TypeNode *typeAnnotation) - : JsDocAllowed(AstNodeType::TS_TYPE_ALIAS_DECLARATION, typeAnnotation, allocator), + : AnnotatedStatement(AstNodeType::TS_TYPE_ALIAS_DECLARATION, typeAnnotation), decorators_(allocator->Adapter()), annotations_(allocator->Adapter()), id_(id), @@ -44,7 +43,7 @@ public: } explicit TSTypeAliasDeclaration(ArenaAllocator *allocator, Identifier *id) - : JsDocAllowed(allocator, AstNodeType::TS_TYPE_ALIAS_DECLARATION), + : AnnotatedStatement(AstNodeType::TS_TYPE_ALIAS_DECLARATION), decorators_(allocator->Adapter()), annotations_(allocator->Adapter()), id_(id), diff --git a/ets2panda/lexer/ETSLexer.cpp b/ets2panda/lexer/ETSLexer.cpp index 7bf139df18..58645e09fa 100644 --- a/ets2panda/lexer/ETSLexer.cpp +++ b/ets2panda/lexer/ETSLexer.cpp @@ -106,21 +106,17 @@ bool ETSLexer::CheckUtf16Compatible(char32_t cp) const void ETSLexer::ScanAsteriskPunctuator() { GetToken().type_ = TokenType::PUNCTUATOR_MULTIPLY; - auto cp = Iterator().Peek(); - if (cp == LEX_CHAR_EQUALS) { - GetToken().type_ = TokenType::PUNCTUATOR_MULTIPLY_EQUAL; - Iterator().Forward(1); - return; - } - Iterator().Backward(1); - if (!IsValidJsDocEnd(&cp)) { - Iterator().Forward(1); - return; + switch (Iterator().Peek()) { + case LEX_CHAR_EQUALS: { + GetToken().type_ = TokenType::PUNCTUATOR_MULTIPLY_EQUAL; + Iterator().Forward(1); + break; + } + default: { + break; + } } - Iterator().Forward(JS_DOC_END_SIZE + 1); - GetToken().type_ = TokenType::JS_DOC_END; - Pos().NextTokenLine() += 1; } void ETSLexer::ConvertNumber(NumberFlags const flags) diff --git a/ets2panda/lexer/lexer.cpp b/ets2panda/lexer/lexer.cpp index f8ed59e1e6..326dcaed3c 100644 --- a/ets2panda/lexer/lexer.cpp +++ b/ets2panda/lexer/lexer.cpp @@ -17,8 +17,6 @@ #include "generated/keywords.h" -#include - namespace ark::es2panda::lexer { LexerPosition::LexerPosition(const util::StringView &source) : iterator_(source) {} @@ -122,6 +120,11 @@ LexerPosition Lexer::Save() const return pos_; } +void Lexer::Reset(size_t offset) +{ + pos_.iterator_.Reset(offset); +} + void Lexer::BackwardToken(TokenType type, size_t offset) { pos_.token_.type_ = type; @@ -136,12 +139,6 @@ void Lexer::ForwardToken(TokenType type, size_t offset) SkipWhiteSpaces(); } -void Lexer::ForwardToken(TokenType type) -{ - pos_.token_.type_ = type; - pos_.iterator_.Forward(1); -} - void Lexer::Rewind(const LexerPosition &pos) { pos_ = pos; @@ -152,11 +149,6 @@ char32_t Lexer::Lookahead() return Iterator().Peek(); } -size_t Lexer::GetIndex() -{ - return Iterator().Index(); -} - util::StringView Lexer::SourceView(const util::StringView::Iterator &begin, const util::StringView::Iterator &end) const { return SourceView(begin.Index(), end.Index()); @@ -977,20 +969,17 @@ void Lexer::ScanMinusPunctuator() void Lexer::ScanSlashPunctuator() { GetToken().type_ = TokenType::PUNCTUATOR_DIVIDE; - auto cp = Iterator().Peek(); - if (cp == LEX_CHAR_EQUALS) { - GetToken().type_ = TokenType::PUNCTUATOR_DIVIDE_EQUAL; - Iterator().Forward(1); - } - Iterator().Backward(1); - if (!IsValidJsDocStart(&cp)) { - Iterator().Forward(1); - return; + switch (Iterator().Peek()) { + case LEX_CHAR_EQUALS: { + GetToken().type_ = TokenType::PUNCTUATOR_DIVIDE_EQUAL; + Iterator().Forward(1); + break; + } + default: { + break; + } } - Iterator().Forward(JS_DOC_START_SIZE + 1); - GetToken().type_ = TokenType::JS_DOC_START; - pos_.nextTokenLine_ += 1; } void Lexer::ScanDotPunctuator(KeywordsUtil &kwu) @@ -1293,45 +1282,6 @@ bool Lexer::SkipWhiteSpacesHelperSlash(char32_t *cp) return false; } -bool Lexer::IsEnableParseJsdoc() const -{ - return parserContext_->IsEnableJsdocParse(); -} - -bool Lexer::IsValidJsDocStart(char32_t *cp) -{ - if (!IsEnableParseJsdoc()) { - return false; - } - - for (size_t idx = 0; idx < JS_DOC_START_SIZE; ++idx) { - Iterator().Forward(1); - *cp = Iterator().Peek(); - if (*cp != JS_DOC_START_LEX[idx]) { - Iterator().Backward(idx + 1); - return false; - } - } - - Iterator().Backward(JS_DOC_START_SIZE); - return true; -} - -bool Lexer::IsValidJsDocEnd(char32_t *cp) -{ - for (size_t idx = 0; idx < JS_DOC_END_SIZE; ++idx) { - Iterator().Forward(1); - *cp = Iterator().Peek(); - if (*cp != JS_DOC_END_LEX[idx]) { - Iterator().Backward(idx + 1); - return false; - } - } - - Iterator().Backward(JS_DOC_END_SIZE); - return true; -} - bool Lexer::SkipWhiteSpacesHelperDefault(const char32_t &cp) { if (cp < LEX_ASCII_MAX_BITS) { @@ -1390,10 +1340,6 @@ void Lexer::SkipWhiteSpaces() Iterator().Forward(1); continue; case LEX_CHAR_SLASH: - if ((GetContext()->Status() & parser::ParserStatus::ALLOW_JS_DOC_START) != 0 && - IsValidJsDocStart(&cp)) { - return; - } if (!SkipWhiteSpacesHelperSlash(&cp)) { return; } @@ -1786,15 +1732,6 @@ void Lexer::FinalizeTokenHelper(util::UString *str, const size_t &startPos, size } } -void Lexer::FinalizeJsDocInfoHelper(util::UString *str, const size_t &startPos, size_t escapeEnd) -{ - if ((GetToken().flags_ & TokenFlags::HAS_ESCAPE) != 0U) { - str->Append(SourceView(escapeEnd, Iterator().Index())); - } else { - str->Append(SourceView(startPos, Iterator().Index())); - } -} - LexerPosition &Lexer::Pos() { return pos_; diff --git a/ets2panda/lexer/lexer.h b/ets2panda/lexer/lexer.h index 47c03ba3d9..2b6ab9d293 100644 --- a/ets2panda/lexer/lexer.h +++ b/ets2panda/lexer/lexer.h @@ -124,7 +124,6 @@ public: // NOLINTNEXTLINE(google-default-arguments) virtual void NextToken(NextTokenFlags flags = NextTokenFlags::NONE); virtual void ScanAsteriskPunctuator(); - bool IsEnableParseJsdoc() const; Token &GetToken(); const Token &GetToken() const; @@ -151,11 +150,6 @@ public: return false; } - void SkipCp() - { - Iterator().SkipCp(); - } - util::DiagnosticEngine &DiagnosticEngine() { return diagnosticEngine_; @@ -173,9 +167,9 @@ public: LexerPosition Save() const; void Rewind(const LexerPosition &pos); + void Reset(size_t offset); void BackwardToken(TokenType type, size_t offset); void ForwardToken(TokenType type, size_t offset); - void ForwardToken(TokenType type); char32_t Lookahead(); bool CheckArrow(); @@ -188,7 +182,6 @@ public: bool HandleDoubleQuoteHelper(const char32_t &end, const char32_t &cp); void PrepareStringTokenHelper(); void FinalizeTokenHelper(util::UString *str, const size_t &startPos, size_t escapeEnd, bool finalize = true); - void FinalizeJsDocInfoHelper(util::UString *str, const size_t &startPos, size_t escapeEnd); template void ScanString(); @@ -262,8 +255,6 @@ public: return GetToken().Start(); } - size_t GetIndex(); - protected: void NextToken(Keywords *kws); ArenaAllocator *Allocator(); @@ -294,8 +285,6 @@ protected: util::StringView SourceView(const util::StringView::Iterator &begin, const util::StringView::Iterator &end) const; bool SkipWhiteSpacesHelperSlash(char32_t *cp); - bool IsValidJsDocStart(char32_t *cp); - bool IsValidJsDocEnd(char32_t *cp); bool SkipWhiteSpacesHelperDefault(const char32_t &cp); void SkipWhiteSpaces(); void SkipSingleLineComment(); @@ -379,10 +368,6 @@ private: util::StringView source_; LexerPosition pos_; util::DiagnosticEngine &diagnosticEngine_; - const parser::ParserContext *GetContext() - { - return parserContext_; - } }; class TemplateLiteralParserContext { diff --git a/ets2panda/lexer/templates/token.inl.erb b/ets2panda/lexer/templates/token.inl.erb index 105251067d..1b90b38032 100644 --- a/ets2panda/lexer/templates/token.inl.erb +++ b/ets2panda/lexer/templates/token.inl.erb @@ -32,10 +32,6 @@ const char *TokenToString(TokenType type) // NOLINT(readability-function-size) return "string literal"; case TokenType::LITERAL_CHAR: return "char literal"; - case TokenType::JS_DOC_START: - return "/**"; - case TokenType::JS_DOC_END: - return "*/"; case TokenType::EOS: return "end of stream"; default: diff --git a/ets2panda/lexer/templates/tokenType.h.erb b/ets2panda/lexer/templates/tokenType.h.erb index a1f944cbec..d02424b582 100644 --- a/ets2panda/lexer/templates/tokenType.h.erb +++ b/ets2panda/lexer/templates/tokenType.h.erb @@ -34,8 +34,6 @@ enum class TokenType { % end <%= token.token %>, % end - JS_DOC_START, - JS_DOC_END, FIRST_PUNCTUATOR = <%= Tokens::tokens[0].token %>, FIRST_KEYW = <%= Keywords::keywords[0].token %> }; diff --git a/ets2panda/lexer/token/letters.h b/ets2panda/lexer/token/letters.h index e33d21b114..e55574600d 100644 --- a/ets2panda/lexer/token/letters.h +++ b/ets2panda/lexer/token/letters.h @@ -156,13 +156,6 @@ inline constexpr char32_t LEX_UTF8_EXTRA_BYTE_MASK = 0xC0; inline constexpr char32_t LEX_CHAR_EOS = 0xFFFF; inline constexpr char32_t UNICODE_CODE_POINT_MAX = 0x10FFFF; inline constexpr char32_t UNICODE_INVALID_CP = UINT32_MAX; - -inline constexpr size_t JS_DOC_START_SIZE = 3; -inline constexpr size_t JS_DOC_END_SIZE = 2; -// NOLINTBEGIN(modernize-avoid-c-arrays) -inline constexpr char32_t JS_DOC_START_LEX[JS_DOC_START_SIZE] = {LEX_CHAR_ASTERISK, LEX_CHAR_ASTERISK, LEX_CHAR_LF}; -inline constexpr char32_t JS_DOC_END_LEX[JS_DOC_END_SIZE] = {LEX_CHAR_SLASH, LEX_CHAR_LF}; -// NOLINTEND(modernize-avoid-c-arrays) } // namespace ark::es2panda::lexer #endif diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 7087699b14..73ab94b616 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -102,9 +102,7 @@ bool ETSParser::IsETSParser() const noexcept std::unique_ptr ETSParser::InitLexer(const SourceFile &sourceFile) { GetProgram()->SetSource(sourceFile); - GetContext().Status() |= ParserStatus::ALLOW_JS_DOC_START; auto lexer = std::make_unique(&GetContext(), DiagnosticEngine()); - GetContext().Status() ^= ParserStatus::ALLOW_JS_DOC_START; SetLexer(lexer.get()); return lexer; } @@ -193,10 +191,6 @@ ir::ETSModule *ETSParser::ParseImportsAndReExportOnly(lexer::SourcePosition star ETSNolintParser etsnolintParser(this); etsnolintParser.CollectETSNolints(); - if (Lexer()->TryEatTokenType(lexer::TokenType::JS_DOC_START)) { - // Note: Not Support JS_DOC for Import declaration now, just go on; - ParseJsDocInfos(); - } auto imports = ParseImportDeclarations(); statements.insert(statements.end(), imports.begin(), imports.end()); etsnolintParser.ApplyETSNolintsToStatements(statements); @@ -801,10 +795,6 @@ ir::TSTypeAliasDeclaration *ETSParser::ParseTypeAliasDeclaration() ES2PANDA_ASSERT(Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_TYPE); const auto start = Lexer()->Save(); - - auto newStatus = GetContext().Status(); - newStatus &= ~ParserStatus::ALLOW_JS_DOC_START; - SavedParserContext savedContext(this, newStatus); lexer::SourcePosition typeStart = Lexer()->GetToken().Start(); Lexer()->NextToken(); // eat type keyword @@ -1201,11 +1191,6 @@ ir::Statement *ETSParser::ParseExport(lexer::SourcePosition startLoc, ir::Modifi ir::ETSPackageDeclaration *ETSParser::ParsePackageDeclaration() { auto startLoc = Lexer()->GetToken().Start(); - auto savedPos = Lexer()->Save(); - if (Lexer()->TryEatTokenType(lexer::TokenType::JS_DOC_START)) { - // Note: Not Support JS_DOC for Package declaration now, just go on; - ParseJsDocInfos(); - } if (!Lexer()->TryEatTokenType(lexer::TokenType::KEYW_PACKAGE)) { // NOTE(vpukhov): the *unnamed* modules are to be removed entirely @@ -1213,7 +1198,6 @@ ir::ETSPackageDeclaration *ETSParser::ParsePackageDeclaration() util::StringView moduleName = isUnnamed ? "" : importPathManager_->FormModuleName(GetProgram()->SourceFile(), startLoc); GetProgram()->SetPackageInfo(moduleName, util::ModuleKind::MODULE); - Lexer()->Rewind(savedPos); return nullptr; } @@ -1285,15 +1269,6 @@ ir::ETSImportDeclaration *ETSParser::BuildImportDeclaration(ir::ImportKinds impo std::move(specifiers), importKind); } -lexer::LexerPosition ETSParser::HandleJsDocLikeComments() -{ - auto savedPos = Lexer()->Save(); - if (Lexer()->TryEatTokenType(lexer::TokenType::JS_DOC_START)) { - ParseJsDocInfos(); - } - return savedPos; -} - ArenaVector ETSParser::ParseETSInitModuleStatements() { std::vector userPaths; @@ -1306,9 +1281,9 @@ ArenaVector ETSParser::ParseETSInitModuleStatements() ArenaVector ETSParser::ParseImportDeclarations() { - auto savedPos = HandleJsDocLikeComments(); std::vector userPaths; ArenaVector statements(Allocator()->Adapter()); + while (Lexer()->GetToken().Type() == lexer::TokenType::KEYW_IMPORT) { auto startLoc = Lexer()->GetToken().Start(); Lexer()->NextToken(); // eat import @@ -1348,11 +1323,8 @@ ArenaVector ETSParser::ParseImportDeclarations() statements.push_back(importDeclDefault->AsETSImportDeclaration()); } } - - savedPos = HandleJsDocLikeComments(); } - Lexer()->Rewind(savedPos); std::sort(statements.begin(), statements.end(), [](const auto *s1, const auto *s2) -> bool { return s1->Specifiers()[0]->IsImportNamespaceSpecifier() && !s2->Specifiers()[0]->IsImportNamespaceSpecifier(); }); @@ -2328,7 +2300,7 @@ ir::FunctionDeclaration *ETSParser::ParseFunctionDeclaration(bool canBeAnonymous ES2PANDA_ASSERT(Lexer()->GetToken().Type() == lexer::TokenType::KEYW_FUNCTION); Lexer()->NextToken(); - auto newStatus = ParserStatus::NEED_RETURN_TYPE | ParserStatus::ALLOW_SUPER | ParserStatus::ALLOW_JS_DOC_START; + auto newStatus = ParserStatus::NEED_RETURN_TYPE | ParserStatus::ALLOW_SUPER; if ((modifiers & ir::ModifierFlags::ASYNC) != 0) { newStatus |= ParserStatus::ASYNC_FUNCTION; diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index d52a0f8706..a62879f8b9 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -71,7 +71,6 @@ public: parser::Program *newProg) const; bool CheckDupAndReplace(Program *&oldProg, Program *newProg); ArenaVector ParseDefaultSources(std::string_view srcFile, std::string_view importSrc); - lexer::LexerPosition HandleJsDocLikeComments(); public: //============================================================================================// @@ -152,7 +151,6 @@ private: ir::Identifier *ParseIdentifierFormatPlaceholder(std::optional nodeFormat) override; ir::TypeNode *ParseTypeFormatPlaceholder(std::optional nodeFormat = std::nullopt); ir::AstNode *ParseTypeParametersFormatPlaceholder() override; - void ApplyJsDocInfoToSpecificNodeType(ir::AstNode *node, ArenaVector &&jsDocInformation); void ApplyAnnotationsToArrayType(ir::AstNode *node, ArenaVector &&annotations, lexer::SourcePosition pos); void ApplyAnnotationsToSpecificNodeType(ir::AstNode *node, ArenaVector &&annotations, @@ -220,7 +218,6 @@ private: ir::AstNode *ParseImportDefaultSpecifier(ArenaVector *specifiers) override; void *ApplyAnnotationsToClassElement(ir::AstNode *property, ArenaVector &&annotations, lexer::SourcePosition pos); - void ApplyJsDocInfoToClassElement(ir::AstNode *property, ArenaVector &&jsDocInformation); ir::MethodDefinition *ParseClassGetterSetterMethod(const ArenaVector &properties, ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags memberModifiers, bool isDefault); @@ -315,7 +312,6 @@ private: const lexer::SourcePosition &startLoc) override; bool IsFieldStartToken(lexer::TokenType t); ir::AstNode *ParseTypeLiteralOrInterfaceMember() override; - ir::AstNode *ParseJsDocInfoInInterfaceBody(); ir::AstNode *ParseAnnotationsInInterfaceBody(); void ParseNameSpaceSpecifier(ArenaVector *specifiers, bool isReExport = false); void CheckModuleAsModifier(); @@ -355,10 +351,6 @@ private: // NOLINTNEXTLINE(google-default-arguments) ir::Statement *ParseStructStatement(StatementParsingFlags flags, ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags modFlags = ir::ModifierFlags::NONE) override; - ir::AstNode *ParseClassElementHelper( - const ArenaVector &properties, - std::tuple modifierInfo, - std::tuple elementFlag, std::tuple posInfo); ir::AstNode *ParseClassElement(const ArenaVector &properties, ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags) override; std::tuple HandleClassElementModifiers(ir::ModifierFlags &memberModifiers); @@ -452,12 +444,6 @@ private: void ParseTrailingBlock([[maybe_unused]] ir::CallExpression *callExpr) override; void CheckDeclare(); - void ExcludeInvalidStart(); - std::tuple ParseJsDocInfoItemValue(); - std::string ParseJsDocInfoItemParam(); - ir::JsDocInfo ParseJsDocInfo(); - ArenaVector ParseJsDocInfos(); - friend class ExternalSourceParser; friend class InnerSourceParser; diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index ac0f510869..d930394c1d 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -677,16 +677,29 @@ std::tuple ETSParser::HandleClassElementModifiers(ir::Modifier return {seenStatic, isStepToken, isDefault}; } -ir::AstNode *ETSParser::ParseClassElementHelper( - const ArenaVector &properties, - std::tuple modifierInfo, - std::tuple elementFlag, std::tuple posInfo) +ir::AstNode *ETSParser::ParseClassElement(const ArenaVector &properties, + ir::ClassDefinitionModifiers modifiers, + [[maybe_unused]] ir::ModifierFlags flags) { - auto [seenStatic, isStepToken, isDefault] = elementFlag; - auto [startLoc, savedPos] = posInfo; - auto [modifiers, memberModifiers, flags] = modifierInfo; - auto delcStartLoc = Lexer()->GetToken().Start(); + auto startLoc = Lexer()->GetToken().Start(); + + ArenaVector annotations(Allocator()->Adapter()); + if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_AT)) { + annotations = ParseAnnotations(false); + } + + ir::ModifierFlags memberModifiers = ir::ModifierFlags::NONE; + auto savedPos = Lexer()->Save(); // NOLINT(clang-analyzer-deadcode.DeadStores) + + if (Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_STATIC && + Lexer()->Lookahead() == lexer::LEX_CHAR_LEFT_BRACE) { + return ParseClassStaticBlock(); + } + + auto [seenStatic, isStepToken, isDefault] = HandleClassElementModifiers(memberModifiers); + ir::AstNode *result = nullptr; + auto delcStartLoc = Lexer()->GetToken().Start(); switch (Lexer()->GetToken().Type()) { case lexer::TokenType::KEYW_INTERFACE: case lexer::TokenType::KEYW_CLASS: @@ -715,45 +728,9 @@ ir::AstNode *ETSParser::ParseClassElementHelper( } default: { result = ParseInnerRest(properties, modifiers, memberModifiers, startLoc, isDefault); + break; } } - return result; -} - -ir::AstNode *ETSParser::ParseClassElement(const ArenaVector &properties, - ir::ClassDefinitionModifiers modifiers, - [[maybe_unused]] ir::ModifierFlags flags) -{ - ArenaVector jsDocInformation(Allocator()->Adapter()); - if (Lexer()->TryEatTokenType(lexer::TokenType::JS_DOC_START)) { - jsDocInformation = ParseJsDocInfos(); - if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_RIGHT_BRACE || - Lexer()->GetToken().Type() == lexer::TokenType::EOS) { - return nullptr; - } - } - - auto startLoc = Lexer()->GetToken().Start(); - - ArenaVector annotations(Allocator()->Adapter()); - if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_AT)) { - annotations = ParseAnnotations(false); - } - - ir::ModifierFlags memberModifiers = ir::ModifierFlags::NONE; - auto savedPos = Lexer()->Save(); // NOLINT(clang-analyzer-deadcode.DeadStores) - - if (Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_STATIC && - Lexer()->Lookahead() == lexer::LEX_CHAR_LEFT_BRACE) { - return ParseClassStaticBlock(); - } - - auto [seenStatic, isStepToken, isDefault] = HandleClassElementModifiers(memberModifiers); - auto delcStartLoc = Lexer()->GetToken().Start(); - ir::AstNode *result = ParseClassElementHelper(properties, std::make_tuple(modifiers, memberModifiers, flags), - std::make_tuple(seenStatic, isStepToken, isDefault), - std::make_tuple(startLoc, savedPos)); - ApplyJsDocInfoToClassElement(result, std::move(jsDocInformation)); ApplyAnnotationsToClassElement(result, std::move(annotations), delcStartLoc); return result; } @@ -786,41 +763,6 @@ void *ETSParser::ApplyAnnotationsToClassElement(ir::AstNode *property, ArenaVect return property; } -static ir::JsDocInfo CloneJsDocInfo(ArenaAllocator *const allocator, const ir::JsDocInfo &src) -{ - ir::JsDocInfo res(allocator->Adapter()); - for (const auto &entry : src) { - const util::StringView &key = entry.first; - const ir::JsDocRecord &record = entry.second; - - util::UString copiedKey {key, allocator}; - util::UString copiedParam {record.param, allocator}; - util::UString copiedComment {record.comment, allocator}; - res.emplace(copiedKey.View(), ir::JsDocRecord(copiedKey.View(), copiedParam.View(), copiedComment.View())); - } - return res; -} - -void ETSParser::ApplyJsDocInfoToClassElement(ir::AstNode *property, ArenaVector &&jsDocInformation) -{ - if (property == nullptr || jsDocInformation.empty()) { - return; - } - - if (!property->IsTSInterfaceBody()) { - ApplyJsDocInfoToSpecificNodeType(property, std::move(jsDocInformation)); - return; - } - - for (auto *node : property->AsTSInterfaceBody()->Body()) { - ArenaVector clonedJsDocInformation(Allocator()->Adapter()); - for (auto const &jsdocInfo : jsDocInformation) { - clonedJsDocInformation.emplace_back(CloneJsDocInfo(Allocator(), jsdocInfo)); - } - ApplyJsDocInfoToSpecificNodeType(node, std::move(clonedJsDocInformation)); - } -} - ir::MethodDefinition *ETSParser::ParseClassGetterSetterMethod(const ArenaVector &properties, const ir::ClassDefinitionModifiers modifiers, const ir::ModifierFlags memberModifiers, bool isDefault) @@ -1251,23 +1193,6 @@ ir::MethodDefinition *ETSParser::ParseInterfaceMethod(ir::ModifierFlags flags, i return method; } -ir::AstNode *ETSParser::ParseJsDocInfoInInterfaceBody() -{ - Lexer()->NextToken(); // eat '/**' - - auto jsDocInformation = ParseJsDocInfos(); - if (Lexer()->GetToken().Type() == lexer::TokenType::EOS || - Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_RIGHT_BRACE) { - return nullptr; - } - - ir::AstNode *result = ParseTypeLiteralOrInterfaceMember(); - if (result != nullptr) { - ApplyJsDocInfoToSpecificNodeType(result, std::move(jsDocInformation)); - } - return result; -} - ir::AstNode *ETSParser::ParseAnnotationsInInterfaceBody() { Lexer()->NextToken(); // eat '@' @@ -1290,10 +1215,6 @@ bool ETSParser::IsFieldStartToken(lexer::TokenType tokenType) ir::AstNode *ETSParser::ParseTypeLiteralOrInterfaceMember() { - if (Lexer()->GetToken().Type() == lexer::TokenType::JS_DOC_START) { - return ParseJsDocInfoInInterfaceBody(); - } - if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_AT) { return ParseAnnotationsInInterfaceBody(); } diff --git a/ets2panda/parser/ETSparserEnums.cpp b/ets2panda/parser/ETSparserEnums.cpp index 56be9b2749..1b929fac1f 100644 --- a/ets2panda/parser/ETSparserEnums.cpp +++ b/ets2panda/parser/ETSparserEnums.cpp @@ -250,7 +250,6 @@ lexer::SourcePosition ETSParser::ParseEnumMember(ArenaVector &mem // Lambda to parse enum member (maybe with initializer) auto const parseMember = [this, &members, ¤tNumberExpr]() { - HandleJsDocLikeComments(); auto *const ident = ExpectIdentifier(false, true); ir::Expression *ordinal; diff --git a/ets2panda/parser/ETSparserJsDocInfo.cpp b/ets2panda/parser/ETSparserJsDocInfo.cpp deleted file mode 100644 index e0dbcb2344..0000000000 --- a/ets2panda/parser/ETSparserJsDocInfo.cpp +++ /dev/null @@ -1,277 +0,0 @@ -/** - * Copyright (c) 2021-2025 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. - */ - -#include "ETSparser.h" -#include "lexer/lexer.h" -#include "lexer/keywordsUtil.h" -#include "ir/astNode.h" -#include "ir/base/methodDefinition.h" -#include "ir/ets/etsStructDeclaration.h" -#include "ir/statements/classDeclaration.h" -#include -#include -#include -#include -#include -#include -#include - -namespace ark::es2panda::parser { -void ETSParser::ExcludeInvalidStart() -{ - auto cp = Lexer()->Lookahead(); - while (!lexer::KeywordsUtil::IsIdentifierStart(cp) && cp != util::StringView::Iterator::INVALID_CP && - cp != lexer::LEX_CHAR_LEFT_BRACE && cp != lexer::LEX_CHAR_ASTERISK) { - Lexer()->ForwardToken(Lexer()->GetToken().Type()); - cp = Lexer()->Lookahead(); - } -} - -std::string ETSParser::ParseJsDocInfoItemParam() -{ - ExcludeInvalidStart(); - Lexer()->NextToken(); - auto token = Lexer()->GetToken(); - std::string jsDocInfoParamStr {}; - bool needAppendToken = token.Type() != lexer::TokenType::PUNCTUATOR_LEFT_BRACE; - bool needBackwardBlank = lexer::KeywordsUtil::IsIdentifierStart(Lexer()->Lookahead()); - size_t leftBraceCount = 1; - while (token.Type() != lexer::TokenType::EOS && token.Type() != lexer::TokenType::JS_DOC_END) { - if (needAppendToken) { - jsDocInfoParamStr += token.ToString(); - } - - if (needBackwardBlank) { - jsDocInfoParamStr += " "; - } - - auto cp = Lexer()->Lookahead(); - if (lexer::KeywordsUtil::IsIdentifierStart(cp) || cp == lexer::LEX_CHAR_ASTERISK) { - Lexer()->NextToken(); - token = Lexer()->GetToken(); - needAppendToken = lexer::KeywordsUtil::IsIdentifierStart(cp); - needBackwardBlank = lexer::KeywordsUtil::IsIdentifierStart(Lexer()->Lookahead()); - continue; - } - - if (cp == lexer::LEX_CHAR_RIGHT_BRACE) { - --leftBraceCount; - if (leftBraceCount == 0) { - Lexer()->NextToken(); - break; - } - } - - if (cp == lexer::LEX_CHAR_LEFT_BRACE) { - ++leftBraceCount; - } - - if (cp == util::StringView::Iterator::INVALID_CP) { - break; - } - - jsDocInfoParamStr += std::string(1, cp); - needAppendToken = false; - needBackwardBlank = false; - Lexer()->ForwardToken(token.Type()); - } - - return jsDocInfoParamStr; -} - -static void RegularCommentStr(std::string &str) -{ - if (str.empty()) { - return; - } - - auto backChar = static_cast(static_cast(str.back())); - while (backChar == lexer::LEX_CHAR_CR || backChar == lexer::LEX_CHAR_LF || backChar == lexer::LEX_CHAR_ASTERISK || - backChar == lexer::LEX_CHAR_NBSP || backChar == lexer::LEX_CHAR_SP) { - str.pop_back(); - if (str.empty()) { - return; - } - backChar = static_cast(static_cast(str.back())); - } -} - -std::tuple ETSParser::ParseJsDocInfoItemValue() -{ - ExcludeInvalidStart(); - util::UString jsDocInfoItemCommentStr(Allocator()); - std::string jsDocInfoParamStr {}; - if (Lexer()->GetToken().Type() == lexer::TokenType::JS_DOC_END) { - util::UString jsDocInfoItemParam {jsDocInfoParamStr, Allocator()}; - return std::make_tuple(jsDocInfoItemParam.View(), jsDocInfoItemCommentStr.View()); - } - - const auto startIdx = Lexer()->GetIndex(); - auto escapeEnd = startIdx; - do { - const char32_t cp = Lexer()->Lookahead(); - switch (cp) { - case lexer::LEX_CHAR_LEFT_BRACE: { - jsDocInfoParamStr = ParseJsDocInfoItemParam(); - continue; - } - case util::StringView::Iterator::INVALID_CP: { - break; - } - case lexer::LEX_CHAR_CR: - case lexer::LEX_CHAR_LF: { - Lexer()->HandleNewlineHelper(&jsDocInfoItemCommentStr, &escapeEnd); - continue; - } - case lexer::LEX_CHAR_AT: - case lexer::LEX_CHAR_ASTERISK: { - auto saved = Lexer()->Save(); - Lexer()->NextToken(); - auto nextType = Lexer()->GetToken().Type(); - Lexer()->Rewind(saved); - if (nextType == lexer::TokenType::JS_DOC_END || nextType == lexer::TokenType::PUNCTUATOR_AT) { - break; - } - [[fallthrough]]; - } - default: { - Lexer()->SkipCp(); - continue; - } - } - Lexer()->FinalizeJsDocInfoHelper(&jsDocInfoItemCommentStr, startIdx, escapeEnd); - break; - } while (true); - - std::string commentStr = std::string(jsDocInfoItemCommentStr.View()); - RegularCommentStr(commentStr); - util::UString jsDocInfoItemParam {jsDocInfoParamStr, Allocator()}; - util::UString jsDocInfoCommentStr {commentStr, Allocator()}; - return std::make_tuple(jsDocInfoItemParam.View(), jsDocInfoCommentStr.View()); -} - -ir::JsDocInfo ETSParser::ParseJsDocInfo() -{ - ir::JsDocInfo jsDocInfo(Allocator()->Adapter()); - auto tokenType = Lexer()->GetToken().Type(); - while (tokenType != lexer::TokenType::EOS && tokenType != lexer::TokenType::JS_DOC_END) { - if (tokenType != lexer::TokenType::PUNCTUATOR_AT) { - auto cp = Lexer()->Lookahead(); - if (cp == lexer::LEX_CHAR_ASTERISK || cp == lexer::LEX_CHAR_AT) { - Lexer()->NextToken(); - tokenType = Lexer()->GetToken().Type(); - continue; - } - - if (Lexer()->Lookahead() == util::StringView::Iterator::INVALID_CP) { - break; - } - - Lexer()->ForwardToken(tokenType, 1); - continue; - } - Lexer()->NextToken(); - auto const &token = Lexer()->GetToken(); - util::UString jsDocInfoItemKey {token.Ident(), Allocator()}; - auto [jsDocInfoItemParam, jsDocInfoItemComment] = ParseJsDocInfoItemValue(); - jsDocInfo.emplace(jsDocInfoItemKey.View(), - ir::JsDocRecord(jsDocInfoItemKey.View(), jsDocInfoItemParam, jsDocInfoItemComment)); - tokenType = Lexer()->GetToken().Type(); - } - - Lexer()->NextToken(); - return jsDocInfo; -} - -ArenaVector ETSParser::ParseJsDocInfos() -{ - ArenaVector result(Allocator()->Adapter()); - bool hasMoreJsDocInfos = true; - while (hasMoreJsDocInfos) { - auto jsDocInfo = ParseJsDocInfo(); - if (!jsDocInfo.empty()) { - result.emplace_back(jsDocInfo); - } - - if (Lexer()->GetToken().Type() != lexer::TokenType::JS_DOC_START) { - hasMoreJsDocInfos = false; - } - } - return result; -} - -static bool ApplyJsDocInfoToNamespace(ir::ETSModule *ns, ArenaVector &jsDocInformation) -{ - if (ns->IsNamespaceChainLastNode()) { - ns->SetJsDocInformation(std::move(jsDocInformation)); - return true; - } - - for (auto *node : ns->Statements()) { - if (node->IsETSModule()) { - if (ApplyJsDocInfoToNamespace(node->AsETSModule(), jsDocInformation)) { - return true; - } - } - } - return false; -} - -// CC-OFFNXT(huge_method,huge_cyclomatic_complexity,G.FUN.01-CPP) big switch-case, solid logic -void ETSParser::ApplyJsDocInfoToSpecificNodeType(ir::AstNode *node, ArenaVector &&jsDocInformation) -{ - if (jsDocInformation.empty()) { - return; - } - - switch (node->Type()) { - case ir::AstNodeType::METHOD_DEFINITION: { - auto *func = node->AsMethodDefinition()->Function(); - ES2PANDA_ASSERT(func != nullptr); - func->SetJsDocInformation(std::move(jsDocInformation)); - break; - } - case ir::AstNodeType::CLASS_DECLARATION: - node->AsClassDeclaration()->Definition()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::STRUCT_DECLARATION: - node->AsETSStructDeclaration()->Definition()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::FUNCTION_DECLARATION: - node->AsFunctionDeclaration()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::TS_INTERFACE_DECLARATION: - node->AsTSInterfaceDeclaration()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::CLASS_PROPERTY: - node->AsClassProperty()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::VARIABLE_DECLARATION: - node->AsVariableDeclaration()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION: - node->AsTSTypeAliasDeclaration()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::ARROW_FUNCTION_EXPRESSION: - node->AsArrowFunctionExpression()->SetJsDocInformation(std::move(jsDocInformation)); - break; - case ir::AstNodeType::ETS_MODULE: - ApplyJsDocInfoToNamespace(node->AsETSModule(), jsDocInformation); - break; - default: { - } - } -} -} // namespace ark::es2panda::parser diff --git a/ets2panda/parser/ETSparserNamespaces.cpp b/ets2panda/parser/ETSparserNamespaces.cpp index 807b70099f..0bb77dc0c3 100644 --- a/ets2panda/parser/ETSparserNamespaces.cpp +++ b/ets2panda/parser/ETSparserNamespaces.cpp @@ -102,9 +102,7 @@ ir::ETSModule *ETSParser::ParseNamespaceImp(ir::ModifierFlags flags) continue; } auto st = ParseTopLevelStatement(); - if (st != nullptr) { - statements.emplace_back(st); - } + statements.emplace_back(st); } Lexer()->NextToken(); if (child != nullptr) { diff --git a/ets2panda/parser/ETSparserStatements.cpp b/ets2panda/parser/ETSparserStatements.cpp index 18393fa47f..1ca8f39129 100644 --- a/ets2panda/parser/ETSparserStatements.cpp +++ b/ets2panda/parser/ETSparserStatements.cpp @@ -241,23 +241,8 @@ ir::Statement *ETSParser::ParseTopLevelDeclStatement(StatementParsingFlags flags ir::Statement *ETSParser::ParseTopLevelStatement() { const auto flags = StatementParsingFlags::ALLOW_LEXICAL; - ArenaVector jsDocInformation(Allocator()->Adapter()); - if (Lexer()->TryEatTokenType(lexer::TokenType::JS_DOC_START)) { - jsDocInformation = ParseJsDocInfos(); - } - if (Lexer()->GetToken().Type() == lexer::TokenType::EOS || - ((GetContext().Status() & ParserStatus::IN_NAMESPACE) != 0 && - Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_RIGHT_BRACE)) { - return nullptr; - } - GetContext().Status() |= ParserStatus::ALLOW_JS_DOC_START; auto result = ParseTopLevelDeclStatement(flags); - GetContext().Status() ^= ParserStatus::ALLOW_JS_DOC_START; - if (result != nullptr) { - ApplyJsDocInfoToSpecificNodeType(result, std::move(jsDocInformation)); - } - if (result == nullptr) { result = ParseStatement(flags); } diff --git a/ets2panda/parser/JsdocHelper.cpp b/ets2panda/parser/JsdocHelper.cpp new file mode 100644 index 0000000000..1d9f34cb33 --- /dev/null +++ b/ets2panda/parser/JsdocHelper.cpp @@ -0,0 +1,199 @@ +/** + * Copyright (c) 2025 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. + */ + +#include "JsdocHelper.h" +#include +#include "lexer/lexer.h" +#include "ir/ets/etsTuple.h" +#include "ir/statements/annotationDeclaration.h" + +namespace ark::es2panda::parser { +static constexpr std::string_view JSDOC_END = "*/"; +static constexpr std::string_view JSDOC_START = "/**"; +static constexpr std::string_view LICENSES_START = "/*"; +static constexpr std::string_view EMPTY_JSDOC = "Empty Jsdoc"; +static constexpr std::string_view EMPTY_LICENSE = "Empty License"; + +static constexpr size_t START_POS = 0; +static constexpr size_t COLLECT_CURRENT_POS = 1; + +// NOLINTBEGIN(modernize-avoid-c-arrays) +static constexpr std::string_view POTENTIAL_PREFIX[] = {"@", "let", "const", "async", "readonly", + "abstract", "native", "static", "public", "private", + "declare", "default", "export"}; +// NOLINTEND(modernize-avoid-c-arrays) + +// Note: Potential annotation allowed node need to collect jsdoc. +// NOLINTBEGIN(fuchsia-statically-constructed-objects, cert-err58-cpp) +static const std::unordered_set ANNOTATION_ALLOWED_NODE = {ir::AstNodeType::METHOD_DEFINITION, + ir::AstNodeType::CLASS_DECLARATION, + ir::AstNodeType::FUNCTION_DECLARATION, + ir::AstNodeType::TS_INTERFACE_DECLARATION, + ir::AstNodeType::CLASS_PROPERTY, + ir::AstNodeType::VARIABLE_DECLARATION, + ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION, + ir::AstNodeType::ARROW_FUNCTION_EXPRESSION, + ir::AstNodeType::ANNOTATION_DECLARATION}; +// NOLINTEND(fuchsia-statically-constructed-objects, cert-err58-cpp) + +static const ArenaVector &GetAstAnnotationUsage(const ir::AstNode *node) +{ + switch (node->Type()) { + case ir::AstNodeType::METHOD_DEFINITION: { + auto *func = node->AsMethodDefinition()->Function(); + ES2PANDA_ASSERT(func != nullptr); + return func->Annotations(); + } + case ir::AstNodeType::CLASS_DECLARATION: + return node->AsClassDeclaration()->Definition()->Annotations(); + case ir::AstNodeType::FUNCTION_DECLARATION: + return node->AsFunctionDeclaration()->Annotations(); + case ir::AstNodeType::TS_INTERFACE_DECLARATION: + return node->AsTSInterfaceDeclaration()->Annotations(); + case ir::AstNodeType::CLASS_PROPERTY: + return node->AsClassProperty()->Annotations(); + case ir::AstNodeType::VARIABLE_DECLARATION: + return node->AsVariableDeclaration()->Annotations(); + case ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION: + return node->AsTSTypeAliasDeclaration()->Annotations(); + case ir::AstNodeType::ETS_PARAMETER_EXPRESSION: + return node->AsETSParameterExpression()->Annotations(); + case ir::AstNodeType::ARROW_FUNCTION_EXPRESSION: + return node->AsArrowFunctionExpression()->Annotations(); + case ir::AstNodeType::ANNOTATION_DECLARATION: + return node->AsAnnotationDeclaration()->Annotations(); + default: + ES2PANDA_UNREACHABLE(); + } +} + +static void HandlePotentialPrefix(parser::JsdocHelper *jsdocHelper) +{ + jsdocHelper->Iterator().Reset(jsdocHelper->Node()->Start().index); + jsdocHelper->BackwardAndSkipSpace(1); + for (auto prefix : POTENTIAL_PREFIX) { + auto currentSv = jsdocHelper->SourceView(START_POS, jsdocHelper->Iterator().Index() + COLLECT_CURRENT_POS); + if (currentSv.EndsWith(prefix)) { + jsdocHelper->BackwardAndSkipSpace(prefix.length()); + } + } +} + +static void HandlePotentialPrefixOrAnnotationUsage(parser::JsdocHelper *jsdocHelper) +{ + if (ANNOTATION_ALLOWED_NODE.count(jsdocHelper->Node()->Type()) == 0) { + HandlePotentialPrefix(jsdocHelper); + return; + } + + auto const &annoUsage = GetAstAnnotationUsage(jsdocHelper->Node()); + if (annoUsage.empty()) { + HandlePotentialPrefix(jsdocHelper); + return; + } + + // Note: eat current iter. + jsdocHelper->Iterator().Reset(annoUsage[0]->Range().start.index - 1); + if (jsdocHelper->Iterator().Index() != START_POS) { + // Note: eat token `@` + jsdocHelper->BackwardAndSkipSpace(1); + } +} + +void JsdocHelper::BackWardUntilJsdocStart() +{ + while (true) { + const char32_t cp = PeekBackWard(); + switch (cp) { + case util::StringView::Iterator::INVALID_CP: { + break; + } + case lexer::LEX_CHAR_ASTERISK: { + Backward(1); + if (PeekBackWard() != lexer::LEX_CHAR_ASTERISK) { + continue; + } + + Backward(1); + if (PeekBackWard() == lexer::LEX_CHAR_SLASH) { + break; + } + [[fallthrough]]; + } + default: { + SkipCpBackward(); + continue; + } + } + break; + } +} + +util::StringView JsdocHelper::GetJsdocBackward() +{ + HandlePotentialPrefixOrAnnotationUsage(this); + size_t jsdocEndPos = Iterator().Index() + COLLECT_CURRENT_POS; + size_t backwardPos = jsdocEndPos; + auto currentSourceView = SourceView(START_POS, jsdocEndPos); + while (currentSourceView.EndsWith(JSDOC_END)) { + BackwardAndSkipSpace(JSDOC_END.length()); + BackWardUntilJsdocStart(); + backwardPos = Iterator().Index(); + if (Iterator().Index() == START_POS) { + break; + } + BackwardAndSkipSpace(1); + currentSourceView = SourceView(START_POS, Iterator().Index() + COLLECT_CURRENT_POS); + } + + if (backwardPos == jsdocEndPos) { + return EMPTY_JSDOC; + } + return SourceView(backwardPos, jsdocEndPos); +} + +util::StringView JsdocHelper::GetLicenseStringFromStart() +{ + if (!sourceCode_.StartsWith(LICENSES_START) && !sourceCode_.StartsWith(JSDOC_START)) { + return EMPTY_LICENSE; + } + Iterator().Reset(START_POS); + Forward(LICENSES_START.length()); + + do { + const char32_t cp = Iterator().Peek(); + switch (cp) { + case util::StringView::Iterator::INVALID_CP: { + break; + } + case lexer::LEX_CHAR_ASTERISK: { + Forward(1); + if (Iterator().Peek() == lexer::LEX_CHAR_SLASH) { + Forward(1); + break; + } + [[fallthrough]]; + } + default: { + Iterator().SkipCp(); + continue; + } + } + break; + } while (true); + + return SourceView(START_POS, Iterator().Index()); +} +} // namespace ark::es2panda::parser diff --git a/ets2panda/parser/JsdocHelper.h b/ets2panda/parser/JsdocHelper.h new file mode 100644 index 0000000000..383d26078f --- /dev/null +++ b/ets2panda/parser/JsdocHelper.h @@ -0,0 +1,190 @@ +/** + * Copyright (c) 2021-2025 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. + */ + +#ifndef ES2PANDA_JSDOC_HELPER_H +#define ES2PANDA_JSDOC_HELPER_H + +#include "parserImpl.h" +#include "parser/program/program.h" +#include "lexer/token/letters.h" +#include "ir/astNode.h" +#include "ir/base/classDefinition.h" +#include "ir/ets/etsModule.h" + +namespace ark::es2panda::parser { +using UStringView = util::StringView; +class JsdocHelper { +public: + explicit JsdocHelper(const ir::AstNode *inputNode) + { + auto root = inputNode; + while (root->Parent() != nullptr) { + root = root->Parent(); + } + root_ = root; + program_ = root_->AsETSModule()->Program(); + sourceCode_ = program_->SourceCode(); + iter_ = util::StringView::Iterator(sourceCode_); + InitNode(inputNode); + } + + NO_COPY_SEMANTIC(JsdocHelper); + DEFAULT_MOVE_SEMANTIC(JsdocHelper); + + ~JsdocHelper() = default; + + util::StringView GetJsdocBackward(); + util::StringView GetLicenseStringFromStart(); + + const ir::AstNode *Node() + { + return node_; + } + + util::StringView::Iterator &Iterator() + { + return iter_; + } + + util::StringView SourceView(size_t begin, size_t end) const + { + return sourceCode_.Substr(begin, end); + } + + void BackwardAndSkipSpace(size_t offset) + { + Iterator().Backward(offset); + SkipWhiteSpacesBackward(); + } + + void Backward(size_t offset) + { + Iterator().Backward(offset); + } + + void Forward(size_t offset) + { + Iterator().Forward(offset); + } + + char32_t PeekBackWard() const + { + return iter_.Index() != 0 ? iter_.Peek() : util::StringView::Iterator::INVALID_CP; + } + +private: + void InitNode(const ir::AstNode *input) + { + if (input->IsClassDefinition()) { + node_ = input->Parent(); + } else { + node_ = input; + } + } + + bool SkipWhiteSpacesBackwardHelper(const char32_t &cp) + { + if (cp < lexer::LEX_ASCII_MAX_BITS) { + return false; + } + + size_t cpSize {}; + + char32_t ch = Iterator().PeekCp(&cpSize); + switch (ch) { + case lexer::LEX_CHAR_LS: + case lexer::LEX_CHAR_PS: + case lexer::LEX_CHAR_NBSP: + case lexer::LEX_CHAR_ZWNBSP: + case lexer::LEX_CHAR_OGHAM: + case lexer::LEX_CHAR_NARROW_NO_BREAK_SP: + case lexer::LEX_CHAR_MATHEMATICAL_SP: + case lexer::LEX_CHAR_IDEOGRAPHIC_SP: + Iterator().Backward(cpSize); + return true; + default: + if (ch >= lexer::LEX_CHAR_ENQUAD && ch <= lexer::LEX_CHAR_ZERO_WIDTH_SP) { + Iterator().Backward(cpSize); + return true; + } + return false; + } + } + + void SkipWhiteSpacesBackward() + { + bool skipContinue = true; + while (skipContinue) { + auto cp = Iterator().Peek(); + switch (cp) { + case lexer::LEX_CHAR_CR: + Iterator().Backward(1); + if (Iterator().Peek() != lexer::LEX_CHAR_LF) { + Iterator().Backward(1); + } + [[fallthrough]]; + case lexer::LEX_CHAR_LF: + case lexer::LEX_CHAR_VT: + case lexer::LEX_CHAR_FF: + case lexer::LEX_CHAR_SP: + case lexer::LEX_CHAR_TAB: + case lexer::LEX_CHAR_NEXT_LINE: + Iterator().Backward(1); + continue; + default: + skipContinue = SkipWhiteSpacesBackwardHelper(cp); + } + } + } + + void SkipCpBackward() + { + if (iter_.Index() == 0) { + return; + } + Backward(1U); + + char32_t cu0 = static_cast(iter_.Peek()); + if (cu0 < UStringView::Constants::UTF8_1BYTE_LIMIT) { + return; + } + + if ((cu0 & UStringView::Constants::UTF8_3BYTE_HEADER) == UStringView::Constants::UTF8_2BYTE_HEADER) { + Backward(1U); + return; + } + + if ((cu0 & UStringView::Constants::UTF8_4BYTE_HEADER) == UStringView::Constants::UTF8_3BYTE_HEADER) { + Backward(2U); + return; + } + + if (((cu0 & UStringView::Constants::UTF8_DECODE_4BYTE_MASK) == UStringView::Constants::UTF8_4BYTE_HEADER) && + (cu0 <= UStringView::Constants::UTF8_DECODE_4BYTE_LIMIT)) { + Backward(3U); + } + } + + void BackWardUntilJsdocStart(); + + const ir::AstNode *root_ {}; + const parser::Program *program_ {}; + util::StringView sourceCode_ {}; + util::StringView::Iterator iter_ {nullptr}; + const ir::AstNode *node_ {}; +}; +} // namespace ark::es2panda::parser + +#endif diff --git a/ets2panda/parser/TypedParser.cpp b/ets2panda/parser/TypedParser.cpp index 474bfaa6ef..90150e77c6 100644 --- a/ets2panda/parser/TypedParser.cpp +++ b/ets2panda/parser/TypedParser.cpp @@ -588,10 +588,6 @@ ArenaVector TypedParser::ParseTypeLiteralOrInterfaceBody() util::ErrorRecursionGuard infiniteLoopBlocker(Lexer()); ir::AstNode *member = ParseTypeLiteralOrInterfaceMember(); - if (member == nullptr) { - break; - } - if (member->IsMethodDefinition() && member->AsMethodDefinition()->Function() != nullptr && member->AsMethodDefinition()->Function()->IsOverload() && member->AsMethodDefinition()->Function()->Body() != nullptr) { @@ -610,10 +606,6 @@ ArenaVector TypedParser::ParseTypeLiteralOrInterfaceBody() break; } - if (Lexer()->GetToken().Type() == lexer::TokenType::JS_DOC_START) { - continue; - } - if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_COMMA && Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_SEMI_COLON) { if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_SUBSTITUTION) { diff --git a/ets2panda/parser/context/parserContext.cpp b/ets2panda/parser/context/parserContext.cpp index 2fa2490a99..5b49e8a438 100644 --- a/ets2panda/parser/context/parserContext.cpp +++ b/ets2panda/parser/context/parserContext.cpp @@ -23,11 +23,6 @@ ParserContext::ParserContext(const Program *program, ParserStatus status) { } -ParserContext::ParserContext(const Program *program, ParserStatus status, bool isEnableJsdoc) - : program_(program), status_(status), lang_(ToLanguage(program->Extension())), isEnableJsdoc_(isEnableJsdoc) -{ -} - const ParserContext *ParserContext::FindLabel(const util::StringView &label) const { const auto *iter = this; diff --git a/ets2panda/parser/context/parserContext.h b/ets2panda/parser/context/parserContext.h index 80620c2c64..3c5fcd70e5 100644 --- a/ets2panda/parser/context/parserContext.h +++ b/ets2panda/parser/context/parserContext.h @@ -74,8 +74,7 @@ enum class ParserStatus : uint64_t { DEPENDENCY_ANALYZER_MODE = 1ULL << 39ULL, STATIC_BLOCK = 1ULL << 40ULL, - ALLOW_JS_DOC_START = 1ULL << 41ULL, - IN_PACKAGE = 1ULL << 42ULL, + IN_PACKAGE = 1ULL << 41ULL, }; } // namespace ark::es2panda::parser @@ -93,8 +92,6 @@ public: explicit ParserContext(const Program *program, ParserStatus status); - explicit ParserContext(const Program *program, ParserStatus status, bool isEnableJsdoc); - explicit ParserContext(ParserContext *current, ParserStatus newStatus, util::StringView label = "") : program_(current->program_), prev_(current), label_(label), lang_(current->lang_) { @@ -103,7 +100,6 @@ public: ParserStatus::ALLOW_THIS_TYPE | ParserStatus::IN_CLASS_BODY | ParserStatus::FUNCTION | ParserStatus::IN_AMBIENT_CONTEXT); status_ = currentStatus | newStatus; - isEnableJsdoc_ = prev_->isEnableJsdoc_; } DEFAULT_COPY_SEMANTIC(ParserContext); @@ -190,11 +186,6 @@ public: return formattingFileName_; } - [[nodiscard]] bool IsEnableJsdocParse() const noexcept - { - return isEnableJsdoc_; - } - template void SetFormattingFileName(T &&fileName) { @@ -208,7 +199,6 @@ private: util::StringView label_ {}; std::string_view formattingFileName_ {DEFAULT_SOURCE_FILE}; Language lang_; - bool isEnableJsdoc_ {false}; }; } // namespace ark::es2panda::parser diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index be0526d944..409e4ef4f6 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -46,10 +46,7 @@ using namespace std::literals::string_literals; namespace ark::es2panda::parser { ParserImpl::ParserImpl(Program *program, const util::Options *options, util::DiagnosticEngine &diagnosticEngine, ParserStatus status) - : program_(program), - context_(program_, status, options == nullptr ? false : options->IsEnableJsdocParse()), - options_(options), - diagnosticEngine_(diagnosticEngine) + : program_(program), context_(program_, status), options_(options), diagnosticEngine_(diagnosticEngine) { } @@ -840,10 +837,6 @@ ParserImpl::ClassBody ParserImpl::ParseClassBody(ir::ClassDefinitionModifiers mo util::ErrorRecursionGuard infiniteLoopBlocker(Lexer()); ir::AstNode *property = ParseClassElement(properties, modifiers, flags); - if (property == nullptr) { - continue; - } - if (property->IsBrokenStatement()) { // Error processing. continue; } diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index ab10f02797..e7a99ab7c3 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -192,6 +192,7 @@ protected: friend class ETSNolintParser; friend class lexer::RegExpParser; friend class util::SourcePositionHelper; + friend class JsdocHelper; void LogExpectedToken(lexer::TokenType tokenType); void LogUnexpectedToken(lexer::TokenType tokenType); diff --git a/ets2panda/parser/statementParser.cpp b/ets2panda/parser/statementParser.cpp index aa8941392c..e45ca21c2d 100644 --- a/ets2panda/parser/statementParser.cpp +++ b/ets2panda/parser/statementParser.cpp @@ -13,7 +13,6 @@ * limitations under the License. */ -#include "ETSparser.h" #include "parser/parserFlags.h" #include "parser/parserStatusContext.h" #include "util/errorRecovery.h" @@ -147,10 +146,6 @@ ir::Statement *ParserImpl::ParseStatementControlFlowTokenHelper(StatementParsing // NOLINTNEXTLINE(google-default-arguments) ir::Statement *ParserImpl::ParseStatement(StatementParsingFlags flags) { - if (IsETSParser()) { - AsETSParser()->HandleJsDocLikeComments(); - } - const auto tokenType = lexer_->GetToken().Type(); bool isPunctuatorToken = tokenType == lexer::TokenType::PUNCTUATOR_LEFT_BRACE || tokenType == lexer::TokenType::PUNCTUATOR_SEMI_COLON || diff --git a/ets2panda/public/CMakeLists.txt b/ets2panda/public/CMakeLists.txt index 6d07c5e907..29ebe35e30 100644 --- a/ets2panda/public/CMakeLists.txt +++ b/ets2panda/public/CMakeLists.txt @@ -281,7 +281,6 @@ set (HEADERS_TO_BE_PARSED ${ES2PANDA_ROOT}/ir/statements/annotationDeclaration.h ${ES2PANDA_ROOT}/ir/statements/annotationUsage.h ${ES2PANDA_ROOT}/ir/annotationAllowed.h - ${ES2PANDA_ROOT}/ir/jsDocAllowed.h ${ES2PANDA_ROOT}/ir/base/scriptFunctionSignature.h ${ES2PANDA_ROOT}/ir/expressions/yieldExpression.h ${ES2PANDA_ROOT}/ir/ets/etsNewMultiDimArrayInstanceExpression.h @@ -317,7 +316,6 @@ set (ES2PANDA_API_GENERATED ${LIBGEN_DIR}/gen/headers/varbinder/variableFlags.yaml ${LIBGEN_DIR}/gen/headers/ir/typed.yaml ${LIBGEN_DIR}/gen/headers/ir/annotationAllowed.yaml - ${LIBGEN_DIR}/gen/headers/ir/jsDocAllowed.yaml ${LIBGEN_DIR}/gen/headers/es2panda.yaml ${LIBGEN_DIR}/gen/headers/ir/statements/labelledStatement.yaml ${LIBGEN_DIR}/gen/headers/checker/types/ts/unknownType.yaml diff --git a/ets2panda/public/cppToCTypes.yaml b/ets2panda/public/cppToCTypes.yaml index 8e8f440bb0..35a4c10b60 100644 --- a/ets2panda/public/cppToCTypes.yaml +++ b/ets2panda/public/cppToCTypes.yaml @@ -2892,28 +2892,6 @@ change_types: cast: reverse_cast: '' - - es2panda_arg: - name: '|arg_name|' - type: - name: JsDocInfo - max_ptr_depth: 0 - new_args: - - type: - name: es2panda_JsDocInfo - ptr_depth: 1 - namespace: 'ir::' - name: '|arg_name|' - cast: - expression: "\ - \tir::JsDocInfo |arg_name|ArenaUnorderedMap{reinterpret_cast(context)->allocator->Adapter()};\n - \tfor (size_t j = 0; j < |arg_name|->len; ++j) {\n - \t\t|arg_name|ArenaUnorderedMap.emplace(|arg_name|->strings[j], JsDocRecordToE2p(|arg_name|->jsDocRecords[j]));\n - \t}" - var_name: '|arg_name|ArenaUnorderedMap' - reverse_cast: - start: JsDocInfoFromE2p(reinterpret_cast(context)->allocator, - end: ) - - es2panda_arg: name: '|arg_name|' type: diff --git a/ets2panda/public/es2panda_lib.cpp b/ets2panda/public/es2panda_lib.cpp index 04039bb900..683d1eeb84 100644 --- a/ets2panda/public/es2panda_lib.cpp +++ b/ets2panda/public/es2panda_lib.cpp @@ -201,37 +201,6 @@ __attribute__((unused)) es2panda_OverloadInfo OverloadInfoToE2p(const ir::Overlo return es2pandaOverloadInfo; } -__attribute__((unused)) ir::JsDocRecord JsDocRecordToE2p(const es2panda_JsDocRecord *jsDocRecord) -{ - return ir::JsDocRecord(jsDocRecord->name, jsDocRecord->param, jsDocRecord->comment); -} - -__attribute__((unused)) es2panda_JsDocRecord *JsDocRecordFromE2p(ArenaAllocator *allocator, - const ir::JsDocRecord &jsDocRecord) -{ - es2panda_JsDocRecord *res = allocator->New(); - res->name = StringViewToCString(allocator, jsDocRecord.name); - res->param = StringViewToCString(allocator, jsDocRecord.param); - res->comment = StringViewToCString(allocator, jsDocRecord.comment); - return res; -} - -__attribute__((unused)) es2panda_JsDocInfo *JsDocInfoFromE2p(ArenaAllocator *allocator, const ir::JsDocInfo &jsDocInfo) -{ - size_t jsDocInfoLen = jsDocInfo.size(); - es2panda_JsDocInfo *res = allocator->New(); - res->len = jsDocInfoLen; - res->strings = allocator->New(jsDocInfoLen); - res->jsDocRecords = allocator->New(jsDocInfoLen); - size_t i = 0; - for (const auto &[key, value] : jsDocInfo) { - res->strings[i] = StringViewToCString(allocator, key); - res->jsDocRecords[i] = JsDocRecordFromE2p(allocator, value); - ++i; - }; - return res; -} - __attribute__((unused)) char const *ArenaStrdup(ArenaAllocator *allocator, char const *src) { size_t len = strlen(src); @@ -1125,6 +1094,40 @@ extern "C" es2panda_AstNode *DeclarationFromIdentifier([[maybe_unused]] es2panda return reinterpret_cast(compiler::DeclarationFromIdentifier(E2pNode)); } +extern "C" bool IsImportTypeKind([[maybe_unused]] es2panda_Context *context, es2panda_AstNode *node) +{ + auto E2pNode = reinterpret_cast(node); + if (E2pNode->IsETSImportDeclaration()) { + return E2pNode->AsETSImportDeclaration()->IsTypeKind(); + } + + if (E2pNode->IsImportDeclaration()) { + return E2pNode->AsETSImportDeclaration()->IsTypeKind(); + } + + auto ctx = reinterpret_cast(context); + auto id = ctx->config->diagnosticKindStorage.size() + 1; + auto type = util::DiagnosticType::PLUGIN_WARNING; + util::DiagnosticMessageParams params {}; + diagnostic::DiagnosticKind *kind = &ctx->config->diagnosticKindStorage.emplace_back(type, id, "Insert wrong node!"); + ctx->diagnosticEngine->LogDiagnostic(*kind, params, E2pNode->Start()); + return false; +} + +extern "C" char *GetLicenseFromRootNode(es2panda_Context *ctx, es2panda_AstNode *node) +{ + auto E2pNode = reinterpret_cast(node); + auto *allocator = reinterpret_cast(ctx)->allocator; + return StringViewToCString(allocator, compiler::GetLicenseFromRootNode(E2pNode)); +} + +extern "C" char *JsdocStringFromDeclaration([[maybe_unused]] es2panda_Context *ctx, es2panda_AstNode *node) +{ + auto E2pNode = reinterpret_cast(node); + auto *allocator = reinterpret_cast(ctx)->allocator; + return StringViewToCString(allocator, compiler::JsdocStringFromDeclaration(E2pNode)); +} + extern "C" es2panda_AstNode *FirstDeclarationByNameFromNode([[maybe_unused]] es2panda_Context *ctx, const es2panda_AstNode *node, const char *name) { @@ -1389,6 +1392,9 @@ es2panda_Impl g_impl = { Es2pandaEnumFromString, Es2pandaEnumToString, DeclarationFromIdentifier, + IsImportTypeKind, + JsdocStringFromDeclaration, + GetLicenseFromRootNode, FirstDeclarationByNameFromNode, FirstDeclarationByNameFromProgram, AllDeclarationsByNameFromNode, diff --git a/ets2panda/public/es2panda_lib.h b/ets2panda/public/es2panda_lib.h index f5380df394..6d466703a0 100644 --- a/ets2panda/public/es2panda_lib.h +++ b/ets2panda/public/es2panda_lib.h @@ -129,18 +129,6 @@ typedef struct es2panda_OverloadInfo { bool returnVoid; } es2panda_OverloadInfo; -typedef struct es2panda_JsDocRecord { - char *name; - char *param; - char *comment; -} es2panda_JsDocRecord; - -typedef struct es2panda_JsDocInfo { - char **strings; - es2panda_JsDocRecord **jsDocRecords; - size_t len; -} es2panda_JsDocInfo; - enum es2panda_ContextState { ES2PANDA_STATE_NEW, ES2PANDA_STATE_PARSED, @@ -259,6 +247,9 @@ struct CAPI_EXPORT es2panda_Impl { Es2pandaEnum (*Es2pandaEnumFromString)(es2panda_Context *ctx, const char *str); char *(*Es2pandaEnumToString)(es2panda_Context *ctx, Es2pandaEnum id); es2panda_AstNode *(*DeclarationFromIdentifier)(es2panda_Context *ctx, es2panda_AstNode *node); + bool (*IsImportTypeKind)(es2panda_Context *ctx, es2panda_AstNode *node); + char *(*JsdocStringFromDeclaration)(es2panda_Context *ctx, es2panda_AstNode *node); + char *(*GetLicenseFromRootNode)(es2panda_Context *ctx, es2panda_AstNode *node); es2panda_AstNode *(*FirstDeclarationByNameFromNode)(es2panda_Context *ctx, const es2panda_AstNode *node, const char *name); es2panda_AstNode *(*FirstDeclarationByNameFromProgram)(es2panda_Context *ctx, const es2panda_Program *program, diff --git a/ets2panda/public/es2panda_lib.idl.erb b/ets2panda/public/es2panda_lib.idl.erb index 3d997031ad..c7984402fe 100644 --- a/ets2panda/public/es2panda_lib.idl.erb +++ b/ets2panda/public/es2panda_lib.idl.erb @@ -216,6 +216,9 @@ interface es2panda_Impl { Es2pandaEnum Es2pandaEnumFromString(es2panda_Context ctx, String str); String Es2pandaEnumToString(es2panda_Context ctx, Es2pandaEnum id); ir.AstNode DeclarationFromIdentifier(es2panda_Context ctx, ir.Identifier node); + boolean IsImportTypeKind(es2panda_Context ctx, ir.AstNode node); + String JsdocStringFromDeclaration(es2panda_Context ctx, ir.AstNode node); + String GetLicenseFromRootNode(es2panda_Context ctx, ir.AstNode node); ir.AstNode FirstDeclarationByNameFromNode(es2panda_Context ctx, ir.AstNode node, String name); ir.AstNode FirstDeclarationByNameFromProgram(es2panda_Context ctx, es2panda_Program program, String name); sequence AllDeclarationsByNameFromNode(es2panda_Context ctx, ir.AstNode node, String name); diff --git a/ets2panda/public/es2panda_lib.rb b/ets2panda/public/es2panda_lib.rb index ea0f1efc6d..38156b8080 100644 --- a/ets2panda/public/es2panda_lib.rb +++ b/ets2panda/public/es2panda_lib.rb @@ -1300,7 +1300,7 @@ module Es2pandaLibApi end def template_extends_classes - %w[Annotated Typed AnnotationAllowed JsDocAllowed] + %w[Annotated Typed AnnotationAllowed] end def primitive_types @@ -1378,8 +1378,6 @@ module Es2pandaLibApi es2panda_Options es2panda_Path es2panda_OverloadInfo - es2panda_JsDocRecord - es2panda_JsDocInfo ] end diff --git a/ets2panda/public/ignoredAllowed.yaml b/ets2panda/public/ignoredAllowed.yaml index ec92b68d45..59992aa8d1 100644 --- a/ets2panda/public/ignoredAllowed.yaml +++ b/ets2panda/public/ignoredAllowed.yaml @@ -251,4 +251,3 @@ allowed_list: - StringView - TypeNode - ArkTsConfig - - JsDocInfo diff --git a/ets2panda/test/compiler/ets/enum_as_class_member_getValue_call-expected.txt b/ets2panda/test/compiler/ets/enum_as_class_member_getValue_call-expected.txt index 1540edf473..09c65034d2 100644 --- a/ets2panda/test/compiler/ets/enum_as_class_member_getValue_call-expected.txt +++ b/ets2panda/test/compiler/ets/enum_as_class_member_getValue_call-expected.txt @@ -5128,14 +5128,14 @@ }, "loc": { "start": { - "line": 1, + "line": 20, "column": 1, - "program": null + "program": "enum_as_class_member_getValue_call.ets" }, "end": { - "line": 1, - "column": 1, - "program": null + "line": 24, + "column": 2, + "program": "enum_as_class_member_getValue_call.ets" } } }, @@ -10258,14 +10258,14 @@ }, "loc": { "start": { - "line": 1, + "line": 26, "column": 1, - "program": null + "program": "enum_as_class_member_getValue_call.ets" }, "end": { - "line": 1, - "column": 1, - "program": null + "line": 30, + "column": 2, + "program": "enum_as_class_member_getValue_call.ets" } } }, diff --git a/ets2panda/test/compiler/ets/import_tests/enum_export-expected.txt b/ets2panda/test/compiler/ets/import_tests/enum_export-expected.txt index 5c2bf9184d..b637e562e8 100644 --- a/ets2panda/test/compiler/ets/import_tests/enum_export-expected.txt +++ b/ets2panda/test/compiler/ets/import_tests/enum_export-expected.txt @@ -2368,14 +2368,14 @@ }, "loc": { "start": { - "line": 1, - "column": 1, - "program": null + "line": 20, + "column": 9, + "program": "enum_export.ets" }, "end": { - "line": 1, - "column": 1, - "program": null + "line": 20, + "column": 38, + "program": "enum_export.ets" } } }, @@ -7506,14 +7506,14 @@ }, "loc": { "start": { - "line": 1, - "column": 1, - "program": null + "line": 22, + "column": 8, + "program": "enum_export.ets" }, "end": { - "line": 1, - "column": 1, - "program": null + "line": 24, + "column": 2, + "program": "enum_export.ets" } } }, @@ -12370,14 +12370,14 @@ }, "loc": { "start": { - "line": 1, - "column": 1, - "program": null + "line": 26, + "column": 8, + "program": "enum_export.ets" }, "end": { - "line": 1, - "column": 1, - "program": null + "line": 29, + "column": 2, + "program": "enum_export.ets" } } }, diff --git a/ets2panda/test/parser/ets/declare_enum-expected.txt b/ets2panda/test/parser/ets/declare_enum-expected.txt index ace3a9cf37..a4b51e8966 100644 --- a/ets2panda/test/parser/ets/declare_enum-expected.txt +++ b/ets2panda/test/parser/ets/declare_enum-expected.txt @@ -2650,14 +2650,14 @@ }, "loc": { "start": { - "line": 1, - "column": 1, - "program": null + "line": 16, + "column": 9, + "program": "declare_enum.ets" }, "end": { - "line": 1, - "column": 1, - "program": null + "line": 19, + "column": 2, + "program": "declare_enum.ets" } } }, diff --git a/ets2panda/test/parser/ets/switch_enum_string_case-expected.txt b/ets2panda/test/parser/ets/switch_enum_string_case-expected.txt index 832e0c73b8..4ece619664 100644 --- a/ets2panda/test/parser/ets/switch_enum_string_case-expected.txt +++ b/ets2panda/test/parser/ets/switch_enum_string_case-expected.txt @@ -5120,14 +5120,14 @@ }, "loc": { "start": { - "line": 1, + "line": 20, "column": 1, - "program": null + "program": "switch_enum_string_case.ets" }, "end": { - "line": 1, - "column": 1, - "program": null + "line": 20, + "column": 41, + "program": "switch_enum_string_case.ets" } } }, diff --git a/ets2panda/test/runtime/ets/JsDocInfoTest.ets b/ets2panda/test/runtime/ets/JsDocInfoTest.ets deleted file mode 100644 index 81d64b4e4f..0000000000 --- a/ets2panda/test/runtime/ets/JsDocInfoTest.ets +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright (c) 2025 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. - */ - -/** -* JsDocTest on namespace -* -* @JsDocRecord1 comments -* @JsDocRecord2 { JsDocRecord1 param } comments -* @JsDocRecord3 {} comments -*/ -namespace ns { - /** - * JsDocTest on interface. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 param } comments - * @JsDocRecord3 {} comments - */ - export interface itfc1 { - /** - * JsDocTest on method. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - foo():void - - /** - * JsDocTest on prop. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - prop:number - } - - /** - * JsDocTest on class. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 param } comments - * @JsDocRecord3 {} comments - */ - export class cls { - /** - * JsDocTest on method. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - foo():void {} - - /** - * JsDocTest on prop. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - prop:number - } - - /** - * JsDocTest on namespace - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 param } comments - * @JsDocRecord3 {} comments - */ - namespace ns_inner {} -} - -/** - * JsDocTest on interface. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 param } comments - * @JsDocRecord3 {} comments - */ -export interface itfc1 { - /** - * JsDocTest on method. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - foo():void - - /** - * JsDocTest on prop. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - prop:number -} - - - -/** - * JsDocTest on class. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 param } comments - * @JsDocRecord3 {} comments - */ -export class cls { - /** - * JsDocTest on method. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - foo():void {} - - /** - * JsDocTest on prop. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - prop:number -} - -/** -* JsDocTest on arrow function. -* -* @JsDocRecord1 comments -* @JsDocRecord2 { JsDocRecord1 'param' } comments -* @JsDocRecord3 {} comments -*/ -let arrowFunc:() => void = ():void => {} - -/** -* JsDocTest on function decl. -* -* @JsDocRecord1 comments -* @JsDocRecord2 { JsDocRecord1 'param' } comments -* @JsDocRecord3 {} comments -*/ -function testFunc():void {} - -/** -* JsDocTest on typeAlias. -* -* @JsDocRecord1 comments -* @JsDocRecord2 { JsDocRecord1 'param' } comments -* @JsDocRecord3 {} comments -*/ -type xx = -/** -* @xx {xx} xx -*/ -'somexx' -/** -* @xx {xx} xx -*/ -| 'somexx2' -/** -* @xx {xx} xx -*/ -| 'somexx3' - -function main() { - /** - * JsDocTest on arrow variable decl. - * - * @JsDocRecord1 comments - * @JsDocRecord2 { JsDocRecord1 'param' } comments - * @JsDocRecord3 {} comments - */ - let val:string = "ssss" - arktest.assertTrue(true); - arktest.assertEQ(val, "ssss") -} \ No newline at end of file diff --git a/ets2panda/test/unit/plugin/CMakeLists.txt b/ets2panda/test/unit/plugin/CMakeLists.txt index 2725a2a434..ec1e423e43 100644 --- a/ets2panda/test/unit/plugin/CMakeLists.txt +++ b/ets2panda/test/unit/plugin/CMakeLists.txt @@ -106,7 +106,7 @@ set(PLUGIN_TESTS "plugin_proceed_to_state_create_ets_new_expression compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_log_diagnostic_with_suggestion compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "use_plugin_to_test_column_number compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" - "plugin_proceed_to_state_check_jsdoc compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" + "plugin_proceed_to_state_check_jsdoc compile.ets ${EXPECTED_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_check_recheck_trailinglambda compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_test_global_func_call_dump compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_test_interface_duplicate_setter compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_ast_node_type_analyzer.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_ast_node_type_analyzer.cpp index 29a0dc42a9..c3a467c38e 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_ast_node_type_analyzer.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_ast_node_type_analyzer.cpp @@ -112,7 +112,7 @@ static void ProcessTSInterface(es2panda_Context *context, es2panda_AstNode *node static void ProcessNewExpression(es2panda_Context *context, es2panda_AstNode *node) { counter.newExpressionCount++; - auto *typeRef = node ? impl->ETSNewClassInstanceExpressionGetTypeRefConst(context, node) : nullptr; + auto *typeRef = node ? impl->ETSTypeReferencePartName(context, node) : nullptr; if (!typeRef) { return; } diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc-expected.txt b/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc-expected.txt new file mode 100644 index 0000000000..caf79bda0b --- /dev/null +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc-expected.txt @@ -0,0 +1,211 @@ +LOAD SUCCESS +PROCEED TO PARSE SUCCESS +PROCEED TO BOUND SUCCESS +PROCEED TO CHECKED SUCCESS +/* + * Copyright (c) 2025 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. + */ +/** + * ==== export declare class A ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +/** + * ==== export abstract class B ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +/** + * ==== export enum EM1 ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +/** + * ==== JsDocClass ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** +* ==== JsDocClassDefault with multi-anno ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +/** +* ==== JsDocClassOutside jsdoc1 ==== +* @param7 +*/ +/** +* ==== JsDocClassOutside jsdoc2 ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +/** + * ==== JsdocInnerNS ==== + * @param1 {} behindStr + * @param2 preStr { p } + * @param3 preStr { p } behindStr + */ +/** +* ==== JsdocNS ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +/** + * ==== JsdocInterface ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== JsdocInterfaceOutside ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== classFoo1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== classFoo2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== classFoo3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== interfaceFoo1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== interfaceFoo2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== interfaceFoo3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== interfaceFoo4 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== private interfaceProp1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== interfaceProp2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== interfaceProp3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== interfaceProp4 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** +* ==== function decl jsdoc ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +/** + * ==== ambient indexer jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== ambient indexer jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== private classProp1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== public classProp2 jsdoc1 ==== + * @param7 + */ + + /** + * ==== public classProp2 jsdoc2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== classProp3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== enum prop1 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== enum prop2 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== variable decl ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== variable decl with annotation ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +/** + * ==== export Annotation jsdoc ==== + * @param1 preStr { p } behindStr + * @param2 preStr {} behindStr +*/ +/** + * ==== my Annotation jsdoc ==== + * @param1 preStr { p } behindStr + * @param2 preStr {} behindStr + */ +/** + * ==== my Annotation With Annotation jsdoc ==== + * @param1 preStr { p } behindStr + * @param2 preStr {} behindStr +*/ +/** + * ==== function param p1 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +/** + * ==== function param p2 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +PROCEED TO LOWERED SUCCESS +PROCEED TO ASM SUCCESS +PROCEED TO BIN SUCCESS diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc.cpp index bd890b99a6..108ff9928a 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_jsdoc.cpp @@ -26,25 +26,341 @@ // NOLINTBEGIN static es2panda_Impl *impl = nullptr; -static es2panda_AstNode *targetClass = nullptr; +static bool testResult = true; -static std::string g_source = R"( +// Note: namespace declaration and enum declaration will be lowered to class definition before checker. +static std::map classMap = {{"A", nullptr}, + {"B", nullptr}, + {"JsDocClass", nullptr}, + {"JsDocClassOutside", nullptr}, + {"JsDocClassDefault", nullptr}, + {"JsdocNS", nullptr}, + {"JsdocInnerNS", nullptr}, + {"EM1", nullptr}}; + +static std::map interfaceMap = {{"JsdocInterface", nullptr}, + {"JsdocInterfaceOutside", nullptr}}; + +// Note: the interface property will be lowered to "getter" and "setter" before check, they will be found in methodMap. +static std::map methodMap = { + {"interfaceFoo1", nullptr}, {"interfaceFoo2", nullptr}, {"interfaceFoo3", nullptr}, {"interfaceFoo4", nullptr}, + {"classFoo1", nullptr}, {"classFoo2", nullptr}, {"classFoo3", nullptr}, {"interfaceProp1", nullptr}, + {"interfaceProp2", nullptr}, {"interfaceProp3", nullptr}, {"interfaceProp4", nullptr}, {"jsDocFunc", nullptr}}; + +// Note: the variableDecl witll be transferred to property of ETSGLOBAL after lowerings. +static std::map propertyMap = { + {"classProp1", nullptr}, {"classProp2", nullptr}, {"classProp3", nullptr}, {"jsdocVal1", nullptr}, + {"jsdocVal2", nullptr}, {"em1Prop1", nullptr}, {"em1Prop2", nullptr}}; + +static std::map annotationMap = { + {"myAnno", nullptr}, {"myAnnoWithAnno", nullptr}, {"exportAnno", nullptr}}; + +static std::map etsParamsMap = {{"fooP1", nullptr}, {"fooP2", nullptr}}; + +static es2panda_AstNode *typeAlias = nullptr; +static es2panda_AstNode *indexerClass = nullptr; +static std::map indexerTransferredAccessor = {{"$_get", nullptr}, {"$_set", nullptr}}; + +static std::string g_source = R"(/* + * Copyright (c) 2025 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. + */ + +@interface myMultiAnno {} + +/** + * ==== my Annotation jsdoc ==== + * @param1 preStr { p } behindStr + * @param2 preStr {} behindStr + */ +@interface myAnno {} + +/** + * ==== my Annotation With Annotation jsdoc ==== + * @param1 preStr { p } behindStr + * @param2 preStr {} behindStr +*/ +@Retention({policy: "SOURCE"}) +@interface myAnnoWithAnno {} + +/** + * ==== export Annotation jsdoc ==== + * @param1 preStr { p } behindStr + * @param2 preStr {} behindStr +*/ +export declare @interface exportAnno {} + +/** + * ==== export declare class A ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +@myAnno +export declare class A {} + +/** + * ==== export abstract class B ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +export declare abstract class B {} + +declare class IndexerClass { + /** + * ==== ambient indexer jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + [idx: number] : string +} + +/** + * ==== export enum EM1 ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ +export declare enum EM1 { + /** + * ==== enum prop1 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + em1Prop1 = 1, + + /** + * ==== enum prop2 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + em1Prop2 = 2 +} + +/** +* ==== JsdocNS ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +namespace JsdocNS { + /** + * ==== JsdocInterface ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + export interface JsdocInterface { + /** + * ==== interfaceFoo1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + interfaceFoo1():void + + /** + * ==== interfaceFoo2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + interfaceFoo2():void + + /** + * ==== private interfaceProp1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + private interfaceProp1:number + + /** + * ==== interfaceProp2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + interfaceProp2:number | string[] + } + + /** + * ==== JsDocClass ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + export class JsDocClass { + /** + * ==== classFoo1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + classFoo1():void {} + + /** + * ==== private classProp1 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + private classProp1:number = 0; + } + + /** + * ==== JsdocInnerNS ==== + * @param1 {} behindStr + * @param2 preStr { p } + * @param3 preStr { p } behindStr + */ + namespace JsdocInnerNS {} +} /** + * ==== JsdocInterfaceOutside ==== * @param1 {} behindStr * @param2 preStr { p } - * @param3 preStr { p } behindStr - * @param4 preStr {} behindStr - * @returns { number } */ -class A {} +export declare interface JsdocInterfaceOutside { + /** + * ==== interfaceFoo3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + interfaceFoo3():void + + /** + * ==== interfaceProp3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + interfaceProp3:number + + /** + * ==== interfaceFoo4 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + interfaceFoo4():void + + /** + * ==== interfaceProp4 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + interfaceProp4:number +} + +/** +* ==== JsDocClassDefault with multi-anno ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +@myMultiAnno +@myAnno +export default class JsDocClassDefault {} + +/** +* ==== JsDocClassOutside jsdoc1 ==== +* @param7 +*/ +/** +* ==== JsDocClassOutside jsdoc2 ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +export class JsDocClassOutside { + /** + * ==== classFoo2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + classFoo2():void {} + + /** + * ==== classFoo3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + classFoo3():void {} + + /** + * ==== public classProp2 jsdoc1 ==== + * @param7 + */ + + /** + * ==== public classProp2 jsdoc2 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + public classProp2:number = 0; + + /** + * ==== classProp3 ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ + classProp3:number = 0; +} + +/** +* ==== function decl jsdoc ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +function jsDocFunc( +/** + * ==== function param p1 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ fooP1: number, + +/** + * ==== function param p2 jsdoc ==== + * @param1 {} behindStr + * @param2 preStr { p } +*/ fooP2: string): void {} + +/** +* ==== typeAlas jsdoc ==== +* @param1 {} behindStr +* @param2 preStr { p } +*/ +type typeAlias = 'somexx3' + +/** + * ==== variable decl ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +let jsdocVal1:string = "ssss" + +/** + * ==== variable decl with annotation ==== + * @param1 {} behindStr + * @param2 preStr { p } + */ +@myAnnoWithAnno +let jsdocVal2:string = "ssss" )"; -static std::vector nameList = {"returns", "param4", "param3", "param2", "param1"}; -static std::vector paramList = {" number", "", " p", " p", ""}; -static std::vector commentsList = {"{ number }", "preStr {} behindStr", "preStr { p } behindStr", - "preStr { p }", "{} behindStr"}; +static void FindAnnotationDecl(es2panda_AstNode *ast, void *context) +{ + auto ctx = reinterpret_cast(context); + if (!impl->IsAnnotationDeclaration(ast)) { + return; + } + auto *ident = impl->AnnotationDeclarationGetBaseNameConst(ctx, ast); + if (ident == nullptr) { + return; + } + auto name = std::string(impl->IdentifierName(ctx, ident)); + if (annotationMap.find(name) != annotationMap.end()) { + annotationMap[name] = ast; + } +} -static void SetTargetClass(es2panda_AstNode *ast, void *context) +static void FindClass(es2panda_AstNode *ast, void *context) { auto ctx = reinterpret_cast(context); if (!impl->IsClassDefinition(ast)) { @@ -54,75 +370,185 @@ static void SetTargetClass(es2panda_AstNode *ast, void *context) if (ident == nullptr) { return; } + auto name = std::string(impl->IdentifierName(ctx, ident)); + if (classMap.find(name) != classMap.end()) { + classMap[name] = ast; + } + + if (name == "IndexerClass") { + indexerClass = ast; + } +} + +static void FindInterface(es2panda_AstNode *ast, void *context) +{ + auto ctx = reinterpret_cast(context); + if (!impl->IsTSInterfaceDeclaration(ast)) { + return; + } + auto *ident = impl->TSInterfaceDeclarationId(ctx, ast); + if (ident == nullptr) { + return; + } + auto name = std::string(impl->IdentifierName(ctx, ident)); + if (interfaceMap.find(name) != interfaceMap.end()) { + interfaceMap[name] = ast; + } +} + +static void FindMethodDef(es2panda_AstNode *ast, void *context) +{ + auto ctx = reinterpret_cast(context); + if (!impl->IsMethodDefinition(ast)) { + return; + } + auto *function = impl->MethodDefinitionFunction(ctx, ast); + if (function == nullptr) { + return; + } + + auto *ident = impl->ScriptFunctionId(ctx, function); + if (ident == nullptr) { + return; + } + auto name = std::string(impl->IdentifierName(ctx, ident)); + if (methodMap.find(name) != methodMap.end()) { + methodMap[name] = ast; + } +} + +static void FindETSParamDecl(es2panda_AstNode *ast, void *context) +{ + auto ctx = reinterpret_cast(context); + if (!impl->IsETSParameterExpression(ast)) { + return; + } + auto *ident = impl->ETSParameterExpressionIdent(ctx, ast); + if (ident == nullptr) { + return; + } + auto name = std::string(impl->IdentifierName(ctx, ident)); + if (etsParamsMap.find(name) != etsParamsMap.end()) { + etsParamsMap[name] = ast; + } +} + +static void FindClassElement(es2panda_AstNode *ast, void *context) +{ + auto ctx = reinterpret_cast(context); + if (!impl->IsClassProperty(ast)) { + return; + } + auto *ident = impl->ClassElementId(ctx, ast); + if (ident == nullptr) { + return; + } + + auto name = std::string(impl->IdentifierName(ctx, ident)); + if (propertyMap.find(name) != propertyMap.end()) { + propertyMap[name] = ast; + } +} + +static void FindTypeAliasDecl(es2panda_AstNode *ast, void *context) +{ + auto ctx = reinterpret_cast(context); + if (!impl->IsTSTypeAliasDeclaration(ast)) { + return; + } + auto *ident = impl->TSTypeAliasDeclarationId(ctx, ast); + if (ident == nullptr) { + return; + } auto name = impl->IdentifierName(ctx, ident); - if (std::string(name) == "A") { - targetClass = ast; + if (std::string(name) == "typeAlias") { + typeAlias = ast; + } +} + +// Note: should be called after `FindClass` +static void FindIndexerTransferredGetterSetter(es2panda_AstNode *ast, void *context) +{ + auto ctx = reinterpret_cast(context); + if (!impl->IsMethodDefinition(ast)) { + return; + } + auto *function = impl->MethodDefinitionFunction(ctx, ast); + if (function == nullptr) { + return; + } + + auto *ident = impl->ScriptFunctionId(ctx, function); + if (ident == nullptr) { + return; + } + auto name = std::string(impl->IdentifierName(ctx, ident)); + if (indexerTransferredAccessor.find(name) != indexerTransferredAccessor.end()) { + indexerTransferredAccessor[name] = ast; } } +static void FindTargetAst(es2panda_Context *context, es2panda_AstNode *ast) +{ + impl->AstNodeForEach(ast, FindClass, context); + impl->AstNodeForEach(ast, FindInterface, context); + impl->AstNodeForEach(ast, FindMethodDef, context); + impl->AstNodeForEach(ast, FindClassElement, context); + impl->AstNodeForEach(ast, FindTypeAliasDecl, context); + impl->AstNodeForEach(ast, FindAnnotationDecl, context); + impl->AstNodeForEach(ast, FindETSParamDecl, context); + impl->AstNodeForEach(indexerClass, FindIndexerTransferredGetterSetter, context); +} + static bool TestJSDoc(es2panda_Context *context) { auto *program = impl->ContextProgram(context); - auto *ast = impl->ProgramAst(context, program); - impl->AstNodeForEach(ast, SetTargetClass, context); - if (ast == nullptr) { - std::cerr << "FAILED TO GET AST" << std::endl; + auto *entryAst = impl->ProgramAst(context, program); + if (entryAst == nullptr) { return false; } - size_t jsDocInfoArrayLen = 0; - auto **jsDocInfoArray = impl->ClassDefinitionJsDocInformation(context, targetClass, &jsDocInfoArrayLen); - if (jsDocInfoArray == nullptr || jsDocInfoArrayLen == 0) { - std::cerr << "FAILED TO GET JsDocInfo" << std::endl; - return false; + FindTargetAst(context, entryAst); + + std::cout << impl->GetLicenseFromRootNode(context, entryAst) << std::endl; + + for (const auto &[name, targetAst] : classMap) { + std::cout << impl->JsdocStringFromDeclaration(context, targetAst) << std::endl; + } + + for (const auto &[name, targetAst] : interfaceMap) { + std::cout << impl->JsdocStringFromDeclaration(context, targetAst) << std::endl; + } + + for (const auto &[name, targetAst] : methodMap) { + std::cout << impl->JsdocStringFromDeclaration(context, targetAst) << std::endl; } - for (size_t i = 0; i < jsDocInfoArrayLen; ++i) { - auto *jsDocInfo = jsDocInfoArray[i]; - if (jsDocInfo == nullptr) { - std::cerr << "FAILED TO GET JsDocInfo" << std::endl; - return false; - } - for (size_t j = 0; j < jsDocInfo->len; ++j) { - auto *jsDocRecord = jsDocInfo->jsDocRecords[j]; - if (jsDocRecord == nullptr) { - std::cerr << "FAILED TO GET JsDocRecord" << std::endl; - return false; - } - auto *name = jsDocInfo->strings[j]; - if (name == nullptr) { - std::cerr << "FAILED TO GET JsDocName" << std::endl; - return false; - } - - if (jsDocRecord->name != nameList[j]) { - std::cerr << "exact: " << jsDocRecord->name << " exp: " << nameList[j] << std::endl; - return false; - } - - if (jsDocRecord->param != paramList[j]) { - std::cerr << "exact: " << jsDocRecord->param << " exp: " << paramList[j] << std::endl; - return false; - } - - if (jsDocRecord->comment != commentsList[j]) { - std::cerr << "exact: " << jsDocRecord->comment << " exp: " << commentsList[j] << std::endl; - return false; - } - } - } - return true; + + for (const auto &[name, targetAst] : indexerTransferredAccessor) { + std::cout << impl->JsdocStringFromDeclaration(context, targetAst) << std::endl; + } + + for (const auto &[name, targetAst] : propertyMap) { + std::cout << impl->JsdocStringFromDeclaration(context, targetAst) << std::endl; + } + + impl->JsdocStringFromDeclaration(context, typeAlias); + + for (const auto &[name, targetAst] : annotationMap) { + std::cout << impl->JsdocStringFromDeclaration(context, targetAst) << std::endl; + } + + for (const auto &[name, targetAst] : etsParamsMap) { + std::cout << impl->JsdocStringFromDeclaration(context, targetAst) << std::endl; + } + + return testResult; } int main(int argc, char **argv) { - std::vector newArgv(argv, argv + argc); - const char *extraArg = "--parse-jsdoc"; - newArgv.push_back(const_cast(extraArg)); - int newArgc = static_cast(newArgv.size()); - char **newArgvRaw = newArgv.data(); - std::map>> testFunctions; - testFunctions[ES2PANDA_STATE_PARSED] = {TestJSDoc}; - ProccedToStatePluginTestData data = {newArgc, newArgvRaw, &impl, testFunctions, true, g_source}; + testFunctions[ES2PANDA_STATE_CHECKED] = {TestJSDoc}; + ProccedToStatePluginTestData data = {argc, argv, &impl, testFunctions, true, g_source}; return RunAllStagesWithTestFunction(data); } diff --git a/ets2panda/test/unit/sizeof_node_test.cpp b/ets2panda/test/unit/sizeof_node_test.cpp index 8cf6d3d293..5980cba512 100644 --- a/ets2panda/test/unit/sizeof_node_test.cpp +++ b/ets2panda/test/unit/sizeof_node_test.cpp @@ -103,24 +103,13 @@ size_t SizeOfNodeTest::SizeOf>() // clang-format on } -template <> -size_t SizeOfNodeTest::SizeOf>>() -{ - JsDocAllowed> *node = nullptr; - - // clang-format off - return SizeOf>() + - sizeof(node->jsDocInformation_); - // clang-format on -} - template <> size_t SizeOfNodeTest::SizeOf() { ClassDefinition *node = nullptr; // clang-format off - return SizeOf>>() + + return SizeOf>() + sizeof(node->scope_) + sizeof(node->internalName_) + sizeof(node->ident_) + @@ -205,24 +194,13 @@ size_t SizeOfNodeTest::SizeOf>() // clang-format on } -template <> -size_t SizeOfNodeTest::SizeOf>>() -{ - JsDocAllowed> *node = nullptr; - - // clang-format off - return SizeOf>() + - sizeof(node->jsDocInformation_); - // clang-format on -} - template <> size_t SizeOfNodeTest::SizeOf() { ClassProperty *node = nullptr; // clang-format off - return SizeOf>>() + + return SizeOf>() + sizeof(node->typeAnnotation_) + Align(sizeof(node->isDefault_) + sizeof(node->needInitInStaticBlock_)); @@ -256,24 +234,13 @@ size_t SizeOfNodeTest::SizeOf>() // clang-format on } -template <> -size_t SizeOfNodeTest::SizeOf>>() -{ - JsDocAllowed> *node = nullptr; - - // clang-format off - return SizeOf>() + - sizeof(node->jsDocInformation_); - // clang-format on -} - template <> size_t SizeOfNodeTest::SizeOf() { ScriptFunction *node = nullptr; // clang-format off - return SizeOf>>() + + return SizeOf>() + sizeof(node->id_) + Align(sizeof(node->irSignature_)) + sizeof(node->body_) + @@ -310,24 +277,13 @@ size_t SizeOfNodeTest::SizeOf>() // clang-format on } -template <> -size_t SizeOfNodeTest::SizeOf>>() -{ - JsDocAllowed> *node = nullptr; - - // clang-format off - return SizeOf>() + - sizeof(node->jsDocInformation_); - // clang-format on -} - template <> size_t SizeOfNodeTest::SizeOf() { FunctionDeclaration *node = nullptr; // clang-format off - return SizeOf>>() + + return SizeOf>() + sizeof(node->decorators_) + sizeof(node->func_) + Align(sizeof(node->isAnonymous_)); @@ -363,24 +319,13 @@ size_t SizeOfNodeTest::SizeOf>() // clang-format on } -template <> -size_t SizeOfNodeTest::SizeOf>>() -{ - JsDocAllowed> *node = nullptr; - - // clang-format off - return SizeOf>() + - sizeof(node->jsDocInformation_); - // clang-format on -} - template <> size_t SizeOfNodeTest::SizeOf() { TSInterfaceDeclaration *node = nullptr; // clang-format off - return SizeOf>>() + + return SizeOf>() + sizeof(node->decorators_) + sizeof(node->scope_) + sizeof(node->id_) + @@ -412,24 +357,13 @@ size_t SizeOfNodeTest::SizeOf() return SizeOf>(); } -template <> -size_t SizeOfNodeTest::SizeOf>() -{ - JsDocAllowed *node = nullptr; - - // clang-format off - return SizeOf() + - sizeof(node->jsDocInformation_); - // clang-format on -} - template <> size_t SizeOfNodeTest::SizeOf() { TSTypeAliasDeclaration *node = nullptr; // clang-format off - return SizeOf>() + + return SizeOf() + sizeof(node->decorators_) + sizeof(node->annotations_) + sizeof(node->id_) + @@ -465,7 +399,7 @@ size_t SizeOfNodeTest::SizeOf() VariableDeclaration *node = nullptr; // clang-format off - return SizeOf>>() + + return SizeOf>() + Align(sizeof(node->kind_) + sizeof(node->decorators_) + sizeof(node->declarators_)); @@ -568,24 +502,13 @@ size_t SizeOfNodeTest::SizeOf>() // clang-format on } -template <> -size_t SizeOfNodeTest::SizeOf>>() -{ - JsDocAllowed> *node = nullptr; - - // clang-format off - return SizeOf>() + - sizeof(node->jsDocInformation_); - // clang-format on -} - template <> size_t SizeOfNodeTest::SizeOf() { ETSModule *node = nullptr; // clang-format off - return SizeOf>>() + + return SizeOf>() + sizeof(node->ident_) + Align(sizeof(node->flag_)) + sizeof(node->program_)+ @@ -652,22 +575,16 @@ TEST_F(SizeOfNodeTest, DeclNodesBase) ASSERT_EQ(sizeof(Typed), SizeOf>()); ASSERT_EQ(sizeof(TypedAstNode), SizeOf()); ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); - ASSERT_EQ(sizeof(JsDocAllowed>), - SizeOf>>()); ASSERT_EQ(sizeof(Statement), SizeOf()); ASSERT_EQ(sizeof(Typed), SizeOf>()); ASSERT_EQ(sizeof(TypedStatement), SizeOf()); ASSERT_EQ(sizeof(ClassElement), SizeOf()); ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); - ASSERT_EQ(sizeof(JsDocAllowed>), - SizeOf>>()); ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); - ASSERT_EQ(sizeof(JsDocAllowed>), SizeOf>>()); + ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); + ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); - ASSERT_EQ(sizeof(JsDocAllowed>), SizeOf>>()); ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); - ASSERT_EQ(sizeof(JsDocAllowed>), - SizeOf>>()); // IMPORTANT NOTICE: This test purpose is to warn a developer who modified fields of specific node classes. // Classes listed below implements CopyTo/Clone methods. @@ -676,7 +593,7 @@ TEST_F(SizeOfNodeTest, DeclNodesBase) ASSERT_EQ(sizeof(Annotated), SizeOf>()); ASSERT_EQ(sizeof(AnnotatedStatement), SizeOf()); - ASSERT_EQ(sizeof(JsDocAllowed), SizeOf>()); + ASSERT_EQ(sizeof(AnnotatedStatement), SizeOf()); ASSERT_EQ(sizeof(Expression), SizeOf()); ASSERT_EQ(sizeof(AnnotationAllowed), SizeOf>()); ASSERT_EQ(sizeof(TypeNode), SizeOf()); diff --git a/ets2panda/util/options.cpp b/ets2panda/util/options.cpp index e2818c61a2..dbb5e1fbee 100644 --- a/ets2panda/util/options.cpp +++ b/ets2panda/util/options.cpp @@ -242,8 +242,6 @@ bool Options::Parse(Span args) logLevel_ = Logger::LevelFromString(GetLogLevel()); } - parseJsdoc_ = WasSetParseJsdoc(); - InitCompilerOptions(); return ProcessEtsSpecificOptions(); diff --git a/ets2panda/util/options.h b/ets2panda/util/options.h index f7bf991b9c..7b115a50f1 100644 --- a/ets2panda/util/options.h +++ b/ets2panda/util/options.h @@ -170,11 +170,6 @@ public: return astVerifierEachPhase_ || astVerifierPhases_.find(std::string(phase)) != astVerifierPhases_.end(); } - bool IsEnableJsdocParse() const - { - return parseJsdoc_; - } - private: template static bool CallPandArgParser(const std::vector &args, T &options, @@ -209,7 +204,6 @@ private: Logger::Level logLevel_ {Logger::Level::ERROR}; EvalMode evalMode_ = {EvalMode::NONE}; util::DiagnosticEngine &diagnosticEngine_; - bool parseJsdoc_ {}; }; } // namespace ark::es2panda::util diff --git a/ets2panda/util/options.yaml b/ets2panda/util/options.yaml index 5aef15cb8f..b8b0938afb 100644 --- a/ets2panda/util/options.yaml +++ b/ets2panda/util/options.yaml @@ -74,11 +74,6 @@ options: default: false description: Print names of files that are part of compilation -- name: parse-jsdoc - type: bool - default: false - description: Enable the ability to parse jsdoc - # Compiler - name: dump-assembly type: bool -- Gitee