diff --git a/es2panda/BUILD.gn b/es2panda/BUILD.gn index 1e543a54196cf25f210777d05052a253d27e3533..ed8b51073eb55465cce3a7698d2c3689375c1904 100644 --- a/es2panda/BUILD.gn +++ b/es2panda/BUILD.gn @@ -164,6 +164,7 @@ es2panda_src = [ "ir/ts/tsQualifiedName.cpp", "ir/ts/tsSignatureDeclaration.cpp", "ir/ts/tsStringKeyword.cpp", + "ir/ts/tsTemplateLiteralType.cpp", "ir/ts/tsThisType.cpp", "ir/ts/tsTupleType.cpp", "ir/ts/tsTypeAliasDeclaration.cpp", diff --git a/es2panda/CMakeLists.txt b/es2panda/CMakeLists.txt index c96649e60d19a3e3d386269896ee4fcfa3f82027..9a213084ed639501358bb18e51bac2756085c22f 100644 --- a/es2panda/CMakeLists.txt +++ b/es2panda/CMakeLists.txt @@ -213,6 +213,7 @@ set(ES2PANDA_LIB_SRC ir/ts/tsQualifiedName.cpp ir/ts/tsSignatureDeclaration.cpp ir/ts/tsStringKeyword.cpp + ir/ts/tsTemplateLiteralType.cpp ir/ts/tsThisType.cpp ir/ts/tsTupleType.cpp ir/ts/tsTypeAliasDeclaration.cpp diff --git a/es2panda/ir/astNodeMapping.h b/es2panda/ir/astNodeMapping.h index decf76bb9d160fc0b8aac063bdfbd5235ff54ac3..33c87d44a7d00171c69723ece769594a244fc7e3 100644 --- a/es2panda/ir/astNodeMapping.h +++ b/es2panda/ir/astNodeMapping.h @@ -95,6 +95,7 @@ _(TS_SIGNATURE_DECLARATION, TSSignatureDeclaration) \ _(TS_PARENT_TYPE, TSParenthesizedType) \ _(TS_LITERAL_TYPE, TSLiteralType) \ + _(TS_TEMPLATE_LITERAL_TYPE, TSTemplateLiteralType) \ _(TS_INFER_TYPE, TSInferType) \ _(TS_CONDITIONAL_TYPE, TSConditionalType) \ _(TS_IMPORT_TYPE, TSImportType) \ diff --git a/es2panda/ir/base/templateElement.cpp b/es2panda/ir/base/templateElement.cpp index a0e843b5944fd30f35331f0ec14475795e677cbc..4013a17ddfc694df74a89c692ab5521d5d745dde 100644 --- a/es2panda/ir/base/templateElement.cpp +++ b/es2panda/ir/base/templateElement.cpp @@ -28,7 +28,7 @@ void TemplateElement::Dump(ir::AstDumper *dumper) const { dumper->Add({ {"type", "TemplateElement"}, - {"value", {{"raw", "" /* raw_ */}, {"cooked", "" /* cooked_ */}}}, + {"value", {{"raw", raw_}, {"cooked", cooked_}}}, }); } diff --git a/es2panda/ir/ts/tsTemplateLiteralType.cpp b/es2panda/ir/ts/tsTemplateLiteralType.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9c300e2548c1f0ff724944c5a59b23ff3d99d84 --- /dev/null +++ b/es2panda/ir/ts/tsTemplateLiteralType.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "tsTemplateLiteralType.h" + +#include + +namespace panda::es2panda::ir { + +void TSTemplateLiteralType::Iterate(const NodeTraverser &cb) const +{ + for (auto *it : references_) { + cb(it); + } + + for (auto *it : quasis_) { + cb(it); + } +} + +void TSTemplateLiteralType::Dump(ir::AstDumper *dumper) const +{ + dumper->Add({{"type", "TSTemplateLiteralType"}, {"references", references_}, {"quasis", quasis_}}); +} + +void TSTemplateLiteralType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} + +checker::Type *TSTemplateLiteralType::Check([[maybe_unused]] checker::Checker *checker) const +{ + // TODO(huangyu): Implement checker for template literal type + return nullptr; +} + +checker::Type *TSTemplateLiteralType::GetType(checker::Checker *checker) const +{ + // TODO(huangyu): Implement GetType for template literal type + return nullptr; +} + +} // namespace panda::es2panda::ir diff --git a/es2panda/ir/ts/tsTemplateLiteralType.h b/es2panda/ir/ts/tsTemplateLiteralType.h new file mode 100644 index 0000000000000000000000000000000000000000..6fe63b084a1a44ad6a041f0c03f13bd9e4f62cd8 --- /dev/null +++ b/es2panda/ir/ts/tsTemplateLiteralType.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_IR_TS_TSTEMPLATELITERALTYPE_H +#define ES2PANDA_IR_TS_TSTEMPLATELITERALTYPE_H + +#include +#include +#include +#include +#include + +namespace panda::es2panda::ir { + +class TSTemplateLiteralType : public TypeNode { +public: + explicit TSTemplateLiteralType(ArenaVector &&quasis, ArenaVector &&references) + : TypeNode(AstNodeType::TS_TEMPLATE_LITERAL_TYPE), + quasis_(std::move(quasis)), + references_(std::move(references)) {} + + void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; + checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override; + checker::Type *GetType(checker::Checker *checker) const override; + +private: + ArenaVector quasis_; + ArenaVector references_; +}; + +} // namespace panda::es2panda::ir + +#endif // ES2PANDA_IR_TS_TSTEMPLATELITERALTYPE_H diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 27c93813a7d2a2aa2fd7a140ecaf8bef67d11ad3..d3039e2a13f1e9f951552eec55d0d17801a936de 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -77,6 +77,7 @@ #include #include #include +#include #include #include #include @@ -388,6 +389,59 @@ ir::Expression *ParserImpl::ParseTsThisTypeOrTsTypePredicate(ir::Expression *typ return ParseTsThisType(throwError); } +ir::Expression *ParserImpl::ParseTsTemplateLiteralType() +{ + ASSERT(lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_BACK_TICK); + lexer::SourcePosition startLoc = lexer_->GetToken().Start(); + + ArenaVector quasis(Allocator()->Adapter()); + ArenaVector references(Allocator()->Adapter()); + + while (true) { + lexer_->ResetTokenEnd(); + const auto startPos = lexer_->Save(); + + lexer_->ScanString(); + util::StringView cooked = lexer_->GetToken().String(); + + lexer_->Rewind(startPos); + auto [raw, end, scanExpression] = lexer_->ScanTemplateString(); + + auto *element = AllocNode(raw.View(), cooked); + element->SetRange({lexer::SourcePosition{startPos.iterator.Index(), startPos.line}, + lexer::SourcePosition{end, lexer_->Line()}}); + quasis.push_back(element); + + if (!scanExpression) { + lexer_->ScanTemplateStringEnd(); + break; + } + + ir::Expression *reference = nullptr; + + { + lexer::TemplateLiteralParserContext ctx(lexer_); + lexer_->PushTemplateContext(&ctx); + lexer_->NextToken(); + TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::THROW_ERROR; + reference = ParseTsTypeAnnotation(&options); + } + + if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_BRACE) { + ThrowSyntaxError("Unexpected token, expected '}'."); + } + + references.push_back(reference); + } + + ir::Expression *typeAnnotation = AllocNode(std::move(quasis), std::move(references)); + typeAnnotation->SetRange({startLoc, lexer_->GetToken().End()}); + + lexer_->NextToken(); + + return typeAnnotation; +} + ir::Expression *ParserImpl::ParseTsTypeAnnotationElement(ir::Expression *typeAnnotation, TypeAnnotationParsingOptions *options) { @@ -490,6 +544,9 @@ ir::Expression *ParserImpl::ParseTsTypeAnnotationElement(ir::Expression *typeAnn *options & TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE, *options & TypeAnnotationParsingOptions::THROW_ERROR); } + case lexer::TokenType::PUNCTUATOR_BACK_TICK: { + return ParseTsTemplateLiteralType(); + } default: { break; } diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index aa97f60574e6020538857083b7480f364489e4a6..ed541f881ccf5b7feb311e87864fc6f77f279228 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -259,6 +259,7 @@ private: ir::Expression *ParseTsTypeReferenceOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate); ir::Expression *ParseTsThisTypeOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate, bool throwError); + ir::Expression *ParseTsTemplateLiteralType(); ir::Expression *ParseTsTypeAnnotationElement(ir::Expression *typeAnnotation, TypeAnnotationParsingOptions *options); ir::ModifierFlags ParseModifiers(); diff --git a/es2panda/test/parser/js/test-tagged-template-expression-expected.txt b/es2panda/test/parser/js/test-tagged-template-expression-expected.txt index d3447b7222689ba79b80a8df58453a3079f69551..4acf364c5f609f08cb9fd13e343f376aa8a576aa 100644 --- a/es2panda/test/parser/js/test-tagged-template-expression-expected.txt +++ b/es2panda/test/parser/js/test-tagged-template-expression-expected.txt @@ -223,8 +223,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "almafa", + "cooked": "almafa" }, "loc": { "start": { @@ -497,8 +497,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "alma", + "cooked": "alma" }, "loc": { "start": { @@ -636,8 +636,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "fa", + "cooked": "fa" }, "loc": { "start": { @@ -710,8 +710,14 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": " + + +", + "cooked": " + + +" }, "loc": { "start": { @@ -799,8 +805,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "fa", + "cooked": "fa" }, "loc": { "start": { @@ -902,8 +908,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "fa", + "cooked": "fa" }, "loc": { "start": { @@ -980,8 +986,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "alma", + "cooked": "alma" }, "loc": { "start": { @@ -1039,8 +1045,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "fa", + "cooked": "fa" }, "loc": { "start": { diff --git a/es2panda/test/parser/js/test-template-literal-expected.txt b/es2panda/test/parser/js/test-template-literal-expected.txt index 96d0aabd855db80a35f556e5b094879a14b3fe8b..2921051fc2ed1eba1a34a85cbb1316e4a61d4d75 100644 --- a/es2panda/test/parser/js/test-template-literal-expected.txt +++ b/es2panda/test/parser/js/test-template-literal-expected.txt @@ -150,8 +150,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "almafa", + "cooked": "almafa" }, "loc": { "start": { @@ -368,8 +368,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "alma", + "cooked": "alma" }, "loc": { "start": { @@ -479,8 +479,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "fa", + "cooked": "fa" }, "loc": { "start": { @@ -525,8 +525,14 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": " + + +", + "cooked": " + + +" }, "loc": { "start": { @@ -571,8 +577,8 @@ { "type": "TemplateElement", "value": { - "raw": "", - "cooked": "" + "raw": "$ {alma}", + "cooked": "$ {alma}" }, "loc": { "start": { diff --git a/es2panda/test/parser/ts/test-type-template-literal1-expected.txt b/es2panda/test/parser/ts/test-type-template-literal1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..0c56453c29011447222bc27ae544d0a1dd4e5930 --- /dev/null +++ b/es2panda/test/parser/ts/test-type-template-literal1-expected.txt @@ -0,0 +1,203 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "weather", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "typeAnnotation": { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "sunny", + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "report", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "TSTemplateLiteralType", + "references": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "weather", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 34 + } + } + } + ], + "quasis": [ + { + "type": "TemplateElement", + "value": { + "raw": "Today is ", + "cooked": "Today is " + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + { + "type": "TemplateElement", + "value": { + "raw": "", + "cooked": "" + }, + "loc": { + "start": { + "line": 18, + "column": 35 + }, + "end": { + "line": 18, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 37 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 18, + "column": 36 + }, + "end": { + "line": 18, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 19, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-type-template-literal1.ts b/es2panda/test/parser/ts/test-type-template-literal1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f102757290aa21b52245aba8eb147a8da4b05d9 --- /dev/null +++ b/es2panda/test/parser/ts/test-type-template-literal1.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +type weather = "sunny"; +type report = `Today is ${weather}`; diff --git a/es2panda/test/parser/ts/test-type-template-literal2-expected.txt b/es2panda/test/parser/ts/test-type-template-literal2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..ca2a3afbe2730f6a2d9f5b20a2bcc82a63e42a14 --- /dev/null +++ b/es2panda/test/parser/ts/test-type-template-literal2-expected.txt @@ -0,0 +1,553 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "axis1", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "typeAnnotation": { + "type": "TSUnionType", + "types": [ + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "x", + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "y", + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "axis2", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "typeAnnotation": { + "type": "TSUnionType", + "types": [ + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "a", + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "b", + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 9 + } + } + }, + "typeAnnotation": { + "type": "TSUnionType", + "types": [ + { + "type": "TSLiteralType", + "literal": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + { + "type": "TSLiteralType", + "literal": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 19, + "column": 16 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 16 + }, + "end": { + "line": 19, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "node", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 6 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "typeAnnotation": { + "type": "TSTemplateLiteralType", + "references": [ + { + "type": "TSUnionType", + "types": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "axis1", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 16 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 16 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "axis2", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 24 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 24 + }, + "end": { + "line": 20, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 16 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "num", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 20, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 20, + "column": 36 + } + } + } + ], + "quasis": [ + { + "type": "TemplateElement", + "value": { + "raw": "", + "cooked": "" + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + { + "type": "TemplateElement", + "value": { + "raw": "_", + "cooked": "_" + }, + "loc": { + "start": { + "line": 20, + "column": 30 + }, + "end": { + "line": 20, + "column": 31 + } + } + }, + { + "type": "TemplateElement", + "value": { + "raw": "", + "cooked": "" + }, + "loc": { + "start": { + "line": 20, + "column": 37 + }, + "end": { + "line": 20, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 39 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 20, + "column": 38 + }, + "end": { + "line": 20, + "column": 39 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 21, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-type-template-literal2.ts b/es2panda/test/parser/ts/test-type-template-literal2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4331cffd0129d784e3469a5d1a1da4463c9986b6 --- /dev/null +++ b/es2panda/test/parser/ts/test-type-template-literal2.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +type axis1 = "x" | "y"; +type axis2 = "a" | "b"; +type num = 1 | 2; +type node = `${axis1 | axis2}_${num}`; diff --git a/es2panda/test/parser/ts/test-type-template-literal3-expected.txt b/es2panda/test/parser/ts/test-type-template-literal3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d1bbd95d000dab62281419d64236b3c32dd810f6 --- /dev/null +++ b/es2panda/test/parser/ts/test-type-template-literal3-expected.txt @@ -0,0 +1,675 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "upperWord", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "TSTemplateLiteralType", + "references": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "Uppercase", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 53 + }, + "end": { + "line": 16, + "column": 62 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "Str", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 63 + }, + "end": { + "line": 16, + "column": 66 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 63 + }, + "end": { + "line": 16, + "column": 66 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 53 + }, + "end": { + "line": 16, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 53 + }, + "end": { + "line": 16, + "column": 62 + } + } + } + ], + "quasis": [ + { + "type": "TemplateElement", + "value": { + "raw": "OpenHarmony-", + "cooked": "OpenHarmony-" + }, + "loc": { + "start": { + "line": 16, + "column": 39 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + { + "type": "TemplateElement", + "value": { + "raw": "", + "cooked": "" + }, + "loc": { + "start": { + "line": 16, + "column": 68 + }, + "end": { + "line": 16, + "column": 68 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 38 + }, + "end": { + "line": 16, + "column": 69 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "Str", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "constraint": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 70 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 16, + "column": 69 + }, + "end": { + "line": 16, + "column": 70 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "lowerWord", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "TSTemplateLiteralType", + "references": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "Lowercase", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 62 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "Str", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 63 + }, + "end": { + "line": 17, + "column": 66 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 63 + }, + "end": { + "line": 17, + "column": 66 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 62 + } + } + } + ], + "quasis": [ + { + "type": "TemplateElement", + "value": { + "raw": "OpenHarmony-", + "cooked": "OpenHarmony-" + }, + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 51 + } + } + }, + { + "type": "TemplateElement", + "value": { + "raw": "", + "cooked": "" + }, + "loc": { + "start": { + "line": 17, + "column": 68 + }, + "end": { + "line": 17, + "column": 68 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 38 + }, + "end": { + "line": 17, + "column": 69 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "Str", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "constraint": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 15 + }, + "end": { + "line": 17, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 70 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 17, + "column": 69 + }, + "end": { + "line": 17, + "column": 70 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "upperArk", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "upperWord", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "Ark", + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "lowerArk", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "lowerWord", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSLiteralType", + "literal": { + "type": "StringLiteral", + "value": "Ark", + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 34 + } + } + }, + { + "type": "EmptyStatement", + "loc": { + "start": { + "line": 19, + "column": 33 + }, + "end": { + "line": 19, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-type-template-literal3.ts b/es2panda/test/parser/ts/test-type-template-literal3.ts new file mode 100644 index 0000000000000000000000000000000000000000..bd5fc825c65dac3e38af721b1894bd7874565485 --- /dev/null +++ b/es2panda/test/parser/ts/test-type-template-literal3.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +type upperWord = `OpenHarmony-${Uppercase}`; +type lowerWord = `OpenHarmony-${Lowercase}`; +type upperArk = upperWord<"Ark">; +type lowerArk = lowerWord<"Ark">;