diff --git a/ets2panda/aot/main.cpp b/ets2panda/aot/main.cpp index 2d0dd7328ae415d0e160df2dd100d94b99cc2dfd..5e96400e2c1dc39260759b9297e5042d745957c3 100644 --- a/ets2panda/aot/main.cpp +++ b/ets2panda/aot/main.cpp @@ -157,7 +157,7 @@ static int Run(Span args) std::string sourceFile; std::string_view parserInput; if (options->GetCompilationMode() == CompilationMode::GEN_STD_LIB) { - sourceFile = "etsstdlib.sts"; + sourceFile = "etsstdlib.ets"; parserInput = ""; } else { sourceFile = options->SourceFileName(); diff --git a/ets2panda/ast_verifier/importExportAccessValid.cpp b/ets2panda/ast_verifier/importExportAccessValid.cpp index 4c55fc42c36a96b591e3b67a4e74514459d16287..8e7ac7e6b7c824f70f6ef898dd2627c28f9a1884 100644 --- a/ets2panda/ast_verifier/importExportAccessValid.cpp +++ b/ets2panda/ast_verifier/importExportAccessValid.cpp @@ -126,7 +126,7 @@ bool ImportExportAccessValid::HandleImportExportIdentifier(std::unordered_setIsReference(ScriptExtension::STS)) { + if (ident->IsReference(ScriptExtension::ETS)) { const auto *var = ident->Variable(); if (var != nullptr) { if (var->HasFlag(varbinder::VariableFlags::METHOD) && callExpr != nullptr) { diff --git a/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp b/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp index 8853e5a684cc2a99684ddd6f725767a127f66719..b6aea1fcc8de9bb9ef4c87cac6de63ec5987cafc 100644 --- a/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp +++ b/ets2panda/ast_verifier/referenceTypeAnnotationIsNull.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -28,7 +28,7 @@ CheckResult ReferenceTypeAnnotationIsNull::operator()(const ir::AstNode *ast) // We are running AST verifier only for ETS files so it is correct to pass ETS extension here auto const *const id = ast->AsIdentifier(); - if (id->IsReference(ScriptExtension::STS) && id->TypeAnnotation() != nullptr) { + if (id->IsReference(ScriptExtension::ETS) && id->TypeAnnotation() != nullptr) { AddCheckMessage("TYPE_ANNOTATION_NOT_NULLPTR", *ast); return {CheckDecision::INCORRECT, CheckAction::CONTINUE}; } diff --git a/ets2panda/bindings/README.md b/ets2panda/bindings/README.md index fcf24d459249c464da708385eb52965934f30650..c2621a7458267f46bbd59b140db478a26d20fa94 100644 --- a/ets2panda/bindings/README.md +++ b/ets2panda/bindings/README.md @@ -43,10 +43,10 @@ check `lsp_api.ts` file import { Lsp } from "@es2panda/bindings" let lsp = new Lsp() -let def = lsp.getDefinitionAtPosition("/path/to/panda/ets_frontend/ets2panda/foo/f2.sts", 70) +let def = lsp.getDefinitionAtPosition("/path/to/panda/ets_frontend/ets2panda/foo/f2.ets", 70) console.log(def) -let foo = lsp.getSemanticDiagnostics("/path/to/panda/ets_frontend/ets2panda/foo/f3.sts") +let foo = lsp.getSemanticDiagnostics("/path/to/panda/ets_frontend/ets2panda/foo/f3.ets") console.log(foo) ``` diff --git a/ets2panda/bindings/src/global.ts b/ets2panda/bindings/src/global.ts index dd8a37a6669c6bd59bf3e797916bf55be83980eb..9e78f0cb6b6b447b8b6ea89be28bea1f39e9a077 100644 --- a/ets2panda/bindings/src/global.ts +++ b/ets2panda/bindings/src/global.ts @@ -20,7 +20,7 @@ import { Es2pandaNativeModule as GeneratedEs2pandaNativeModule } from "./generat import { initInterop, InteropNativeModule } from "./InteropNativeModule" export class global { - public static filePath: string = "./examples/input/main.sts" + public static filePath: string = "./examples/input/main.ets" private static _config?: KNativePointer public static set config(config: KNativePointer) { diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 45f4e5bdc3f2d437ccb65329236fda48fd67bff0..f96b917d4710833e3dd8c233288e34f80e6e9219 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -2490,7 +2490,7 @@ ir::ClassProperty *ETSChecker::ClassPropToImplementationProp(ir::ClassProperty * auto *fieldDecl = Allocator()->New(classProp->Key()->AsIdentifier()->Name()); fieldDecl->BindNode(classProp); - auto fieldVar = scope->InstanceFieldScope()->AddDecl(Allocator(), fieldDecl, ScriptExtension::STS); + auto fieldVar = scope->InstanceFieldScope()->AddDecl(Allocator(), fieldDecl, ScriptExtension::ETS); fieldVar->AddFlag(varbinder::VariableFlags::PROPERTY); fieldVar->SetScope(scope->InstanceFieldScope()); @@ -2744,7 +2744,7 @@ void ETSChecker::GenerateGetterSetterPropertyAndMethod(ir::ClassProperty *origin auto *const methodScope = scope->InstanceMethodScope(); auto *const decl = Allocator()->New(Allocator(), name, getter); - auto *var = methodScope->AddDecl(Allocator(), decl, ScriptExtension::STS); + auto *var = methodScope->AddDecl(Allocator(), decl, ScriptExtension::ETS); if (var == nullptr) { auto *const prevDecl = methodScope->FindDecl(name); for (const auto &method : {getter, setter}) { diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 428ed3e99c552e37ac1d26dcf60f95b815466932..dc5431712a2e74bae4a7fd5701cdb5b6daccd4bb 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -909,7 +909,7 @@ void ETSChecker::ValidateNonOverriddenFunction(ETSObjectType *classType, ArenaVe ->Scope() ->AsClassScope() ->InstanceFieldScope() - ->AddDecl(Allocator(), newFieldDecl, ScriptExtension::STS) + ->AddDecl(Allocator(), newFieldDecl, ScriptExtension::ETS) ->AsLocalVariable(); newFieldVar->AddFlag(varbinder::VariableFlags::PROPERTY); newFieldVar->AddFlag(varbinder::VariableFlags::PUBLIC); @@ -1842,7 +1842,7 @@ PropertySearchFlags ETSChecker::GetSearchFlags(const ir::MemberExpression *const if (targetRef != nullptr && (targetRef->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE_OR_ENUM) || // NOTE (DZ): need to investigate when and why `targetRef->TsType()->Variable()` can be `nullptr` - // (see ast/parser/ets/union_static_method.sts) + // (see ast/parser/ets/union_static_method.ets) (targetRef->HasFlag(varbinder::VariableFlags::TYPE_ALIAS) && targetRef->TsType()->Variable() != nullptr && targetRef->TsType()->Variable()->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE)))) { searchFlag &= ~PropertySearchFlags::SEARCH_INSTANCE; diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index ea659bddeaf5d955382beb4bbbb704a05feb7eb6..f21483a79f7ee1ce36f68516958c568a926328f9 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -698,7 +698,7 @@ Type *ETSChecker::CreatePartialTypeInterfaceDecl(ir::TSInterfaceDeclaration *con if (prop->IsMethodDefinition() && prop->AsMethodDefinition()->Function()->IsGetter()) { auto *decl = Allocator()->New( Allocator(), prop->AsMethodDefinition()->Key()->AsIdentifier()->Name(), prop); - methodscope->AddDecl(Allocator(), decl, ScriptExtension::STS); + methodscope->AddDecl(Allocator(), decl, ScriptExtension::ETS); } } diff --git a/ets2panda/checker/typeChecker/TypeChecker.cpp b/ets2panda/checker/typeChecker/TypeChecker.cpp index 191e54e33dbf5200535857e86027b978cd1ebcac..b9977ba38af132168115304b4eba1ef966c98192 100644 --- a/ets2panda/checker/typeChecker/TypeChecker.cpp +++ b/ets2panda/checker/typeChecker/TypeChecker.cpp @@ -29,7 +29,7 @@ void ETSTypeChecker::VisitArrowFunctionExpression(ir::ArrowFunctionExpression *n bool RunTypeChecker(Checker *checker, ScriptExtension ext, ir::AstNode *node) { switch (ext) { - case ScriptExtension::STS: + case ScriptExtension::ETS: return ETSTypeChecker(checker).Check(node); case ScriptExtension::JS: return JSTypeChecker(checker).Check(node); diff --git a/ets2panda/checker/types/signature.cpp b/ets2panda/checker/types/signature.cpp index ceed788dbbd9ab2d7a247c391e1540ecef1124e9..34b2d8a74e3ada647e58b00c84a1ff81b9940f23 100644 --- a/ets2panda/checker/types/signature.cpp +++ b/ets2panda/checker/types/signature.cpp @@ -248,7 +248,7 @@ void Signature::IsSubtypeOf(TypeRelation *relation, Signature *super) relation->Result(MethodSignaturesAreCompatible(relation, !relation->IsOverridingCheck(), super, sub)); } -// This function is not used by sts extension +// This function is not used by ets extension void Signature::AssignmentTarget([[maybe_unused]] TypeRelation *relation, [[maybe_unused]] Signature *source) { if (signatureInfo_->restVar == nullptr && source->MinArgCount() > signatureInfo_->params.size()) { diff --git a/ets2panda/compiler/core/compilerImpl.cpp b/ets2panda/compiler/core/compilerImpl.cpp index 22eee1a0c7c03b25ed1726df6c361100c451d131..d716d16816d000b9bc446e2b77d5422c9b69510c 100644 --- a/ets2panda/compiler/core/compilerImpl.cpp +++ b/ets2panda/compiler/core/compilerImpl.cpp @@ -253,7 +253,7 @@ static pandasm::Program *Compile(const CompilationUnit &unit, const PhaseListGet // We have to check the return status of 'RunVerifierAndPhase` and 'RunPhases` separately because there can be // some internal errors (say, in Post-Conditional check) or terminate options (say in 'CheckOptionsAfterPhase') // that were not reported to the log. - if (unit.ext == ScriptExtension::STS) { + if (unit.ext == ScriptExtension::ETS) { if (!RunVerifierAndPhases(context, getPhases(unit.ext), program)) { return nullptr; } @@ -288,7 +288,7 @@ pandasm::Program *CompilerImpl::Compile(const CompilationUnit &unit) compiler::JSFunctionEmitter, compiler::JSEmitter>(unit, compiler::GetPhaseList, this); } - case ScriptExtension::STS: { + case ScriptExtension::ETS: { return compiler::Compile(unit, compiler::GetPhaseList, diff --git a/ets2panda/compiler/lowering/ets/enumLowering.cpp b/ets2panda/compiler/lowering/ets/enumLowering.cpp index 98a79a181889dd5cad514a9816bc7bb300a305b1..201bbdbbc3c975fc8281740288981996c88b012b 100644 --- a/ets2panda/compiler/lowering/ets/enumLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumLowering.cpp @@ -435,7 +435,7 @@ static EnumLoweringPhase::DeclarationFlags GetDeclFlags(ir::TSEnumDeclaration *c bool EnumLoweringPhase::PerformForModule(public_lib::Context *ctx, parser::Program *program) { bool isPerformedSuccess = true; - if (program->Extension() != ScriptExtension::STS) { + if (program->Extension() != ScriptExtension::ETS) { return isPerformedSuccess; } diff --git a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp index 2e80b18777222e922247eed8eddf6453952b85d1..d22507058d1c51418e6ab4f9a5fdbc1d416df338 100644 --- a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp @@ -366,7 +366,7 @@ ir::SwitchStatement *EnumPostCheckLoweringPhase::GenerateGetOrdinalCallForSwitch bool EnumPostCheckLoweringPhase::PerformForModule(public_lib::Context *ctx, parser::Program *program) { - if (program->Extension() != ScriptExtension::STS) { + if (program->Extension() != ScriptExtension::ETS) { return true; } diff --git a/ets2panda/compiler/lowering/ets/extensionAccessorLowering.cpp b/ets2panda/compiler/lowering/ets/extensionAccessorLowering.cpp index 22bf06f071c8050bd984aae9ad128ea5685ed593..06d32a449ab9867ac856218baaea7e22ee51384c 100644 --- a/ets2panda/compiler/lowering/ets/extensionAccessorLowering.cpp +++ b/ets2panda/compiler/lowering/ets/extensionAccessorLowering.cpp @@ -123,7 +123,7 @@ using AstNodePtr = ir::AstNode *; bool ExtensionAccessorPhase::PerformForModule(public_lib::Context *ctx, parser::Program *program) { - if (program->Extension() != ScriptExtension::STS) { + if (program->Extension() != ScriptExtension::ETS) { return true; } diff --git a/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp b/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp index 831d0709364e2ecf1e1d01110010a996e1998ca6..15d96c4b11685895bb290575593e3e6891b5aafc 100644 --- a/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp +++ b/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp @@ -130,7 +130,7 @@ ir::MethodDefinition *InterfacePropertyDeclarationsPhase::GenerateGetterOrSetter auto methodIdent = checker->AllocNode(name, checker->Allocator()); auto *decl = checker->Allocator()->New(name); - auto var = functionScope->AddDecl(checker->Allocator(), decl, ScriptExtension::STS); + auto var = functionScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS); ES2PANDA_ASSERT(var != nullptr); methodIdent->SetVariable(var); @@ -216,7 +216,7 @@ ir::Expression *InterfacePropertyDeclarationsPhase::UpdateInterfaceProperties(ch auto *decl = checker->Allocator()->New(checker->Allocator(), name, getter); - if (methodScope->AddDecl(checker->Allocator(), decl, ScriptExtension::STS) == nullptr) { + if (methodScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS) == nullptr) { auto prevDecl = methodScope->FindDecl(name); ES2PANDA_ASSERT(prevDecl->IsFunctionDecl()); prevDecl->Node()->AsMethodDefinition()->AddOverload(getter); diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index 3e2d877307b68ac3e4eb383a123956c226164cb2..86484ce4412af6d064dabe6f2f09a02f1e6106c7 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -920,7 +920,7 @@ static ir::ScriptFunction *GetWrappingLambdaParentFunction(public_lib::Context * for (auto *p : func->Params()) { ir::Identifier *clone = p->AsETSParameterExpression()->Ident()->Clone(allocator, nullptr); - if (clone->IsIdentifier() && (clone->IsReference(ScriptExtension::STS)) && + if (clone->IsIdentifier() && (clone->IsReference(ScriptExtension::ETS)) && (clone->TypeAnnotation() != nullptr)) { clone->SetTsTypeAnnotation(nullptr); } @@ -1118,7 +1118,7 @@ static ir::AstNode *BuildLambdaClassWhenNeeded(public_lib::Context *ctx, ir::Ast auto *var = id->Variable(); // We are running this lowering only for ETS files // so it is correct to pass ETS extension here to isReference() - if (id->IsReference(ScriptExtension::STS) && id->TsType() != nullptr && id->TsType()->IsETSFunctionType() && + if (id->IsReference(ScriptExtension::ETS) && id->TsType() != nullptr && id->TsType()->IsETSFunctionType() && var != nullptr && var->Declaration() != nullptr && var->Declaration()->IsFunctionDecl() && !IsInCalleePosition(id) && !IsEnumFunctionCall(id)) { return ConvertFunctionReference(ctx, id); diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h b/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h index 5e078e6df42b931e25e8c6693df4c9821e21f14f..51c8c69b3dd759e5943acffdbd5c01bf615f3dcf 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h @@ -25,7 +25,7 @@ namespace ark::es2panda::compiler { class ImportExportDecls : ir::visitor::EmptyAstVisitor { - static constexpr std::string_view DEFAULT_IMPORT_SOURCE_FILE = ".sts"; + static constexpr std::string_view DEFAULT_IMPORT_SOURCE_FILE = ".ets"; static std::string CreateDefaultImportSource(const std::vector &paths) { diff --git a/ets2panda/compiler/lowering/phase.cpp b/ets2panda/compiler/lowering/phase.cpp index b4a995a5e7c96eba120dde33aa2b69608a07e3a2..76673a17194ae7fcbeea4fa001e52387e1edf55e 100644 --- a/ets2panda/compiler/lowering/phase.cpp +++ b/ets2panda/compiler/lowering/phase.cpp @@ -193,7 +193,7 @@ std::vector GetJSPhaseList() std::vector GetPhaseList(ScriptExtension ext) { switch (ext) { - case ScriptExtension::STS: + case ScriptExtension::ETS: return GetETSPhaseList(); case ScriptExtension::AS: return GetASPhaseList(); diff --git a/ets2panda/docs/cheatsheet.md b/ets2panda/docs/cheatsheet.md index 58288c389629e71c0669ab667365d4f535f7766e..27eca5e02edf094aaa57599f73a2eb2c88c0b5de 100644 --- a/ets2panda/docs/cheatsheet.md +++ b/ets2panda/docs/cheatsheet.md @@ -16,8 +16,8 @@ and execution commands will be the same and look like this: ```bash # compilation of separate module and library -./bin/es2panda --extension=sts --output=out.abc --opt-level=2 x.sts -./bin/es2panda --extension=sts --output=etsstdlib.abc --opt-level=2 --gen-stdlib=true +./bin/es2panda --extension=ets --output=out.abc --opt-level=2 x.ets +./bin/es2panda --extension=ets --output=etsstdlib.abc --opt-level=2 --gen-stdlib=true # execution command ./bin/ark --boot-panda-files=etsstdlib.abc --load-runtimes=ets out.abc ETSGLOBAL::main ``` @@ -75,7 +75,7 @@ For more details see *./bin/ark --help*. The library module is compiled the same way as a regular module: ```bash -./bin/es2panda --extension=sts --output=out.abc --opt-level=2 --gen-stdlib=false /path/to/module/folder +./bin/es2panda --extension=ets --output=out.abc --opt-level=2 --gen-stdlib=false /path/to/module/folder ``` *Notes:* @@ -85,7 +85,7 @@ As for now it's imported via *arktsconfig.json* file. This file is generated by *std* and *compat*. They're imported in each module that we compile. - Package modules are not fully supported, see #16267 internal issue. -### How to use .abc and .d.sts without samples +### How to use .abc and .d.ets without samples todo @@ -151,8 +151,8 @@ In additional, implicitly imported paths can be added. After that, your custom c should be passed to the appropriate option: ```bash -./bin/es2panda --extension=sts --output=out.abc --opt-level=2 --gen-stdlib=false \ ---arktsconfig=/path/to/arktsconfig.json x.sts +./bin/es2panda --extension=ets --output=out.abc --opt-level=2 --gen-stdlib=false \ +--arktsconfig=/path/to/arktsconfig.json x.ets ``` ### How to make one .abc file from two @@ -186,7 +186,7 @@ extern "C" ETS_EXPORT ets_int ETS_CALL EtsNapiOnLoad(EtsEnv *env) { ### How to execute program with native .so Need to write *loadLibrary("name of the .so")* in the right place on the ts side. -An example can be found in *NativeModule.sts* file which can be generated by: +An example can be found in *NativeModule.ets* file which can be generated by: ```bash npm panda:sdk:install @@ -207,22 +207,22 @@ todo ### How to run multi-module programm? ```typescript -//x.sts -import { foo } from "./y.sts" +//x.ets +import { foo } from "./y.ets" function main () { console.log(foo()) } ``` ```typescript -//y.sts +//y.ets export function foo(): int {return 54} ``` ```bash # compilation of separate module -./bin/es2panda --extension=sts --output=b.abc --opt-level=2 --ets-module y.sts -./bin/es2panda --extension=sts --output=a.abc --opt-level=2 x.sts +./bin/es2panda --extension=ets --output=b.abc --opt-level=2 --ets-module y.ets +./bin/es2panda --extension=ets --output=a.abc --opt-level=2 x.ets # linkage of binary files ./bin/ark_link --output out.abc -- a.abc b.abc diff --git a/ets2panda/docs/import_export.md b/ets2panda/docs/import_export.md index e99d4058e6a260533765c0f37c40dfa3126fc04b..28c011978df689ee748470325336c31c3ec13142 100644 --- a/ets2panda/docs/import_export.md +++ b/ets2panda/docs/import_export.md @@ -38,7 +38,7 @@ function main() { } ``` -It is ensured currently via the *ETSParser::ParseDefaultSources* method, which parse an internally created ets file named ".sts". +It is ensured currently via the *ETSParser::ParseDefaultSources* method, which parse an internally created ets file named ".ets". ### 3.1. Package level scope @@ -74,19 +74,19 @@ This **foreignBinding_** member helps to eliminate "export waterfall" that is de ``` -// C.sts +// C.ets export let a = 2 ``` -```b.sts -// B.sts -import { a } from c.sts // 'a' marked as foreign +```b.ets +// B.ets +import { a } from c.ets // 'a' marked as foreign ``` ``` -// A.sts -import { a } from b.sts -// 'a' is not visible here, since 'a' is foreign in B.sts +// A.ets +import { a } from b.ets +// 'a' is not visible here, since 'a' is foreign in B.ets ``` ### 4.3. Improvement possibilities/suggestions @@ -109,7 +109,7 @@ Import directive use the following form: ## 5.1. Resolving importPath -The *importPath* is a string literal which can be points to a module (separate module | package module) or a folder (package folder, or a folder which contains an index.ts/index.sts file). It can be specified with a relative or an absolute path, with or without a file extension, and must be able to manage the paths entered in arktsconfig as well. +The *importPath* is a string literal which can be points to a module (separate module | package module) or a folder (package folder, or a folder which contains an index.ts/index.ets file). It can be specified with a relative or an absolute path, with or without a file extension, and must be able to manage the paths entered in arktsconfig as well. Resolving these paths within the compiler is the responsibility of the *importPathManager*. In the process of parsing an import path, the string literal will be passed to the importPathManager which will resolve it as an absolute path and adds it to an own list, called *parseList_.* The latter list serves to let the compiler know what still needs to be parsed (handle and avoid duplications), and this list will be requested and traversed during the *ParseSources* call. The importPathManager also handles errors that can be caught before parsing, for example non-existent, incorrectly specified import paths, but not errors that can only be found after parsing (for example, the package folder should contains only package files that use the same package directive, etc.) @@ -211,10 +211,10 @@ In nutshell, when a top-level declaration is exported, the following happens: Default import binding allows importing a declaration exported from some module as default export ``` -//export.sts +//export.ets export default class TestClass {} -//import.sts +//import.ets import ImportedClass from "./export" ``` @@ -295,20 +295,20 @@ As it has been mentioned above, renaming inside selective export is still under * A compile time error will be thrown, if something is being imported using its original name, but it was exported with an alias * Similar behaviour will occur if something is being referred by its original name after being imported with a namespace import: ``` - //export.sts + //export.ets function test_func(): void {} export {test_func as foo} - //import.sts + //import.ets import * as all from "./export all.test_func() //A compile time error will be thrown at this point, since 'test_func' has an alias 'foo' ``` * Support for exporting an imported program element will also be added at some point: ``` - //export.sts + //export.ets export function foo(): void {} - //export_imported.sts + //export_imported.ets import {foo} from "./export" export {foo} @@ -331,10 +331,10 @@ reExportDirective: ``` ``` - //export.sts + //export.ets export function foo(): void {} - //re-export.sts + //re-export.ets export {foo} from "./export" ``` @@ -359,30 +359,30 @@ A brief description how it works: Currently, re-export is a bit tweaked. See the following example, that is a typical usage of re-export: ``` -// C.sts +// C.ets export let a = 3 -// B.sts -export {a} from "./C.sts" +// B.ets +export {a} from "./C.ets" -// A.sts -import {a} from "./B.sts" +// A.ets +import {a} from "./B.ets" ``` -On a very high level view, the engine copies the path of C.sts (that is defined in B.sts for re-export", and creates a direct import call to C.sts from A.sts. The following code symbolizes it: +On a very high level view, the engine copies the path of C.ets (that is defined in B.ets for re-export", and creates a direct import call to C.ets from A.ets. The following code symbolizes it: ``` -// C.sts +// C.ets export let a = 3 -// B.sts -export {a} from "./C.sts" +// B.ets +export {a} from "./C.ets" -// A.sts -import {a} from "./C.sts" <---- here, a direct import call is executed to C.sts, that works, but not correct. +// A.ets +import {a} from "./C.ets" <---- here, a direct import call is executed to C.ets, that works, but not correct. ``` -Instead, B.sts should import all variables from C.sts and all re-exported variables should be stored in a separated re-exported-variables variable map in B. These variables are not visible in B.sts (for usage) since they are treated for re-export only. After that, A.sts can import all exported variables from B.sts including re-exported-variables. +Instead, B.ets should import all variables from C.ets and all re-exported variables should be stored in a separated re-exported-variables variable map in B. These variables are not visible in B.ets (for usage) since they are treated for re-export only. After that, A.ets can import all exported variables from B.ets including re-exported-variables. ### 6.6. Type exports diff --git a/ets2panda/driver/build_system/src/build/base_mode.ts b/ets2panda/driver/build_system/src/build/base_mode.ts index 40129bf6b042ed7d3446afc479d84ae25be8745b..26767b3061054f2c1b0dc5ec0a8bb8da4b351dd9 100644 --- a/ets2panda/driver/build_system/src/build/base_mode.ts +++ b/ets2panda/driver/build_system/src/build/base_mode.ts @@ -149,7 +149,7 @@ export abstract class BaseMode { arktsGlobal.config = arkts.createConfig([ '_', '--extension', - 'sts', + 'ets', '--arktsconfig', fileInfo.arktsConfigFile, fileInfo.filePath @@ -172,7 +172,7 @@ export abstract class BaseMode { let ets2pandaCmd: string[] = [ '_', '--extension', - 'sts', + 'ets', '--arktsconfig', fileInfo.arktsConfigFile, '--output', diff --git a/ets2panda/driver/build_system/test/demo_hap/build_config.json b/ets2panda/driver/build_system/test/demo_hap/build_config.json index f856a871200b6830d64175b4fb146cf95ef37a9c..fa99f233d6a1cd25f532e905669a0898ec4c66d1 100644 --- a/ets2panda/driver/build_system/test/demo_hap/build_config.json +++ b/ets2panda/driver/build_system/test/demo_hap/build_config.json @@ -5,10 +5,10 @@ "compileFiles": [ "${absolute_path_to_build_system}/test/demo_hap/entry/b.ets", - "${absolute_path_to_build_system}/test/demo_hap/entry/c.sts", - "${absolute_path_to_build_system}/test/demo_hap/entry/d.sts", - "${absolute_path_to_build_system}/test/demo_hap/harA/index.sts", - "${absolute_path_to_build_system}/test/demo_hap/harB/indexB.sts" + "${absolute_path_to_build_system}/test/demo_hap/entry/c.ets", + "${absolute_path_to_build_system}/test/demo_hap/entry/d.ets", + "${absolute_path_to_build_system}/test/demo_hap/harA/index.ets", + "${absolute_path_to_build_system}/test/demo_hap/harB/indexB.ets" ], "packageName": "entry", @@ -35,7 +35,7 @@ "moduleType": "har", "modulePath": "${absolute_path_to_build_system}/test/demo_hap/harA", "sourceRoots": ["./"], - "entryFile": "index.sts" + "entryFile": "index.ets" }, { "packageName": "harB", @@ -43,7 +43,7 @@ "moduleType": "har", "modulePath": "${absolute_path_to_build_system}/test/demo_hap/harB", "sourceRoots": ["./"], - "entryFile": "indexB.sts" + "entryFile": "indexB.ets" } ] } \ No newline at end of file diff --git a/ets2panda/driver/build_system/test/demo_hap/entry/c.sts b/ets2panda/driver/build_system/test/demo_hap/entry/c.ets similarity index 100% rename from ets2panda/driver/build_system/test/demo_hap/entry/c.sts rename to ets2panda/driver/build_system/test/demo_hap/entry/c.ets diff --git a/ets2panda/driver/build_system/test/demo_hap/entry/d.sts b/ets2panda/driver/build_system/test/demo_hap/entry/d.ets similarity index 100% rename from ets2panda/driver/build_system/test/demo_hap/entry/d.sts rename to ets2panda/driver/build_system/test/demo_hap/entry/d.ets diff --git a/ets2panda/driver/build_system/test/demo_hap/harA/index.sts b/ets2panda/driver/build_system/test/demo_hap/harA/index.ets similarity index 100% rename from ets2panda/driver/build_system/test/demo_hap/harA/index.sts rename to ets2panda/driver/build_system/test/demo_hap/harA/index.ets diff --git a/ets2panda/driver/build_system/test/demo_hap/harB/indexB.sts b/ets2panda/driver/build_system/test/demo_hap/harB/indexB.ets similarity index 100% rename from ets2panda/driver/build_system/test/demo_hap/harB/indexB.sts rename to ets2panda/driver/build_system/test/demo_hap/harB/indexB.ets diff --git a/ets2panda/es2panda.h b/ets2panda/es2panda.h index bd18a2294f5f581e4108cd4dcf10fec42d2262a2..12e724ff9bcde1a85aaf7280c9e2974f607c6de3 100644 --- a/ets2panda/es2panda.h +++ b/ets2panda/es2panda.h @@ -65,7 +65,7 @@ inline Language ToLanguage(ScriptExtension ext) return Language(Language::Id::TS); case ScriptExtension::AS: return Language(Language::Id::AS); - case ScriptExtension::STS: + case ScriptExtension::ETS: return Language(Language::Id::ETS); default: ES2PANDA_UNREACHABLE(); diff --git a/ets2panda/ir/expressions/identifier.cpp b/ets2panda/ir/expressions/identifier.cpp index 9bc10a098c83fbebf9e3d8113a44b3276ff5cc5b..3234d31f5d25bb584b07707aea481ce0c391bee0 100644 --- a/ets2panda/ir/expressions/identifier.cpp +++ b/ets2panda/ir/expressions/identifier.cpp @@ -75,7 +75,7 @@ Identifier *Identifier::Clone(ArenaAllocator *const allocator, AstNode *const pa Identifier *Identifier::CloneReference(ArenaAllocator *const allocator, AstNode *const parent) { auto *const clone = Clone(allocator, parent); - if (clone->IsReference(ScriptExtension::STS)) { + if (clone->IsReference(ScriptExtension::ETS)) { clone->SetTsTypeAnnotation(nullptr); } return clone; diff --git a/ets2panda/lexer/ETSLexer.cpp b/ets2panda/lexer/ETSLexer.cpp index e2719cb3fb97ec653b1572c5c30a529a78842600..4579e1873690098c05afbf411005b4f9a628f67c 100644 --- a/ets2panda/lexer/ETSLexer.cpp +++ b/ets2panda/lexer/ETSLexer.cpp @@ -96,7 +96,7 @@ void ETSLexer::CheckNumberLiteralEnd() bool ETSLexer::CheckUtf16Compatible(char32_t cp) const { if (cp >= util::StringView::Constants::CELESTIAL_OFFSET) { - // lexer_invalid_characters.sts + // lexer_invalid_characters.ets LogSyntaxError("Unsupported character literal"); return false; } diff --git a/ets2panda/lexer/ETSLexer.h b/ets2panda/lexer/ETSLexer.h index a4a40d6a1715b32152b4393aadf0caf656e6d1f7..0210139c52e63b1dfec5d98bcb7e7a47d25e263d 100644 --- a/ets2panda/lexer/ETSLexer.h +++ b/ets2panda/lexer/ETSLexer.h @@ -65,7 +65,7 @@ public: void CheckNumberLiteralEnd() override; void CheckNumberLiteralEndForIdentifier() override { - // don't need check in STS + // don't need check in ETS } bool CheckUtf16Compatible(char32_t cp) const; void ConvertNumber(NumberFlags flags) override; diff --git a/ets2panda/linter/README.md b/ets2panda/linter/README.md index 10d9ad8168882c82e4092e065bb995df2ca30ed2..ad020b2d6202e0cf319b6de28317a2059639dc88 100644 --- a/ets2panda/linter/README.md +++ b/ets2panda/linter/README.md @@ -1,7 +1,7 @@ # TypeScript linter Typescript linter ( further mentioned as 'linter' ) is a tool to check typescript sources and find language elements -and constructions which are deprecated to use in a purpose to migrate sources to STS. +and constructions which are deprecated to use in a purpose to migrate sources to ETS. The linter is currently under development. ## Prerequisits diff --git a/ets2panda/linter/scripts/update-test-results.mjs b/ets2panda/linter/scripts/update-test-results.mjs index b4b17b965f1b5ae866a282f499733aa50bc829fe..89da6fa798ccfa74bd74337cf3f4d999a064e505 100644 --- a/ets2panda/linter/scripts/update-test-results.mjs +++ b/ets2panda/linter/scripts/update-test-results.mjs @@ -19,8 +19,7 @@ import * as path from 'node:path'; const TS_EXT = ".ts"; const ETS_EXT = ".ets"; const TSX_EXT = ".tsx"; -const STS_EXT = ".sts"; -const STSX_EXT = ".stsx"; +const ETSX_EXT = ".etsx"; const D_TS_EXT = '.d.ts'; class Mode { @@ -74,9 +73,9 @@ function readJsonFile(filePath) { } function updateTestFile(testDir, testFile) { - // Temporary solution: rename '.sts' extension to '.ts' - if (testFile.endsWith(STS_EXT) || testFile.endsWith(STSX_EXT)) { - testFile = testFile.replace(STS_EXT, TS_EXT); + // Temporary solution: rename '.ets' extension to '.ts' + if (testFile.endsWith(ETS_EXT) || testFile.endsWith(ETSX_EXT)) { + testFile = testFile.replace(ETS_EXT, TS_EXT); } let testModes = [Mode.DEFAULT]; @@ -131,8 +130,7 @@ for (let testDir of testDirs) { (x.trimEnd().endsWith(TS_EXT) && !x.trimEnd().endsWith(D_TS_EXT)) || x.trimEnd().endsWith(TSX_EXT) || x.trimEnd().endsWith(ETS_EXT) || - x.trimEnd().endsWith(STS_EXT) || - x.trimEnd().endsWith(STSX_EXT) + x.trimEnd().endsWith(ETSX_EXT) ); if (!testFiles) continue; diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index a0b55b657d9a8ebe404fc75dc202062aa9a5b260..dfff6e09d44e2a46b92aba89eccc9a7c67f4fd9d 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -1466,8 +1466,8 @@ export class TypeScriptLinter { const leftSymbol = this.tsUtils.trueSymbolAtLocation(leftExpr); /* - * In STS, the left-hand side expression may be of any reference type, otherwise - * a compile-time error occurs. In addition, the left operand in STS cannot be a type. + * In ETS, the left-hand side expression may be of any reference type, otherwise + * a compile-time error occurs. In addition, the left operand in ETS cannot be a type. */ if (tsLhsExpr.kind === ts.SyntaxKind.ThisKeyword) { return; @@ -1612,7 +1612,7 @@ export class TypeScriptLinter { /* * In TS catch clause doesn't permit specification of the exception varible type except 'any' or 'unknown'. - * It is not compatible with STS 'catch' where the exception variable has to be of type + * It is not compatible with ETS 'catch' where the exception variable has to be of type * Error or derived from it. * So each 'catch' which has explicit type for the exception object goes to problems. */ diff --git a/ets2panda/linter/src/testRunner/TestRunner.ts b/ets2panda/linter/src/testRunner/TestRunner.ts index 942a269c6326425937d8a8fe90199a7e76e9401c..c1317e29db3c438ffa51ce4a3024f058f9d1b7a5 100755 --- a/ets2panda/linter/src/testRunner/TestRunner.ts +++ b/ets2panda/linter/src/testRunner/TestRunner.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -47,9 +47,9 @@ const RESULTS_DIR = 'results'; const ARGS_CONFIG_EXT = '.args.json'; const DIFF_EXT = '.diff'; -const TEST_EXTENSION_STS = '.sts'; -const TEST_EXTENSION_STSX = '.stsx'; -const TEST_EXTENSION_D_STS = '.d.sts'; +const TEST_EXTENSION_ETS = '.ets'; +const TEST_EXTENSION_ETSX = '.etsx'; +const TEST_EXTENSION_D_ETS = '.d.ets'; interface TestStatistics { passed: number; @@ -212,8 +212,8 @@ function runTests(): boolean { x.trimEnd().endsWith(ts.Extension.Ts) && !x.trimEnd().endsWith(ts.Extension.Dts) || x.trimEnd().endsWith(ts.Extension.Tsx) || x.trimEnd().endsWith(ts.Extension.Ets) || - x.trimEnd().endsWith(TEST_EXTENSION_STS) && !x.trimEnd().endsWith(TEST_EXTENSION_D_STS) || - x.trimEnd().endsWith(TEST_EXTENSION_STSX) + x.trimEnd().endsWith(TEST_EXTENSION_ETS) && !x.trimEnd().endsWith(TEST_EXTENSION_D_ETS) || + x.trimEnd().endsWith(TEST_EXTENSION_ETSX) ); }); runTestFiles(testFiles, testDir, testRunnerOpts, testStats); @@ -246,9 +246,9 @@ function runTestFiles( try { let renamed = false; let tsName = testFile; - if (testFile.includes(TEST_EXTENSION_STS)) { + if (testFile.includes(TEST_EXTENSION_ETS)) { renamed = true; - tsName = testFile.replace(TEST_EXTENSION_STS, ts.Extension.Ts); + tsName = testFile.replace(TEST_EXTENSION_ETS, ts.Extension.Ts); fs.renameSync(path.join(testDir, testFile), path.join(testDir, tsName)); } runTestFile({ testDir, testFile: tsName, testRunnerOpts }, testStats); diff --git a/ets2panda/linter/test/extended_features/@arkts.collections.d.ets b/ets2panda/linter/test/extended_features/@arkts.collections.d.ets index 9500b429be0eee529206b84b4c9ee8e80f96bb98..70313afffa6354c4138881d74b3b1693ff533b3d 100644 --- a/ets2panda/linter/test/extended_features/@arkts.collections.d.ets +++ b/ets2panda/linter/test/extended_features/@arkts.collections.d.ets @@ -18,6 +18,7 @@ * @kit ArkTS */ +// After change extention .sts to .ets got an errors #23897 import lang from './@arkts.lang' /** diff --git a/ets2panda/linter/test/extended_features/@arkts.collections.d.ets.json b/ets2panda/linter/test/extended_features/@arkts.collections.d.ets.json deleted file mode 100644 index 167c48a33a21c1fc3895d8e8830b9cf767800a97..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/extended_features/@arkts.collections.d.ets.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "copyright": [ - "Copyright (c) 2024 Huawei Device Co., Ltd.", - "Licensed under the Apache License, Version 2.0 (the 'License');", - "you may not use this file except in compliance with the License.", - "You may obtain a copy of the License at", - "", - "http://www.apache.org/licenses/LICENSE-2.0", - "", - "Unless required by applicable law or agreed to in writing, software", - "distributed under the License is distributed on an 'AS IS' BASIS,", - "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", - "See the License for the specific language governing permissions and", - "limitations under the License." - ], - "result": [] -} \ No newline at end of file diff --git a/ets2panda/linter/test/extended_features/@arkts.collections.d.ts.json b/ets2panda/linter/test/extended_features/@arkts.collections.d.ts.json new file mode 100644 index 0000000000000000000000000000000000000000..3bf893090b90ec15a030b8f0fcc1b675e4a6afbb --- /dev/null +++ b/ets2panda/linter/test/extended_features/@arkts.collections.d.ts.json @@ -0,0 +1,118 @@ +{ + "copyright": [ + "Copyright (c) 2024 Huawei Device Co., Ltd.", + "Licensed under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + "http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an 'AS IS' BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + ], + "result": [ + { + "line": 477, + "column": 5, + "endLine": 477, + "endColumn": 22, + "problem": "SendableComputedPropName", + "suggest": "", + "rule": "Computed property names are not allowed in \"Sendable\" classes and interfaces (arkts-sendable-computed-prop-name)", + "severity": "ERROR" + }, + { + "line": 628, + "column": 5, + "endLine": 628, + "endColumn": 24, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 1567, + "column": 5, + "endLine": 1567, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 2042, + "column": 5, + "endLine": 2042, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 2607, + "column": 5, + "endLine": 2607, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 3172, + "column": 5, + "endLine": 3172, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 3736, + "column": 5, + "endLine": 3736, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 4301, + "column": 5, + "endLine": 4301, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 4865, + "column": 5, + "endLine": 4865, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + }, + { + "line": 5128, + "column": 5, + "endLine": 5128, + "endColumn": 29, + "problem": "IndexMember", + "suggest": "", + "rule": "Indexed signatures are not supported (arkts-no-indexed-signatures)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/extended_features/indexable_collections_array.ets.json b/ets2panda/linter/test/extended_features/indexable_collections_array.ts.json similarity index 100% rename from ets2panda/linter/test/extended_features/indexable_collections_array.ets.json rename to ets2panda/linter/test/extended_features/indexable_collections_array.ts.json diff --git a/ets2panda/linter/test/main/@arkts.lang.d.ets.json b/ets2panda/linter/test/main/@arkts.lang.d.ts.json similarity index 100% rename from ets2panda/linter/test/main/@arkts.lang.d.ets.json rename to ets2panda/linter/test/main/@arkts.lang.d.ts.json diff --git a/ets2panda/linter/test/main/UMD_module.ets b/ets2panda/linter/test/main/UMD_module.ets new file mode 100644 index 0000000000000000000000000000000000000000..1f98817acbd8b4932ea41458bf6ff0b6adaf9962 --- /dev/null +++ b/ets2panda/linter/test/main/UMD_module.ets @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022-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 function isPrime(x: number): boolean; +export as namespace mathLib; diff --git a/ets2panda/linter/test/main/UMD_module.sts b/ets2panda/linter/test/main/UMD_module.sts deleted file mode 100644 index 9db07b83ebb68fe6b76da78ffdf18f65e4e1707d..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/UMD_module.sts +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export function isPrime(x: number): boolean; -export as namespace mathLib; diff --git a/ets2panda/linter/test/main/ambient_module.ets b/ets2panda/linter/test/main/ambient_module.ets new file mode 100644 index 0000000000000000000000000000000000000000..d6a596fa9da55e8612e96c05d54415ed98570436 --- /dev/null +++ b/ets2panda/linter/test/main/ambient_module.ets @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022-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. + */ + +declare module 'ambient-module'; + +class C {} + +declare module '*!text' { + const content: string; + export default content; +} + +declare module 'json!*' { + const value: any; + export default value; +} + +declare module 'library/*'; + +declare module 'abc' { + export function f(): void; +} + +declare namespace abc2 { + export function f(): void; + function foo(): void; + export { foo } +} diff --git a/ets2panda/linter/test/main/ambient_module.sts b/ets2panda/linter/test/main/ambient_module.sts deleted file mode 100644 index c8d9d800c75c30bb68c7b98af761ec6473cbb34f..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/ambient_module.sts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -declare module 'ambient-module'; - -class C {} - -declare module '*!text' { - const content: string; - export default content; -} - -declare module 'json!*' { - const value: any; - export default value; -} - -declare module 'library/*'; - -declare module 'abc' { - export function f(): void; -} - -declare namespace abc2 { - export function f(): void; - function foo(): void; - export { foo } -} diff --git a/ets2panda/linter/test/main/animatable_extend_decorator_1.sts b/ets2panda/linter/test/main/animatable_extend_decorator_1.ets similarity index 100% rename from ets2panda/linter/test/main/animatable_extend_decorator_1.sts rename to ets2panda/linter/test/main/animatable_extend_decorator_1.ets diff --git a/ets2panda/linter/test/main/animatable_extend_decorator_2.sts b/ets2panda/linter/test/main/animatable_extend_decorator_2.ets similarity index 100% rename from ets2panda/linter/test/main/animatable_extend_decorator_2.sts rename to ets2panda/linter/test/main/animatable_extend_decorator_2.ets diff --git a/ets2panda/linter/test/main/arkts_no_duplicate_function_name1.sts b/ets2panda/linter/test/main/arkts_no_duplicate_function_name1.ets similarity index 100% rename from ets2panda/linter/test/main/arkts_no_duplicate_function_name1.sts rename to ets2panda/linter/test/main/arkts_no_duplicate_function_name1.ets diff --git a/ets2panda/linter/test/main/arkts_no_duplicate_function_name2.sts b/ets2panda/linter/test/main/arkts_no_duplicate_function_name2.ets similarity index 100% rename from ets2panda/linter/test/main/arkts_no_duplicate_function_name2.sts rename to ets2panda/linter/test/main/arkts_no_duplicate_function_name2.ets diff --git a/ets2panda/linter/test/main/arkts_sendable_imported_variables.ets b/ets2panda/linter/test/main/arkts_sendable_imported_variables.ets new file mode 100644 index 0000000000000000000000000000000000000000..7e047e9cc1b90074bcac3c9796269669c4315b68 --- /dev/null +++ b/ets2panda/linter/test/main/arkts_sendable_imported_variables.ets @@ -0,0 +1,161 @@ +/* + * 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. + */ + +@Sendable +class SendableClassA { + a: number = 1; +} + +@Sendable +class SendableClassB { + b: SendableClassA = new SendableClassA(); +} + +@Sendable +class SendableClassC { + static a: number = 1; +} + +@Sendable +class SendableClassD { + b: number = SendableClassC.a; +} + +@Sendable +class SendableClassE { + static public a: number = 1; + f(p: number) { + @Sendable + class SendableClassF { + public b: number = SendableClassE.a; + } + } +} + +if (true) { + @Sendable + class SendableClassG { + static a: number = 1; + } + + @Sendable + class SendableClassH { + b: SendableClassG = new SendableClassG(); //ERROR + } +} + +let a: number = 1 + +@Sendable +class SendableClassI { + b: number = a; //ERROR +} + + +@Sendable +class SendableClassJ { + p: number = 1; +} +let b:SendableClassJ = new SendableClassJ(); + +@Sendable +class SendableClassK { + ma: SendableClassJ = b; // ERROR +} + +// + +@Sendable +class SendableLocalClassA { + +} + +@Sendable +function sendableFunctionA() { + +}; + +@Sendable +export class SendableLocalClassB { + +} + +@Sendable +export function sendableFunctionB() { + +}; + +@Sendable +class SendableLocalClassC { + +} + +@Sendable +function sendableFunctionC() { + +}; + +@Sendable +class SendableLocalClassD { + +} + +@Sendable +function sendableFunctionD() { + +}; + +@Sendable +class SendableLocalClassE { + +} + +@Sendable +function sendableFunctionE() { + +}; + +@Sendable +class SendableClass { + handle() { + new SendableLocalClassA(); // OK + sendableFunctionA(); // OK + new SendableLocalClassB(); // WARING, export decl + sendableFunctionB(); // WARING, export decl + new SendableLocalClassC(); // WARING, export { decl } + sendableFunctionC(); // WARING, export { decl } + new SendableLocalClassD(); // WARING, export { decl as alias } + sendableFunctionD(); // WARING, export { decl as alias } + new SendableLocalClassE(); // WARING, export default decl + } +} + +@Sendable +function sendableFunction() { + new SendableLocalClassA(); // OK + sendableFunctionA(); // OK + new SendableLocalClassB(); // WARING, export decl + sendableFunctionB(); // WARING, export decl + new SendableLocalClassC(); // WARING, export { decl } + sendableFunctionC(); // WARING, export { decl } + new SendableLocalClassD(); // WARING, export { decl as alias } + sendableFunctionD(); // WARING, export { decl as alias } + new SendableLocalClassE(); // WARING, export default decl +} + +export { SendableLocalClassC, sendableFunctionC, SendableLocalClassD as Sc42, sendableFunctionD as sf42 }; + + +export default SendableLocalClassE; \ No newline at end of file diff --git a/ets2panda/linter/test/main/arkts_sendable_imported_variables.sts b/ets2panda/linter/test/main/arkts_sendable_imported_variables.sts deleted file mode 100644 index 007ed9c52ac5dccee1f6dac1e77ce2964867f212..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/arkts_sendable_imported_variables.sts +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -@Sendable -class SendableClassA { - a: number = 1; -} - -@Sendable -class SendableClassB { - b: SendableClassA = new SendableClassA(); -} - -@Sendable -class SendableClassC { - static a: number = 1; -} - -@Sendable -class SendableClassD { - b: number = SendableClassC.a; -} - -@Sendable -class SendableClassE { - static public a: number = 1; - f(p: number) { - @Sendable - class SendableClassF { - public b: number = SendableClassE.a; - } - } -} - -if (true) { - @Sendable - class SendableClassG { - static a: number = 1; - } - - @Sendable - class SendableClassH { - b: SendableClassG = new SendableClassG(); //ERROR - } -} - -let a: number = 1 - -@Sendable -class SendableClassI { - b: number = a; //ERROR -} - - -@Sendable -class SendableClassJ { - p: number = 1; -} -let b:SendableClassJ = new SendableClassJ(); - -@Sendable -class SendableClassK { - ma: SendableClassJ = b; // ERROR -} - -// - -@Sendable -class SendableLocalClassA { - -} - -@Sendable -function sendableFunctionA() { - -}; - -@Sendable -export class SendableLocalClassB { - -} - -@Sendable -export function sendableFunctionB() { - -}; - -@Sendable -class SendableLocalClassC { - -} - -@Sendable -function sendableFunctionC() { - -}; - -@Sendable -class SendableLocalClassD { - -} - -@Sendable -function sendableFunctionD() { - -}; - -@Sendable -class SendableLocalClassE { - -} - -@Sendable -function sendableFunctionE() { - -}; - -@Sendable -class SendableClass { - handle() { - new SendableLocalClassA(); // OK - sendableFunctionA(); // OK - new SendableLocalClassB(); // WARING, export decl - sendableFunctionB(); // WARING, export decl - new SendableLocalClassC(); // WARING, export { decl } - sendableFunctionC(); // WARING, export { decl } - new SendableLocalClassD(); // WARING, export { decl as alias } - sendableFunctionD(); // WARING, export { decl as alias } - new SendableLocalClassE(); // WARING, export default decl - } -} - -@Sendable -function sendableFunction() { - new SendableLocalClassA(); // OK - sendableFunctionA(); // OK - new SendableLocalClassB(); // WARING, export decl - sendableFunctionB(); // WARING, export decl - new SendableLocalClassC(); // WARING, export { decl } - sendableFunctionC(); // WARING, export { decl } - new SendableLocalClassD(); // WARING, export { decl as alias } - sendableFunctionD(); // WARING, export { decl as alias } - new SendableLocalClassE(); // WARING, export default decl -} - -export { SendableLocalClassC, sendableFunctionC, SendableLocalClassD as Sc42, sendableFunctionD as sf42 }; - - -export default SendableLocalClassE; \ No newline at end of file diff --git a/ets2panda/linter/test/main/arkui_decorators.ets b/ets2panda/linter/test/main/arkui_decorators.ets new file mode 100644 index 0000000000000000000000000000000000000000..5dc71c8e5bd844a3ced93c0db11f0a7bb1d8ca10 --- /dev/null +++ b/ets2panda/linter/test/main/arkui_decorators.ets @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2022-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. + */ + +type PropDecorator = (target: any, propertyKey: any) => void; +type PropDecoratorCall = ((val: string) => PropDecorator); +type PropDecoratorWithCall = PropDecorator & PropDecoratorCall; + +let Builder: PropDecorator; +let BuilderParam: PropDecorator; +let Component: PropDecorator = (x, y) => {}; +let Consume: PropDecoratorWithCall; +let Entry: PropDecoratorWithCall = () => ((x, y) => {}); +let Link: PropDecorator; +let LocalStorageLink: PropDecoratorCall; +let LocalStorageProp: PropDecoratorCall; +let ObjectLink: PropDecorator; +let Observed: PropDecorator = (x, y) => {}; +let Prop: PropDecorator; +let Provide: PropDecoratorWithCall; +let State: PropDecorator; +let StorageLink: PropDecoratorCall; +let StorageProp: PropDecoratorCall; +let Styles: PropDecorator; +let Watch: PropDecoratorCall; +let Concurrent: PropDecorator = (x, y) => {}; + +let InvalidDecorator: PropDecorator = (x, y) => {}; +let InvalidDecoratorWithCall: PropDecoratorWithCall = () => ((x, y) => {}); + +@Observed class Person { + name: string; + age: number; +} + +@Component +class PersonView { + @BuilderParam + builderParam: number; + + @Consume + @Consume('1') + consume: string; + + @Link + link: number; + + @LocalStorageLink('2') + @LocalStorageProp('3') + localStorage: string; + + @ObjectLink + objLink: string; + + @Prop + prop: string; + + @Provide + @Provide('4') + provide: number; + + @State + state: number; + + @StorageLink('5') + @StorageProp('6') + storage: string; + + @Styles + style: string; + + @Watch('7') + watch: number; + + @Builder + foo() {} +} + +@Entry +@Component +class PageEntry { + @InvalidDecorator + invalid: string; + + @Builder + @InvalidDecoratorWithCall('10') + bar() {} +} + +@InvalidDecorator +class C {} + +@Concurrent +class ConcurrentArray { + data: Object[]; +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/arkui_decorators.sts b/ets2panda/linter/test/main/arkui_decorators.sts deleted file mode 100644 index 6602e976fa619933635f1dc1b815a72d71983a7c..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/arkui_decorators.sts +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -type PropDecorator = (target: any, propertyKey: any) => void; -type PropDecoratorCall = ((val: string) => PropDecorator); -type PropDecoratorWithCall = PropDecorator & PropDecoratorCall; - -let Builder: PropDecorator; -let BuilderParam: PropDecorator; -let Component: PropDecorator = (x, y) => {}; -let Consume: PropDecoratorWithCall; -let Entry: PropDecoratorWithCall = () => ((x, y) => {}); -let Link: PropDecorator; -let LocalStorageLink: PropDecoratorCall; -let LocalStorageProp: PropDecoratorCall; -let ObjectLink: PropDecorator; -let Observed: PropDecorator = (x, y) => {}; -let Prop: PropDecorator; -let Provide: PropDecoratorWithCall; -let State: PropDecorator; -let StorageLink: PropDecoratorCall; -let StorageProp: PropDecoratorCall; -let Styles: PropDecorator; -let Watch: PropDecoratorCall; -let Concurrent: PropDecorator = (x, y) => {}; - -let InvalidDecorator: PropDecorator = (x, y) => {}; -let InvalidDecoratorWithCall: PropDecoratorWithCall = () => ((x, y) => {}); - -@Observed class Person { - name: string; - age: number; -} - -@Component -class PersonView { - @BuilderParam - builderParam: number; - - @Consume - @Consume('1') - consume: string; - - @Link - link: number; - - @LocalStorageLink('2') - @LocalStorageProp('3') - localStorage: string; - - @ObjectLink - objLink: string; - - @Prop - prop: string; - - @Provide - @Provide('4') - provide: number; - - @State - state: number; - - @StorageLink('5') - @StorageProp('6') - storage: string; - - @Styles - style: string; - - @Watch('7') - watch: number; - - @Builder - foo() {} -} - -@Entry -@Component -class PageEntry { - @InvalidDecorator - invalid: string; - - @Builder - @InvalidDecoratorWithCall('10') - bar() {} -} - -@InvalidDecorator -class C {} - -@Concurrent -class ConcurrentArray { - data: Object[]; -} \ No newline at end of file diff --git a/ets2panda/linter/test/main/array_index_expr_type.sts b/ets2panda/linter/test/main/array_index_expr_type.ets similarity index 100% rename from ets2panda/linter/test/main/array_index_expr_type.sts rename to ets2panda/linter/test/main/array_index_expr_type.ets diff --git a/ets2panda/linter/test/main/array_indexing_iteration.ets b/ets2panda/linter/test/main/array_indexing_iteration.ets new file mode 100644 index 0000000000000000000000000000000000000000..3a3bdd1f026a26df7b10ea4426d4b2f9469c03af --- /dev/null +++ b/ets2panda/linter/test/main/array_indexing_iteration.ets @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2022-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. + */ + +{ + function rOnlyNum(num: readonly number[], num1: number[]): number { + let arr: number[] = num as number[] + let b = num[0] + let c = num1[0] + return arr[0] + } + + let arr: number[] = [1,2,3] + let arrNum = rOnlyNum(arr, arr) + + let s: number[] = [1,2,3] + for (let c of s) { + console.log(s) + } +} +{ + function rOnlyNum(num: readonly ArrayLike, num1: ArrayLike): number { + let arr: ArrayLike = num as ArrayLike + let b = num[0] + let c = num1[0] + return arr[0] + } + + let arr: ArrayLike = [1,2,3] + let arrNum = rOnlyNum(arr, arr) + + let s: ArrayLike = [1,2,3] + for (let c of s) { + console.log(s) + } +} +{ + function rOnlyNum(num: readonly ConcatArray, num1: ConcatArray): number { + let arr: ConcatArray = num as ConcatArray + let b = num[0] + let c = num1[0] + return arr[0] + } + + let arr: ConcatArray = [1,2,3] + let arrNum = rOnlyNum(arr, arr) + + let s: ConcatArray = [1,2,3] + for (let c of s) { + console.log(s) + } +} +{ + function rOnlyNum(num: readonly ReadonlyArray, num1: ReadonlyArray): number { + let arr: ReadonlyArray = num as ReadonlyArray + let b = num[0] + let c = num1[0] + return arr[0] + } + + let arr: ReadonlyArray = [1,2,3] + let arrNum = rOnlyNum(arr, arr) + + let s: ReadonlyArray = [1,2,3] + for (let c of s) { + console.log(s) + } +} +{ + function rOnlyNum(num: readonly Array, num1: Array): number { + let arr: Array = num as Array + let b = num[0] + let c = num1[0] + return arr[0] + } + + let arr: Array = [1,2,3] + let arrNum = rOnlyNum(arr, arr) + + let s: Array = [1,2,3] + for (let c of s) { + console.log(s) + } +} diff --git a/ets2panda/linter/test/main/array_indexing_iteration.sts b/ets2panda/linter/test/main/array_indexing_iteration.sts deleted file mode 100644 index dba746bc69fc2835617352b4a09123ac43d67223..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/array_indexing_iteration.sts +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -{ - function rOnlyNum(num: readonly number[], num1: number[]): number { - let arr: number[] = num as number[] - let b = num[0] - let c = num1[0] - return arr[0] - } - - let arr: number[] = [1,2,3] - let arrNum = rOnlyNum(arr, arr) - - let s: number[] = [1,2,3] - for (let c of s) { - console.log(s) - } -} -{ - function rOnlyNum(num: readonly ArrayLike, num1: ArrayLike): number { - let arr: ArrayLike = num as ArrayLike - let b = num[0] - let c = num1[0] - return arr[0] - } - - let arr: ArrayLike = [1,2,3] - let arrNum = rOnlyNum(arr, arr) - - let s: ArrayLike = [1,2,3] - for (let c of s) { - console.log(s) - } -} -{ - function rOnlyNum(num: readonly ConcatArray, num1: ConcatArray): number { - let arr: ConcatArray = num as ConcatArray - let b = num[0] - let c = num1[0] - return arr[0] - } - - let arr: ConcatArray = [1,2,3] - let arrNum = rOnlyNum(arr, arr) - - let s: ConcatArray = [1,2,3] - for (let c of s) { - console.log(s) - } -} -{ - function rOnlyNum(num: readonly ReadonlyArray, num1: ReadonlyArray): number { - let arr: ReadonlyArray = num as ReadonlyArray - let b = num[0] - let c = num1[0] - return arr[0] - } - - let arr: ReadonlyArray = [1,2,3] - let arrNum = rOnlyNum(arr, arr) - - let s: ReadonlyArray = [1,2,3] - for (let c of s) { - console.log(s) - } -} -{ - function rOnlyNum(num: readonly Array, num1: Array): number { - let arr: Array = num as Array - let b = num[0] - let c = num1[0] - return arr[0] - } - - let arr: Array = [1,2,3] - let arrNum = rOnlyNum(arr, arr) - - let s: Array = [1,2,3] - for (let c of s) { - console.log(s) - } -} diff --git a/ets2panda/linter/test/main/array_literal_spread.ets b/ets2panda/linter/test/main/array_literal_spread.ets new file mode 100644 index 0000000000000000000000000000000000000000..004b101bcb950b47c26b5bbd9e599fedf69e1367 --- /dev/null +++ b/ets2panda/linter/test/main/array_literal_spread.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022-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. + */ + +class DerivedFromArray extends Uint16Array { +}; + +let arr1 = [1, 2, 3]; +let arr2 = new Uint16Array([4, 5, 6]); +let arr3 = new DerivedFromArray([7, 8, 9]) + +let arr4 = [...arr1, 10, ...arr2, 11, ...arr3] diff --git a/ets2panda/linter/test/main/array_literal_spread.sts b/ets2panda/linter/test/main/array_literal_spread.sts deleted file mode 100644 index 31be96b7681c28509b44e3bfccd7df4a45afab7b..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/array_literal_spread.sts +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class DerivedFromArray extends Uint16Array { -}; - -let arr1 = [1, 2, 3]; -let arr2 = new Uint16Array([4, 5, 6]); -let arr3 = new DerivedFromArray([7, 8, 9]) - -let arr4 = [...arr1, 10, ...arr2, 11, ...arr3] diff --git a/ets2panda/linter/test/main/array_literals.ets b/ets2panda/linter/test/main/array_literals.ets new file mode 100644 index 0000000000000000000000000000000000000000..62ed45e3efca56b78f616d893a0f0b5b34965529 --- /dev/null +++ b/ets2panda/linter/test/main/array_literals.ets @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2023-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. + */ + +class C { + a: any; + b: any[]; + c: [number, number]; + d: number[]; +} + +function arrayLiteralsInVarDecl(): void { + const a = [1, 2]; // NOW OK + const b: any = [3, 4]; // NOW OK + const c: any[] = [5, 6]; // OK + const d: [number, number] = [7, 8]; // NOW OK + const e: number[] = [9, 10]; // OK + const f = [1, 'x', true]; // NOW OK + const g: object[] = [2, 'y', false]; // OK + + const h: C = { + a: [1, 2], // NOW OK + b: [3, 4], // OK + c: [5, 6], // NOW OK + d: [7, 8], // OK + }; + + const x = [1, 2, 3][1]; // NOW OK +} + +let a: number[]; +let b: any; +let c: any[]; +let d: [number, number]; +let e: number[]; +let f: [number, string, boolean]; +let g: object[]; +let h: C; + +function arrayLiteralsInAssignment(): void { + a = [1, 2]; // OK + b = [3, 4]; // NOW OK + c = [5, 6]; // OK + d = [7, 8]; // NOW OK + e = [9, 10]; // OK + f = [1, 'x', true]; // NOW OK for ARR.LITERAL + g = [2, 'y', false]; // OK + + h = { + a: [1, 2], // NOW OK + b: [3, 4], // OK + c: [5, 6], // NOW OK + d: [7, 8], // OK + }; +} + +// Default parameter value +function foo(x = [1, 2]) { + return x; +} // NOW OK +function foo2(x: any = [3, 4]) { + return x; +} // NOW OK +function foo3(x: any[] = [5, 6]) { + return x; +} // OK +function foo4(x: [number, number] = [7, 8]) { + return x; +} // NOW OK +function foo5(x: number[] = [9, 10]) { + return x; +} // OK + +function arrayLiteralsInFunCall(): void { + foo([1, 2]); // OK + foo2([3, 4]); // NOW OK + foo3([5, 6]); // OK + foo4([7, 8]); // NOW OK + foo5([9, 10]); // OK +} + +// Return from function +function bar() { + return [1, 2]; +} // NOW OK +function bar2(): any { + return [3, 4]; +} // NOW OK +function bar3(): any[] { + return [5, 6]; +} // OK +function bar4(): [number, number] { + return [7, 8]; +} // NOW OK +function bar5(): number[] { + return [9, 10]; +} // OK + +function arrayLiteralsInTernaryOp(): void { + const condition = true; + a = condition ? [1, 2] : [3, 4]; // OK + b = condition ? [5, 6] : [7, 8]; // NOW OK + c = condition ? [9, 10] : [11, 12]; // OK + d = condition ? [13, 14] : [15, 16]; // NOW OK + e = condition ? [17, 18] : [19, 20]; // OK +} + +class P { + n:number = 0; + s:string = ''; +} + +let a1 = [ { n:1, s:"1" } as P, { n:2, s:"2" } as P ]; // OK +let a2: P[] = [ { n:3, s:"3" }, { n:4, s:"4" } ]; // OK +let a3 = [ { n:1, s:"1" }, { n:2, s:"2" } ]; // NOT OK diff --git a/ets2panda/linter/test/main/array_literals.sts b/ets2panda/linter/test/main/array_literals.sts deleted file mode 100644 index 45ce7ccf0659398f92725fc5f5e6e61a69877743..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/array_literals.sts +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class C { - a: any; - b: any[]; - c: [number, number]; - d: number[]; -} - -function arrayLiteralsInVarDecl(): void { - const a = [1, 2]; // NOW OK - const b: any = [3, 4]; // NOW OK - const c: any[] = [5, 6]; // OK - const d: [number, number] = [7, 8]; // NOW OK - const e: number[] = [9, 10]; // OK - const f = [1, 'x', true]; // NOW OK - const g: object[] = [2, 'y', false]; // OK - - const h: C = { - a: [1, 2], // NOW OK - b: [3, 4], // OK - c: [5, 6], // NOW OK - d: [7, 8], // OK - }; - - const x = [1, 2, 3][1]; // NOW OK -} - -let a: number[]; -let b: any; -let c: any[]; -let d: [number, number]; -let e: number[]; -let f: [number, string, boolean]; -let g: object[]; -let h: C; - -function arrayLiteralsInAssignment(): void { - a = [1, 2]; // OK - b = [3, 4]; // NOW OK - c = [5, 6]; // OK - d = [7, 8]; // NOW OK - e = [9, 10]; // OK - f = [1, 'x', true]; // NOW OK for ARR.LITERAL - g = [2, 'y', false]; // OK - - h = { - a: [1, 2], // NOW OK - b: [3, 4], // OK - c: [5, 6], // NOW OK - d: [7, 8], // OK - }; -} - -// Default parameter value -function foo(x = [1, 2]) { - return x; -} // NOW OK -function foo2(x: any = [3, 4]) { - return x; -} // NOW OK -function foo3(x: any[] = [5, 6]) { - return x; -} // OK -function foo4(x: [number, number] = [7, 8]) { - return x; -} // NOW OK -function foo5(x: number[] = [9, 10]) { - return x; -} // OK - -function arrayLiteralsInFunCall(): void { - foo([1, 2]); // OK - foo2([3, 4]); // NOW OK - foo3([5, 6]); // OK - foo4([7, 8]); // NOW OK - foo5([9, 10]); // OK -} - -// Return from function -function bar() { - return [1, 2]; -} // NOW OK -function bar2(): any { - return [3, 4]; -} // NOW OK -function bar3(): any[] { - return [5, 6]; -} // OK -function bar4(): [number, number] { - return [7, 8]; -} // NOW OK -function bar5(): number[] { - return [9, 10]; -} // OK - -function arrayLiteralsInTernaryOp(): void { - const condition = true; - a = condition ? [1, 2] : [3, 4]; // OK - b = condition ? [5, 6] : [7, 8]; // NOW OK - c = condition ? [9, 10] : [11, 12]; // OK - d = condition ? [13, 14] : [15, 16]; // NOW OK - e = condition ? [17, 18] : [19, 20]; // OK -} - -class P { - n:number = 0; - s:string = ''; -} - -let a1 = [ { n:1, s:"1" } as P, { n:2, s:"2" } as P ]; // OK -let a2: P[] = [ { n:3, s:"3" }, { n:4, s:"4" } ]; // OK -let a3 = [ { n:1, s:"1" }, { n:2, s:"2" } ]; // NOT OK diff --git a/ets2panda/linter/test/main/assign_to_method.ets b/ets2panda/linter/test/main/assign_to_method.ets new file mode 100644 index 0000000000000000000000000000000000000000..5633e7948cb88218ab4ea459a64713555b568231 --- /dev/null +++ b/ets2panda/linter/test/main/assign_to_method.ets @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022-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. + */ + +class A { + foo():boolean { + return false; + } + + static bar():void { + } +} + +let a = new A(); +a.foo = ():boolean => { return false; } +A.bar = ():void => {}; + +interface I { + foo(): void; +} + +function foo2(i: I) { + i.foo = () => {}; +} diff --git a/ets2panda/linter/test/main/assign_to_method.sts b/ets2panda/linter/test/main/assign_to_method.sts deleted file mode 100644 index b56a5637f81358b944fca318a842d6d836110fcf..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/assign_to_method.sts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class A { - foo():boolean { - return false; - } - - static bar():void { - } -} - -let a = new A(); -a.foo = ():boolean => { return false; } -A.bar = ():void => {}; - -interface I { - foo(): void; -} - -function foo2(i: I) { - i.foo = () => {}; -} diff --git a/ets2panda/linter/test/main/avoid_using_union_types.sts b/ets2panda/linter/test/main/avoid_using_union_types.ets similarity index 100% rename from ets2panda/linter/test/main/avoid_using_union_types.sts rename to ets2panda/linter/test/main/avoid_using_union_types.ets diff --git a/ets2panda/linter/test/main/binary_wrong_types.ets b/ets2panda/linter/test/main/binary_wrong_types.ets new file mode 100644 index 0000000000000000000000000000000000000000..0d3609bd4eb575d3eac339454d683e7a782cd6ec --- /dev/null +++ b/ets2panda/linter/test/main/binary_wrong_types.ets @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2022-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. + */ + +function gets(): string { + return 'ss'; +} + +const b0 = true; + +const c0 = b0 && true; +const c1 = true && '3'; +const c2 = gets() && false; +const c3 = 5 && 6; + +const d0 = false || b0; +const d1 = ~'3' || true; +const d2 = false || gets(); +const d3 = 4 || 5; + +console.log(c1); +console.log(c2); +console.log(c3); +console.log(d1); +console.log(d2); +console.log(d3); + +const varAny: any = null; + +const add0 = 2 + 2; +const add1 = '2' + '2'; +const add2 = '2' + varAny; +const add3 = varAny + '2'; +const add4 = varAny + 2; +const add5 = 6 + varAny; +const add6 = '2' + 2; +const add7 = 2 + '2'; + +enum Const { + PI = 3.14, + E = 2.7818, +} +enum State { + OK = 'ok', + FAULT = 'fail', +} + +const b1 = 7 ^ varAny; +const b2 = 7 | varAny; +const b3 = 7 & varAny; + +const b4 = 7 << varAny; +const b5 = 7 >> varAny; +const b6 = 7 >>> varAny; + +const b7 = varAny << 1; +const b8 = varAny >> 2; +const b9 = varAny >>> 3; + +const b11 = 7 ^ Const.PI; +const b12 = 7 | Const.E; +const b13 = 7 & Const.PI; + +const b14 = 7 << Const.PI; +const b15 = 7 >> Const.E; +const b16 = 7 >>> Const.PI; + +const b17 = Const.PI << 1; +const b18 = Const.E >> 2; +const b19 = Const.PI >>> 3; + +const b31 = State.OK ^ 7; +const b32 = 7 | State.FAULT; +const b33 = 7 & State.OK; + +const b34 = 7 << State.OK; +const b35 = 7 >> State.FAULT; +const b36 = 7 >>> State.OK; + +const b37 = State.FAULT << 1; +const b38 = State.OK >> 2; +const b39 = State.FAULT >>> 3; + +const a000 = ((k = 10), 2 + 7); + +function foo(n: number, m: number): number { + return (n/m) + ((n%m) ? 1 : 0); +} + +const b40 = 7 & 5.5; +const b41 = 5.5 & b40; +const b42 = 2 | 5.5; +const b43 = 5.5 | b42; +const b44 = 4 ^ 5.5; +const b45 = 5.5 ^ b44; + + +let e1 = Const.PI + Const.E; // OK +let e2 = State.FAULT + State.OK; // OK +let e3 = "State: " + State.FAULT; // OK +let e4 = "Const = " + Const.PI; // OK + +let f1 = Const.PI + State.FAULT; // Error +let f2 = State.OK + Const.E; // Error diff --git a/ets2panda/linter/test/main/binary_wrong_types.sts b/ets2panda/linter/test/main/binary_wrong_types.sts deleted file mode 100644 index 6e3de7154b2466f740f7d54d430c0329c98e7d02..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/binary_wrong_types.sts +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function gets(): string { - return 'ss'; -} - -const b0 = true; - -const c0 = b0 && true; -const c1 = true && '3'; -const c2 = gets() && false; -const c3 = 5 && 6; - -const d0 = false || b0; -const d1 = ~'3' || true; -const d2 = false || gets(); -const d3 = 4 || 5; - -console.log(c1); -console.log(c2); -console.log(c3); -console.log(d1); -console.log(d2); -console.log(d3); - -const varAny: any = null; - -const add0 = 2 + 2; -const add1 = '2' + '2'; -const add2 = '2' + varAny; -const add3 = varAny + '2'; -const add4 = varAny + 2; -const add5 = 6 + varAny; -const add6 = '2' + 2; -const add7 = 2 + '2'; - -enum Const { - PI = 3.14, - E = 2.7818, -} -enum State { - OK = 'ok', - FAULT = 'fail', -} - -const b1 = 7 ^ varAny; -const b2 = 7 | varAny; -const b3 = 7 & varAny; - -const b4 = 7 << varAny; -const b5 = 7 >> varAny; -const b6 = 7 >>> varAny; - -const b7 = varAny << 1; -const b8 = varAny >> 2; -const b9 = varAny >>> 3; - -const b11 = 7 ^ Const.PI; -const b12 = 7 | Const.E; -const b13 = 7 & Const.PI; - -const b14 = 7 << Const.PI; -const b15 = 7 >> Const.E; -const b16 = 7 >>> Const.PI; - -const b17 = Const.PI << 1; -const b18 = Const.E >> 2; -const b19 = Const.PI >>> 3; - -const b31 = State.OK ^ 7; -const b32 = 7 | State.FAULT; -const b33 = 7 & State.OK; - -const b34 = 7 << State.OK; -const b35 = 7 >> State.FAULT; -const b36 = 7 >>> State.OK; - -const b37 = State.FAULT << 1; -const b38 = State.OK >> 2; -const b39 = State.FAULT >>> 3; - -const a000 = ((k = 10), 2 + 7); - -function foo(n: number, m: number): number { - return (n/m) + ((n%m) ? 1 : 0); -} - -const b40 = 7 & 5.5; -const b41 = 5.5 & b40; -const b42 = 2 | 5.5; -const b43 = 5.5 | b42; -const b44 = 4 ^ 5.5; -const b45 = 5.5 ^ b44; - - -let e1 = Const.PI + Const.E; // OK -let e2 = State.FAULT + State.OK; // OK -let e3 = "State: " + State.FAULT; // OK -let e4 = "Const = " + Const.PI; // OK - -let f1 = Const.PI + State.FAULT; // Error -let f2 = State.OK + Const.E; // Error diff --git a/ets2panda/linter/test/main/case_expr.sts b/ets2panda/linter/test/main/case_expr.ets similarity index 100% rename from ets2panda/linter/test/main/case_expr.sts rename to ets2panda/linter/test/main/case_expr.ets diff --git a/ets2panda/linter/test/main/catch_clause.ets b/ets2panda/linter/test/main/catch_clause.ets new file mode 100644 index 0000000000000000000000000000000000000000..87ed4bf37e3587bc090c1010356ba94791d89d1f --- /dev/null +++ b/ets2panda/linter/test/main/catch_clause.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022-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. + */ + +try { + if (true) throw "Catch with 'any' type"; +} catch (e: any) { + console.log(e); +} + +try { + if (true) throw "Catch with 'unknown' type"; +} catch (e: unknown) { + console.log(e); +} + +try { + if (true) throw 'Catch without explicit type'; +} catch (e) { + console.log(e); +} diff --git a/ets2panda/linter/test/main/catch_clause.sts b/ets2panda/linter/test/main/catch_clause.sts deleted file mode 100644 index d28f4889490f02414e64eb1542f5ad83f9cf655e..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/catch_clause.sts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -try { - if (true) throw "Catch with 'any' type"; -} catch (e: any) { - console.log(e); -} - -try { - if (true) throw "Catch with 'unknown' type"; -} catch (e: unknown) { - console.log(e); -} - -try { - if (true) throw 'Catch without explicit type'; -} catch (e) { - console.log(e); -} diff --git a/ets2panda/linter/test/main/classA.ets b/ets2panda/linter/test/main/classA.ets new file mode 100644 index 0000000000000000000000000000000000000000..045044ed1b532bd55fce0ea30dda17b97b41adc2 --- /dev/null +++ b/ets2panda/linter/test/main/classA.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023-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. + */ + + /* + * test class A for issue 13644 + */ + +import { B } from './classB' +export class A { + private uri: string; + + private routers() { + B.getInstance().navigation(this); + } + + constructor(uri: string) { + this.uri = uri; + } +} diff --git a/ets2panda/linter/test/main/classA.sts b/ets2panda/linter/test/main/classA.sts deleted file mode 100644 index 248dfba55c1f29d738223ef3f33668eadc7ca7d9..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/classA.sts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - /* - * test class A for issue 13644 - */ - -import { B } from './classB' -export class A { - private uri: string; - - private routers() { - B.getInstance().navigation(this); - } - - constructor(uri: string) { - this.uri = uri; - } -} diff --git a/ets2panda/linter/test/main/classB.ets b/ets2panda/linter/test/main/classB.ets new file mode 100644 index 0000000000000000000000000000000000000000..83fcadacd3f8bedf7ab344abaab8d0f90a01dc65 --- /dev/null +++ b/ets2panda/linter/test/main/classB.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023-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. + */ + + /* + * test class B for issue 13644 + */ + +import { A } from './classA' + +export class B { + public static sInstance: B; + public static getInstance(): B { + return this.sInstance; + } + + navigation(a: A) { + console.log("hi"); + } +} diff --git a/ets2panda/linter/test/main/classB.sts b/ets2panda/linter/test/main/classB.sts deleted file mode 100644 index 47932aef7440dbc7710fc535f452ba75e99b6995..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/classB.sts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - /* - * test class B for issue 13644 - */ - -import { A } from './classA' - -export class B { - public static sInstance: B; - public static getInstance(): B { - return this.sInstance; - } - - navigation(a: A) { - console.log("hi"); - } -} diff --git a/ets2panda/linter/test/main/classC.ets b/ets2panda/linter/test/main/classC.ets new file mode 100644 index 0000000000000000000000000000000000000000..d78d56de0e88331755f63937c4797a065418b5ec --- /dev/null +++ b/ets2panda/linter/test/main/classC.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + + /* + * test class C for issue 17357 + */ + +export declare class C { + bar(id: number, name?: string): Promise; + bar(id: number): void; +} diff --git a/ets2panda/linter/test/main/classC.sts b/ets2panda/linter/test/main/classC.sts deleted file mode 100644 index 1c35145249b40721d636ede3482f2876c615ff7c..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/classC.sts +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - /* - * test class C for issue 17357 - */ - -export declare class C { - bar(id: number, name?: string): Promise; - bar(id: number): void; -} diff --git a/ets2panda/linter/test/main/class_as_object.ets b/ets2panda/linter/test/main/class_as_object.ets new file mode 100644 index 0000000000000000000000000000000000000000..348f5f5bdc954582d1a61c24126f49d3b2bcd90a --- /dev/null +++ b/ets2panda/linter/test/main/class_as_object.ets @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2022-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. + */ + +import { Something, SomethingFactory, SomethingBar, Bar, Select } from "./oh_modules/ohos_factory"; + +class C { + static a = 5 + static b = 8 +} + +namespace H { + export class G {} +} + +let c = C +c = C +let g = H.G +g = H.G + +let ca = C.a +let cb = C.b + +let cc = new C() +let gg = new H.G() + +function foo1(arg: typeof C) {} +foo1(C) + +function foo2(arg: C) { + return C +} + +function foo3(arg: typeof H.G) {} +foo3(H.G) + +function foo4(arg: H.G) { + return H.G +} + +class A {} +interface B {} +interface I {} +class CC extends A implements I, B {} + +class D { + constructor(arg: typeof C) {} +} +new D(C) + +type X = D; + +namespace test1 { + class SomethingFoo extends Something { } + namespace NS { export class SomethingFoo extends Something { } } + + let fact = SomethingFactory.getInstance(); + + let x1 = fact.create1(SomethingFoo).beep(); + let x2 = fact.create1(SomethingBar).beep(); + let x3 = fact.create1(NS.SomethingFoo).beep(); + + let x4 = fact.create2({ o: SomethingFoo }); + let x5 = fact.create2({ o: SomethingBar }); + let x6 = fact.create2({ o: NS.SomethingFoo }); + + let x7 = fact.create3(() => SomethingFoo); + let x8 = fact.create4(() => SomethingFoo); + + let x9 = new Bar(SomethingFoo); +} + +enum Color { WHITE, BLUE, RED }; +let color: Color = Color.RED; + +for (let item = 0; item < Object.keys(Color).length; item++) { + console.log(item); +} + +foo2(() => C); + +export { C as H }; + +// #14228 +let data = new Select().from(C).eq('key').query(C); // Ok +invalid_func(C); // Ok +let a: any; +a.foo(C); // Ok + +let col = 'WHITE'; +console.log(Color[col]) + +// #14184 +namespace NS { + export enum E { + A = 'A', + B = 'B', + C = 'C' + } +} + +let s: string = 'B'; +let s2: string = NS.E[s]; + +for (let item = 0; item < Object.keys(NS.E).length; item++) { + console.log(item); +} + +/** + * {@link C} - should not report error + */ +class JSDocClass {} + +function classAsObject(x: any): void {} + +classAsObject(Object); +classAsObject(Function); +classAsObject(String); +classAsObject(Boolean); +classAsObject(Number); +classAsObject(Math); +classAsObject(Date); +classAsObject(RegExp); +classAsObject(Error); +classAsObject(EvalError); +classAsObject(RangeError); +classAsObject(ReferenceError); +classAsObject(SyntaxError); +classAsObject(TypeError); +classAsObject(URIError); +classAsObject(AggregateError); +classAsObject(JSON); +classAsObject(Array); +classAsObject(ArrayBuffer); +classAsObject(DataView); +classAsObject(Int8Array); +classAsObject(Uint8Array); +classAsObject(Uint8ClampedArray); +classAsObject(Int16Array); +classAsObject(Uint16Array); +classAsObject(Int32Array); +classAsObject(Uint32Array); +classAsObject(Float32Array); +classAsObject(Float64Array); +classAsObject(Map); +classAsObject(WeakMap); +classAsObject(Set); +classAsObject(WeakSet); +classAsObject(Promise); +classAsObject(Proxy); +classAsObject(Symbol); +classAsObject(SharedArrayBuffer); +classAsObject(Atomics); +classAsObject(BigInt); +classAsObject(BigInt64Array); +classAsObject(BigUint64Array); +classAsObject(WeakRef); +classAsObject(FinalizationRegistry); \ No newline at end of file diff --git a/ets2panda/linter/test/main/class_as_object.sts b/ets2panda/linter/test/main/class_as_object.sts deleted file mode 100644 index c065d42cc4d2e0366de5a26c10e8b76ddc8e42db..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/class_as_object.sts +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Something, SomethingFactory, SomethingBar, Bar, Select } from "./oh_modules/ohos_factory"; - -class C { - static a = 5 - static b = 8 -} - -namespace H { - export class G {} -} - -let c = C -c = C -let g = H.G -g = H.G - -let ca = C.a -let cb = C.b - -let cc = new C() -let gg = new H.G() - -function foo1(arg: typeof C) {} -foo1(C) - -function foo2(arg: C) { - return C -} - -function foo3(arg: typeof H.G) {} -foo3(H.G) - -function foo4(arg: H.G) { - return H.G -} - -class A {} -interface B {} -interface I {} -class CC extends A implements I, B {} - -class D { - constructor(arg: typeof C) {} -} -new D(C) - -type X = D; - -namespace test1 { - class SomethingFoo extends Something { } - namespace NS { export class SomethingFoo extends Something { } } - - let fact = SomethingFactory.getInstance(); - - let x1 = fact.create1(SomethingFoo).beep(); - let x2 = fact.create1(SomethingBar).beep(); - let x3 = fact.create1(NS.SomethingFoo).beep(); - - let x4 = fact.create2({ o: SomethingFoo }); - let x5 = fact.create2({ o: SomethingBar }); - let x6 = fact.create2({ o: NS.SomethingFoo }); - - let x7 = fact.create3(() => SomethingFoo); - let x8 = fact.create4(() => SomethingFoo); - - let x9 = new Bar(SomethingFoo); -} - -enum Color { WHITE, BLUE, RED }; -let color: Color = Color.RED; - -for (let item = 0; item < Object.keys(Color).length; item++) { - console.log(item); -} - -foo2(() => C); - -export { C as H }; - -// #14228 -let data = new Select().from(C).eq('key').query(C); // Ok -invalid_func(C); // Ok -let a: any; -a.foo(C); // Ok - -let col = 'WHITE'; -console.log(Color[col]) - -// #14184 -namespace NS { - export enum E { - A = 'A', - B = 'B', - C = 'C' - } -} - -let s: string = 'B'; -let s2: string = NS.E[s]; - -for (let item = 0; item < Object.keys(NS.E).length; item++) { - console.log(item); -} - -/** - * {@link C} - should not report error - */ -class JSDocClass {} - -function classAsObject(x: any): void {} - -classAsObject(Object); -classAsObject(Function); -classAsObject(String); -classAsObject(Boolean); -classAsObject(Number); -classAsObject(Math); -classAsObject(Date); -classAsObject(RegExp); -classAsObject(Error); -classAsObject(EvalError); -classAsObject(RangeError); -classAsObject(ReferenceError); -classAsObject(SyntaxError); -classAsObject(TypeError); -classAsObject(URIError); -classAsObject(AggregateError); -classAsObject(JSON); -classAsObject(Array); -classAsObject(ArrayBuffer); -classAsObject(DataView); -classAsObject(Int8Array); -classAsObject(Uint8Array); -classAsObject(Uint8ClampedArray); -classAsObject(Int16Array); -classAsObject(Uint16Array); -classAsObject(Int32Array); -classAsObject(Uint32Array); -classAsObject(Float32Array); -classAsObject(Float64Array); -classAsObject(Map); -classAsObject(WeakMap); -classAsObject(Set); -classAsObject(WeakSet); -classAsObject(Promise); -classAsObject(Proxy); -classAsObject(Symbol); -classAsObject(SharedArrayBuffer); -classAsObject(Atomics); -classAsObject(BigInt); -classAsObject(BigInt64Array); -classAsObject(BigUint64Array); -classAsObject(WeakRef); -classAsObject(FinalizationRegistry); \ No newline at end of file diff --git a/ets2panda/linter/test/main/class_static_block.ets b/ets2panda/linter/test/main/class_static_block.ets new file mode 100644 index 0000000000000000000000000000000000000000..109733572fe602b0fba53d747b7d6d25fcff8d05 --- /dev/null +++ b/ets2panda/linter/test/main/class_static_block.ets @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022-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. + */ + +class C { + static s: string; + static { + C.s = 'string'; + } + + static n: number; + static { + C.n = C.s.length; + } +} + +class B { + static s: string; + static { + C.s = 'string'; + } + + static n: number; +} + +{ + class C { + static t: string + static { + C.t = "aa" + C.t = C.t + "bb" + } + } +} + +{ + class C { + static s: string + static { + C.s = "aa" + C.s = C.s + "bb" + } + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/class_static_block.sts b/ets2panda/linter/test/main/class_static_block.sts deleted file mode 100644 index bcb800b3705552481ed0ac734e2e5c1a167d03ad..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/class_static_block.sts +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class C { - static s: string; - static { - C.s = 'string'; - } - - static n: number; - static { - C.n = C.s.length; - } -} - -class B { - static s: string; - static { - C.s = 'string'; - } - - static n: number; -} - -{ - class C { - static t: string - static { - C.t = "aa" - C.t = C.t + "bb" - } - } -} - -{ - class C { - static s: string - static { - C.s = "aa" - C.s = C.s + "bb" - } - } -} \ No newline at end of file diff --git a/ets2panda/linter/test/main/class_static_init.sts b/ets2panda/linter/test/main/class_static_init.ets similarity index 100% rename from ets2panda/linter/test/main/class_static_init.sts rename to ets2panda/linter/test/main/class_static_init.ets diff --git a/ets2panda/linter/test/main/concurrent_decorator_arkts2.sts b/ets2panda/linter/test/main/concurrent_decorator_arkts2.ets similarity index 100% rename from ets2panda/linter/test/main/concurrent_decorator_arkts2.sts rename to ets2panda/linter/test/main/concurrent_decorator_arkts2.ets diff --git a/ets2panda/linter/test/main/conditional_types.ets b/ets2panda/linter/test/main/conditional_types.ets new file mode 100644 index 0000000000000000000000000000000000000000..020d6293591e17829ecae5ceaa6d58943c238e8a --- /dev/null +++ b/ets2panda/linter/test/main/conditional_types.ets @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023-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 Animal { + live(): void; +} +interface Dog extends Animal { + woof(): void; +} +type Example1 = Dog extends Animal ? number : string; // type Example1 = number +type Example2 = RegExp extends Animal ? number : string; // type Example2 = string + +interface IdLabel { + id: number /* some fields */; +} +interface NameLabel { + name: string /* other fields */; +} +type NameOrId = T extends number + ? IdLabel + : NameLabel; +function createLabel(idOrName: T): NameOrId { + throw 1; +} +const a = createLabel('typescript'); // let a: NameLabel +const b = createLabel(2.8); // let b: IdLabel +const c = createLabel(Math.random() ? 'hello' : 42); // let c: NameLabel | IdLabel + +type MessageOf = T extends { message: unknown } ? T['message'] : never; +interface Email { + message: string; +} +interface Dog { + bark(): void; +} +type EmailMessageContents = MessageOf; // type EmailMessageContents = string +type DogMessageContents = MessageOf; // type DogMessageContents = never + +type Flatten = T extends any[] ? T[number] : T; +type Str = Flatten; // type Str = string +type Num = Flatten; // type Num = number diff --git a/ets2panda/linter/test/main/conditional_types.sts b/ets2panda/linter/test/main/conditional_types.sts deleted file mode 100644 index 7d0badff32e81a5247c87e3486e4bc757235357b..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/conditional_types.sts +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -interface Animal { - live(): void; -} -interface Dog extends Animal { - woof(): void; -} -type Example1 = Dog extends Animal ? number : string; // type Example1 = number -type Example2 = RegExp extends Animal ? number : string; // type Example2 = string - -interface IdLabel { - id: number /* some fields */; -} -interface NameLabel { - name: string /* other fields */; -} -type NameOrId = T extends number - ? IdLabel - : NameLabel; -function createLabel(idOrName: T): NameOrId { - throw 1; -} -const a = createLabel('typescript'); // let a: NameLabel -const b = createLabel(2.8); // let b: IdLabel -const c = createLabel(Math.random() ? 'hello' : 42); // let c: NameLabel | IdLabel - -type MessageOf = T extends { message: unknown } ? T['message'] : never; -interface Email { - message: string; -} -interface Dog { - bark(): void; -} -type EmailMessageContents = MessageOf; // type EmailMessageContents = string -type DogMessageContents = MessageOf; // type DogMessageContents = never - -type Flatten = T extends any[] ? T[number] : T; -type Str = Flatten; // type Str = string -type Num = Flatten; // type Num = number diff --git a/ets2panda/linter/test/main/const_assertion.ets b/ets2panda/linter/test/main/const_assertion.ets new file mode 100644 index 0000000000000000000000000000000000000000..25942a311ec3f2fb5108cb2b7e6255f97ad64582 --- /dev/null +++ b/ets2panda/linter/test/main/const_assertion.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-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. + */ + +const x = 'hello' as const; +const y = [10, 20] as const; + +const x1 = 'hello'; +const y1 = [10, 20]; diff --git a/ets2panda/linter/test/main/const_assertion.sts b/ets2panda/linter/test/main/const_assertion.sts deleted file mode 100644 index 0503732b3849b10c42090ff391d71d38e2812dc5..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/const_assertion.sts +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const x = 'hello' as const; -const y = [10, 20] as const; - -const x1 = 'hello'; -const y1 = [10, 20]; diff --git a/ets2panda/linter/test/main/constructor_types.ets b/ets2panda/linter/test/main/constructor_types.ets new file mode 100644 index 0000000000000000000000000000000000000000..137224171703f2d0df718b9f28c80d7be76ef92d --- /dev/null +++ b/ets2panda/linter/test/main/constructor_types.ets @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2023-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 ClockInterface { + tick(): void; +} + +class DigitalClock implements ClockInterface { + private h: number; + private m: number; + constructor(h: number, m: number) { + this.h = h; + this.m = m; + } + tick() { + console.log("beep beep"); + } +} + +class AnalogClock implements ClockInterface { + private h: number; + private m: number; + constructor(h: number, m: number) { + this.h = h; + this.m = m; + } + tick() { + console.log("tick tock"); + } +} + +function constructorSignature(): void { + type ClockConstructor = new (hour: number, minute: number) => ClockInterface; + + function createClock( + ctor: ClockConstructor, + hour: number, + minute: number + ): ClockInterface { + return new ctor(hour, minute); + } + + const digital = createClock(DigitalClock, 12, 17); + const analog = createClock(AnalogClock, 7, 32); +} + +function constructorType(): void { + function createClock( + ctor: new (hour: number, minute: number) => ClockInterface, + h: number, + m: number + ): ClockInterface { + return new ctor(h, m); + } + + const digital = createClock(DigitalClock, 16, 30); + const analog = createClock(AnalogClock, 23, 45); +} + +interface I { + new (s: string): I; +} + +function fn1(i: I) { + return new i("hello"); +} + +class SomeObject {} + +type SomeConstructor = { + new (s: string): SomeObject; +}; + +function fn2(ctor: SomeConstructor) { + return new ctor("hello"); +} + +class Person { + constructor(name: string, age: number) {} +} + +type PersonCtor = new (name: string, age: number) => Person; + +function createPerson1(Ctor: PersonCtor, name: string, age: number): Person { + return new Ctor(name, age); +} +function createPerson2( + Ctor: new (name: string, age: number) => Person, + name: string, + age: number +): Person { + return new Ctor(name, age); +} + +const person1 = createPerson1(Person, "John", 30); +const person2 = createPerson2(Person, "John", 30); diff --git a/ets2panda/linter/test/main/constructor_types.sts b/ets2panda/linter/test/main/constructor_types.sts deleted file mode 100644 index 395127310f8e35c5f4815b2184bd8be2dfda5b43..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/constructor_types.sts +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -interface ClockInterface { - tick(): void; -} - -class DigitalClock implements ClockInterface { - private h: number; - private m: number; - constructor(h: number, m: number) { - this.h = h; - this.m = m; - } - tick() { - console.log("beep beep"); - } -} - -class AnalogClock implements ClockInterface { - private h: number; - private m: number; - constructor(h: number, m: number) { - this.h = h; - this.m = m; - } - tick() { - console.log("tick tock"); - } -} - -function constructorSignature(): void { - type ClockConstructor = new (hour: number, minute: number) => ClockInterface; - - function createClock( - ctor: ClockConstructor, - hour: number, - minute: number - ): ClockInterface { - return new ctor(hour, minute); - } - - const digital = createClock(DigitalClock, 12, 17); - const analog = createClock(AnalogClock, 7, 32); -} - -function constructorType(): void { - function createClock( - ctor: new (hour: number, minute: number) => ClockInterface, - h: number, - m: number - ): ClockInterface { - return new ctor(h, m); - } - - const digital = createClock(DigitalClock, 16, 30); - const analog = createClock(AnalogClock, 23, 45); -} - -interface I { - new (s: string): I; -} - -function fn1(i: I) { - return new i("hello"); -} - -class SomeObject {} - -type SomeConstructor = { - new (s: string): SomeObject; -}; - -function fn2(ctor: SomeConstructor) { - return new ctor("hello"); -} - -class Person { - constructor(name: string, age: number) {} -} - -type PersonCtor = new (name: string, age: number) => Person; - -function createPerson1(Ctor: PersonCtor, name: string, age: number): Person { - return new Ctor(name, age); -} -function createPerson2( - Ctor: new (name: string, age: number) => Person, - name: string, - age: number -): Person { - return new Ctor(name, age); -} - -const person1 = createPerson1(Person, "John", 30); -const person2 = createPerson2(Person, "John", 30); diff --git a/ets2panda/linter/test/main/d_ts.ets b/ets2panda/linter/test/main/d_ts.ets new file mode 100644 index 0000000000000000000000000000000000000000..915c09a4698c7b73990b2727967e18e8030019ce --- /dev/null +++ b/ets2panda/linter/test/main/d_ts.ets @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023-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. + */ + +// We don't report errors that originated from *.d.ts files (i.e. from any external library) + +import { + InsightIntent, + margin, + WindowsStage, + Indexed, + TypeLiteral +} from "./d_ts.lib"; + +// issue 13041: Object literal +let intent: InsightIntent = { // OK + intentEntityInfo: { + entityId: '1', + entityName: '2', + prop1: '3', + prop2: '4' + } +} + +// issue 13043: Object literal +margin({ top: 24, bottom: 16 }); // OK + +// issue 13048: Lambda parameter's omitted types +let windowStage: WindowsStage = new WindowsStage(); +windowStage.loadContent('page/1', (err, data) => {}); // OK, lambda instantiates functional interface from the library, so parameter types may be omitted. +windowStage.loadContent2('page/1', (err, data) => {}); // OK, same as above + +// Property indexed access +let idx1: Indexed = { + a: 1, + b: 2 +} +idx1[1]; // OK +idx1['2']; // OK + +interface Indexed2 { + [x: string]: number; +} +let idx2: Indexed2 = { // CTE in ArkTS + a: 1, + b: 2 +} +idx2[1]; // CTE in ArkTS +idx2['2']; // CTE in ArkTS + +// initializing Type literal +let typeLiteral: TypeLiteral = { // OK + a: 1, + b: '2' +}; + +// Standard Library cases +let aescbc: AesCbcParams = { // OK, object literal conforms the interface + name: 'A', + iv: { + buffer: new ArrayBuffer(0), + byteLength: 1, + byteOffset: 2 + } +}; + +let iterRes: IteratorResult = { // OK, object literal conforms the interface + value: 5 +}; + +let obj: Object = { // CTE in ArkTS, Object can not be initialized with object literal + a: 0 +}; \ No newline at end of file diff --git a/ets2panda/linter/test/main/d_ts.sts b/ets2panda/linter/test/main/d_ts.sts deleted file mode 100644 index 43b550dc8c430dbbeee1b03cc967c845cb7ce791..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/d_ts.sts +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// We don't report errors that originated from *.d.ts files (i.e. from any external library) - -import { - InsightIntent, - margin, - WindowsStage, - Indexed, - TypeLiteral -} from "./d_ts.lib"; - -// issue 13041: Object literal -let intent: InsightIntent = { // OK - intentEntityInfo: { - entityId: '1', - entityName: '2', - prop1: '3', - prop2: '4' - } -} - -// issue 13043: Object literal -margin({ top: 24, bottom: 16 }); // OK - -// issue 13048: Lambda parameter's omitted types -let windowStage: WindowsStage = new WindowsStage(); -windowStage.loadContent('page/1', (err, data) => {}); // OK, lambda instantiates functional interface from the library, so parameter types may be omitted. -windowStage.loadContent2('page/1', (err, data) => {}); // OK, same as above - -// Property indexed access -let idx1: Indexed = { - a: 1, - b: 2 -} -idx1[1]; // OK -idx1['2']; // OK - -interface Indexed2 { - [x: string]: number; -} -let idx2: Indexed2 = { // CTE in ArkTS - a: 1, - b: 2 -} -idx2[1]; // CTE in ArkTS -idx2['2']; // CTE in ArkTS - -// initializing Type literal -let typeLiteral: TypeLiteral = { // OK - a: 1, - b: '2' -}; - -// Standard Library cases -let aescbc: AesCbcParams = { // OK, object literal conforms the interface - name: 'A', - iv: { - buffer: new ArrayBuffer(0), - byteLength: 1, - byteOffset: 2 - } -}; - -let iterRes: IteratorResult = { // OK, object literal conforms the interface - value: 5 -}; - -let obj: Object = { // CTE in ArkTS, Object can not be initialized with object literal - a: 0 -}; \ No newline at end of file diff --git a/ets2panda/linter/test/main/debugger_statememt.sts b/ets2panda/linter/test/main/debugger_statememt.ets similarity index 100% rename from ets2panda/linter/test/main/debugger_statememt.sts rename to ets2panda/linter/test/main/debugger_statememt.ets diff --git a/ets2panda/linter/test/main/definite_assignment.ets b/ets2panda/linter/test/main/definite_assignment.ets new file mode 100644 index 0000000000000000000000000000000000000000..4e920afbadf2cdf7360f7bd17cbcb0b103215341 --- /dev/null +++ b/ets2panda/linter/test/main/definite_assignment.ets @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023-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. + */ + +let x!: number; +initializeX(); +console.log(x + x); + +function initializeX() { + x = 10; +} + +let y: number; +initializeY(); + +console.log(y! + y!); // it's not a definite assignment assertion, it's the 'Ensure Not-Null expression', see 7.16 ArkTS spec +function initializeY() { + y = 20; +} + +class ArticleMeta { + private wordsPerMinute!: number; + private secondsPerImage: number; + + constructor(wordsPerMinute: number) { + this.secondsPerImage = 15; + } +} + +class Person { + name: string = "" + age: number = 0 + location: string = "" +} + +type OptionalPerson = Person | undefined +let persons : Record = { + "Bob": { + name: "Bob", + age: 48, + location: "New York" + } +} +console.log(persons["Bob"]!.age) diff --git a/ets2panda/linter/test/main/definite_assignment.sts b/ets2panda/linter/test/main/definite_assignment.sts deleted file mode 100644 index 288a335953d57f7ad7c4928878c0c9f439ac170b..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/definite_assignment.sts +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -let x!: number; -initializeX(); -console.log(x + x); - -function initializeX() { - x = 10; -} - -let y: number; -initializeY(); - -console.log(y! + y!); // it's not a definite assignment assertion, it's the 'Ensure Not-Null expression', see 7.16 ArkTS spec -function initializeY() { - y = 20; -} - -class ArticleMeta { - private wordsPerMinute!: number; - private secondsPerImage: number; - - constructor(wordsPerMinute: number) { - this.secondsPerImage = 15; - } -} - -class Person { - name: string = "" - age: number = 0 - location: string = "" -} - -type OptionalPerson = Person | undefined -let persons : Record = { - "Bob": { - name: "Bob", - age: 48, - location: "New York" - } -} -console.log(persons["Bob"]!.age) diff --git a/ets2panda/linter/test/main/delete_operator.ets b/ets2panda/linter/test/main/delete_operator.ets new file mode 100644 index 0000000000000000000000000000000000000000..8c81df3053a2e18779a1bddd9565450f996e11ce --- /dev/null +++ b/ets2panda/linter/test/main/delete_operator.ets @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022-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. + */ + +type T0 = { + field: any; +}; + +const f0 = (o: T0) => delete o.field; + +type T1 = { + field: unknown; +}; + +const f1 = (o: T1) => delete o.field; + +type T2 = { + field: never; +}; + +const f2 = (o: T2) => delete o.field; + +type T3 = { + field?: number; +}; + +const f3 = (o: T3) => delete o.field; + +type T4 = { + field: number; +}; + +const f4 = (o: T4) => delete o.field; diff --git a/ets2panda/linter/test/main/delete_operator.sts b/ets2panda/linter/test/main/delete_operator.sts deleted file mode 100644 index d5e0a93a8f39db2be395cd8bd43ea064b4f67ffb..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/delete_operator.sts +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -type T0 = { - field: any; -}; - -const f0 = (o: T0) => delete o.field; - -type T1 = { - field: unknown; -}; - -const f1 = (o: T1) => delete o.field; - -type T2 = { - field: never; -}; - -const f2 = (o: T2) => delete o.field; - -type T3 = { - field?: number; -}; - -const f3 = (o: T3) => delete o.field; - -type T4 = { - field: number; -}; - -const f4 = (o: T4) => delete o.field; diff --git a/ets2panda/linter/test/main/destructuring_assignments.ets b/ets2panda/linter/test/main/destructuring_assignments.ets new file mode 100644 index 0000000000000000000000000000000000000000..042cb714c414d49899add72f5c5bf7c11bf5c8b5 --- /dev/null +++ b/ets2panda/linter/test/main/destructuring_assignments.ets @@ -0,0 +1,88 @@ +/* + * 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. + */ + +// Object destructuring +let a = 5, b = 10, c = 15, d = 20, s = 'foo'; +let rest, rest2; +({ a, b, s } = { a: 100, b: 200, s: 'bar' }); // NOT OK +({ a, b, s } = {}); // NOT OK, not fixable +({ a, ...rest } = { a: 1, b: 2 }); // NOT OK +({ a, b: { c, d: s } } = { a: 1, b: { c: 3, d: 'baz' }}); // NOT OK +({ a, b: { ...rest } } = { a: 1, b: { c: 3, d: 'bah' }}); // NOT OK + +function getObject() { + return { a: 1, b: 2 }; +} +({ a, b } = getObject()); // NOT OK + +// Array destructuring +[a, b, c] = [10, 20, 30]; +[a, b, c] = []; +[[a1, a2], [b1, b2], [c1, c2]] = [[1, 2], [3, 4], [5, 6]]; +[a, b] = [b, a]; +[a, b, ...rest] = [10, 20, 30, 40, 50]; // NOT OK +[a, b, [c, d]] = [10, 20, [300, 400], 50]; +[a, b, [c, ...rest]] = [10, 20, [30, 40, 50]]; // NOT OK + +let tuple: [number, string, number] = [1, '2', 3]; +[a, , b] = tuple; +[a, ...rest] = tuple; // NOT OK + +const getArray = (): number[] => [1, 2, 3]; +[a, b] = getArray(); + +const set: Set = new Set([1, 2, 3, 4]); +[a, b, c] = set; // NOT OK + +const map: Map = new Map(); +[[a, b], [c, d]] = map; // NOT OK + +// Mixed destructuring +let e: number, f: number, x: { e: number }, g; +[a, b, [x, { f }]] = [1, 2, [{ e: 20 }, { f: 5 }]]; // NOT OK +[a, b, {e, e: f, ...g}] = [1, 2, {e: 10}]; // NOT OK +[a, b, ...{length}] = [1, 2, {e: 10}]; // NOT OK + +({ a, b : [c, d] } = { a: 1, b: [2, 3] }); // NOT OK +({ + a, + b: { + s, + c: [d, f], + }, +} = { a: 10, b: { s: 'foo', c: [30, 40] } }); // NOT OK + +// test for default value +({ a, b = 7, s } = { a: 100, b: 200, s: 'bar' }); // NOT OK +({ a, b: { c = 6, d: s } } = { a: 1, b: { c: 3, d: 'baz' }}); // NOT OK +({ a, b : [c, d = 8] } = { a: 1, b: [2, 3] }); // NOT OK +({ + a, + b: { + s, + c: [d = 9, f], + }, +} = { a: 10, b: { s: 'foo', c: [30, 40] } }); // NOT OK + + +// test for new Expression +class C { + public a: number = 10; + public b: number = 10; + public s: string = "0000"; +} + +({ a, b, s } = new C()); // NOT OK + diff --git a/ets2panda/linter/test/main/destructuring_assignments.sts b/ets2panda/linter/test/main/destructuring_assignments.sts deleted file mode 100644 index 5032eb24d6d0b0477558b6bdbe7db25e87d7308f..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/destructuring_assignments.sts +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Object destructuring -let a = 5, b = 10, c = 15, d = 20, s = 'foo'; -let rest, rest2; -({ a, b, s } = { a: 100, b: 200, s: 'bar' }); // NOT OK -({ a, b, s } = {}); // NOT OK, not fixable -({ a, ...rest } = { a: 1, b: 2 }); // NOT OK -({ a, b: { c, d: s } } = { a: 1, b: { c: 3, d: 'baz' }}); // NOT OK -({ a, b: { ...rest } } = { a: 1, b: { c: 3, d: 'bah' }}); // NOT OK - -function getObject() { - return { a: 1, b: 2 }; -} -({ a, b } = getObject()); // NOT OK - -// Array destructuring -[a, b, c] = [10, 20, 30]; -[a, b, c] = []; -[[a1, a2], [b1, b2], [c1, c2]] = [[1, 2], [3, 4], [5, 6]]; -[a, b] = [b, a]; -[a, b, ...rest] = [10, 20, 30, 40, 50]; // NOT OK -[a, b, [c, d]] = [10, 20, [300, 400], 50]; -[a, b, [c, ...rest]] = [10, 20, [30, 40, 50]]; // NOT OK - -let tuple: [number, string, number] = [1, '2', 3]; -[a, , b] = tuple; -[a, ...rest] = tuple; // NOT OK - -const getArray = (): number[] => [1, 2, 3]; -[a, b] = getArray(); - -const set: Set = new Set([1, 2, 3, 4]); -[a, b, c] = set; // NOT OK - -const map: Map = new Map(); -[[a, b], [c, d]] = map; // NOT OK - -// Mixed destructuring -let e: number, f: number, x: { e: number }, g; -[a, b, [x, { f }]] = [1, 2, [{ e: 20 }, { f: 5 }]]; // NOT OK -[a, b, {e, e: f, ...g}] = [1, 2, {e: 10}]; // NOT OK -[a, b, ...{length}] = [1, 2, {e: 10}]; // NOT OK - -({ a, b : [c, d] } = { a: 1, b: [2, 3] }); // NOT OK -({ - a, - b: { - s, - c: [d, f], - }, -} = { a: 10, b: { s: 'foo', c: [30, 40] } }); // NOT OK - -// test for default value -({ a, b = 7, s } = { a: 100, b: 200, s: 'bar' }); // NOT OK -({ a, b: { c = 6, d: s } } = { a: 1, b: { c: 3, d: 'baz' }}); // NOT OK -({ a, b : [c, d = 8] } = { a: 1, b: [2, 3] }); // NOT OK -({ - a, - b: { - s, - c: [d = 9, f], - }, -} = { a: 10, b: { s: 'foo', c: [30, 40] } }); // NOT OK - - -// test for new Expression -class C { - public a: number = 10; - public b: number = 10; - public s: string = "0000"; -} - -({ a, b, s } = new C()); // NOT OK - diff --git a/ets2panda/linter/test/main/destructuring_declarations.ets b/ets2panda/linter/test/main/destructuring_declarations.ets new file mode 100644 index 0000000000000000000000000000000000000000..92519800ebf5a8b142fe2791c512253e291224ce --- /dev/null +++ b/ets2panda/linter/test/main/destructuring_declarations.ets @@ -0,0 +1,85 @@ +/* + * 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. + */ + +// Object destructuring +let { a, b, c } = { a: 100, b: 200, c: 'bar' }; // NOT OK +let { a2, ...rest2 } = { a2: 1, b2: 2 }; // NOT OK +let { a3, b3: { c3, d3: e3 } } = { a3: 1, b3: { c3: 3, d3: 'baz' }}; // NOT OK +let { a4, b4: { ...rest4 } } = { a4: 1, b4: { c4: 3, d4: 'bah' }}; // NOT OK + +let { a, b, c } = {}; // NOT OK, not fixable + +function getObject() { + return { a5: 1, b5: 2 }; +} +let { a5, b5 } = getObject(); // NOT OK + +// Array destructuring +const [a6, b6, c6] = [10, 20, 30]; +const [a7, b7, ...rest7] = [10, 20, 30, 40, 50]; // NOT OK +const [a8, b8, [c8, e8]] = [10, 20, [300, 400], 50]; +const [a9, b9, [c9, ...rest9]] = [10, 20, [30, 40, 50]]; // NOT OK + +const [[a1, a2], [b1, b2], [c1, c2]] = [[1, 2], [3, 4], [5, 6]]; +const [[a1, a2, a3], [b1, b2, b3], [c1, c2, c3]] = [[1, 2, 3], [3, 4, 5], [5, 6, 7]]; + +let tuple: [number, string, number] = [1, '2', 3]; +let [a10, , b10] = tuple; +let [a11, ...rest11] = tuple; // NOT OK + +const getArray = (): number[] => [1, 2, 3]; +let [a12, b12] = getArray(); + +const set: Set = new Set([1, 2, 3, 4]); +let [a13, b13, c13] = set; // NOT OK + +const map: Map = new Map(); +let [[a14, b14], [c14, d14]] = map; // NOT OK + +// Mixed destructuring +let [a15, b15, [x15, { f15 }]] = [1, 2, [{ e15: 20 }, { f15: 5 }]]; // NOT OK +let [a16, b16, {e16, e16: f16, ...g16}] = [1, 2, {e16: 10}]; // NOT OK +{ let [a17, b17, ...{length}] = [1, 2, 3, 4]; } // NOT OK + +let { a18, b18: [c18, d18] } = { a18: 1, b18: [2, 3] }; // NOT OK +let { + a19, + b19: { + c19, + d19: [e19, f19], + }, +} = { a19: 10, b19: { c19: 'foo', d19: [30, 40] } }; // NOT OK + +// test for default value +let { a, b, c = 9 } = { a: 100, b: 200, c: 'bar' }; // NOT OK +let { a3, b3: { c3 = 10, d3: e3 } } = { a3: 1, b3: { c3: 3, d3: 'baz' }}; // NOT OK +let { a5, b5 = 7 } = getObject(); // NOT OK +let { a18, b18: [c18 = 66, d18] } = { a18: 1, b18: [2, 3] }; // NOT OK +let { + a19, + b19: { + c19, + d19: [e19, f19 = 9], + }, +} = { a19: 10, b19: { c19: 'foo', d19: [30, 40] } }; // NOT OK + +// test for new Expression +class C { + public a: number = 10; + public b: number = 10; + public s: string = "0000"; +} + +let { a, b, s } = new C(); // NOT OK \ No newline at end of file diff --git a/ets2panda/linter/test/main/destructuring_declarations.sts b/ets2panda/linter/test/main/destructuring_declarations.sts deleted file mode 100644 index 2064ff6bcd61a00d42097c528d9e54a97211f771..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/destructuring_declarations.sts +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Object destructuring -let { a, b, c } = { a: 100, b: 200, c: 'bar' }; // NOT OK -let { a2, ...rest2 } = { a2: 1, b2: 2 }; // NOT OK -let { a3, b3: { c3, d3: e3 } } = { a3: 1, b3: { c3: 3, d3: 'baz' }}; // NOT OK -let { a4, b4: { ...rest4 } } = { a4: 1, b4: { c4: 3, d4: 'bah' }}; // NOT OK - -let { a, b, c } = {}; // NOT OK, not fixable - -function getObject() { - return { a5: 1, b5: 2 }; -} -let { a5, b5 } = getObject(); // NOT OK - -// Array destructuring -const [a6, b6, c6] = [10, 20, 30]; -const [a7, b7, ...rest7] = [10, 20, 30, 40, 50]; // NOT OK -const [a8, b8, [c8, e8]] = [10, 20, [300, 400], 50]; -const [a9, b9, [c9, ...rest9]] = [10, 20, [30, 40, 50]]; // NOT OK - -const [[a1, a2], [b1, b2], [c1, c2]] = [[1, 2], [3, 4], [5, 6]]; -const [[a1, a2, a3], [b1, b2, b3], [c1, c2, c3]] = [[1, 2, 3], [3, 4, 5], [5, 6, 7]]; - -let tuple: [number, string, number] = [1, '2', 3]; -let [a10, , b10] = tuple; -let [a11, ...rest11] = tuple; // NOT OK - -const getArray = (): number[] => [1, 2, 3]; -let [a12, b12] = getArray(); - -const set: Set = new Set([1, 2, 3, 4]); -let [a13, b13, c13] = set; // NOT OK - -const map: Map = new Map(); -let [[a14, b14], [c14, d14]] = map; // NOT OK - -// Mixed destructuring -let [a15, b15, [x15, { f15 }]] = [1, 2, [{ e15: 20 }, { f15: 5 }]]; // NOT OK -let [a16, b16, {e16, e16: f16, ...g16}] = [1, 2, {e16: 10}]; // NOT OK -{ let [a17, b17, ...{length}] = [1, 2, 3, 4]; } // NOT OK - -let { a18, b18: [c18, d18] } = { a18: 1, b18: [2, 3] }; // NOT OK -let { - a19, - b19: { - c19, - d19: [e19, f19], - }, -} = { a19: 10, b19: { c19: 'foo', d19: [30, 40] } }; // NOT OK - -// test for default value -let { a, b, c = 9 } = { a: 100, b: 200, c: 'bar' }; // NOT OK -let { a3, b3: { c3 = 10, d3: e3 } } = { a3: 1, b3: { c3: 3, d3: 'baz' }}; // NOT OK -let { a5, b5 = 7 } = getObject(); // NOT OK -let { a18, b18: [c18 = 66, d18] } = { a18: 1, b18: [2, 3] }; // NOT OK -let { - a19, - b19: { - c19, - d19: [e19, f19 = 9], - }, -} = { a19: 10, b19: { c19: 'foo', d19: [30, 40] } }; // NOT OK - -// test for new Expression -class C { - public a: number = 10; - public b: number = 10; - public s: string = "0000"; -} - -let { a, b, s } = new C(); // NOT OK \ No newline at end of file diff --git a/ets2panda/linter/test/main/destructuring_for_loop.ets b/ets2panda/linter/test/main/destructuring_for_loop.ets new file mode 100644 index 0000000000000000000000000000000000000000..e6880ae7a85d1a87103bbe9b7687cdd08694f18f --- /dev/null +++ b/ets2panda/linter/test/main/destructuring_for_loop.ets @@ -0,0 +1,60 @@ +/* + * 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. + */ + +const objects: { a: number; b: string }[] = [ + { a: 10, b: 'q' }, + { a: 20, b: 'w' }, + { a: 30, b: 'e' }, +]; +for (const { a, b } of objects) { + console.log(a, b); +} + +const arrays = [ + [1, 2], + [3, 4], + [5, 6], +]; +for (const [q, w] of arrays) { + console.log(q, w); +} + +let a: number, b: string; +for ({ a, b } of objects) { + console.log(a, b); +} + +let x: number, y: number; +for ([x, y] of arrays) { + console.log(x, y); +} + +const people = [ + { + name: 'Mike Smith', + family: { mother: 'Jane Smith', father: 'Harry Smith' }, + }, + { + name: 'Tom Jones', + family: { mother: 'Norah Jones', father: 'Richard Jones' }, + }, +]; +let n: string, f: string; +for ({ + name: n, + family: { father: f }, +} of people) { + console.log(`Name: ${n}, Father: ${f}`); +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/destructuring_for_loop.sts b/ets2panda/linter/test/main/destructuring_for_loop.sts deleted file mode 100644 index 2179d1b23d833c310da51494f753b315c2dbfc6e..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/destructuring_for_loop.sts +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const objects: { a: number; b: string }[] = [ - { a: 10, b: 'q' }, - { a: 20, b: 'w' }, - { a: 30, b: 'e' }, -]; -for (const { a, b } of objects) { - console.log(a, b); -} - -const arrays = [ - [1, 2], - [3, 4], - [5, 6], -]; -for (const [q, w] of arrays) { - console.log(q, w); -} - -let a: number, b: string; -for ({ a, b } of objects) { - console.log(a, b); -} - -let x: number, y: number; -for ([x, y] of arrays) { - console.log(x, y); -} - -const people = [ - { - name: 'Mike Smith', - family: { mother: 'Jane Smith', father: 'Harry Smith' }, - }, - { - name: 'Tom Jones', - family: { mother: 'Norah Jones', father: 'Richard Jones' }, - }, -]; -let n: string, f: string; -for ({ - name: n, - family: { father: f }, -} of people) { - console.log(`Name: ${n}, Father: ${f}`); -} \ No newline at end of file diff --git a/ets2panda/linter/test/main/destructuring_parameters.ets b/ets2panda/linter/test/main/destructuring_parameters.ets new file mode 100644 index 0000000000000000000000000000000000000000..0c4fdc6492320a265053803f3ae5f48ea60b3f7d --- /dev/null +++ b/ets2panda/linter/test/main/destructuring_parameters.ets @@ -0,0 +1,89 @@ +/* + * 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. + */ + +function drawText({ text = '', position: [x, y] = [0, 0] }): void { // NOT OK + // Draw text +} +drawText({ text: 'Figure 1', position: [10, 20] }); + +const hello = ({ // NOT OK + firstName, + lastName, +}: { + firstName: string; + lastName: string; +}): string => `Hello ${firstName} ${lastName}`; +console.log(hello({ firstName: 'Karl', lastName: 'Marks' })); + +const person = { firstName: 'Adam', lastName: 'Smith' }; +console.log(hello(person)); + +interface I { + d: number +} + +function f1({d = 1}: I) { // NOT OK +} +f1({d:2}) + +function f2({d = 1}: {d: number}) { // NOT OK +} +f2({d:2}) + +function f3({x, ...y}) { // NOT OK + console.log(x); + Object.keys(y).forEach(k => console.log(k, y[k])) +} +f3({x: 1, b: 2, c: 3}) + +function f4([a, b]): void { + console.log(a, b); +} +f4(['Hello', 'Wolrd']); + +function f5([a, b]: number[]): void { + console.log(a, b); +} +f5([1, 2, 3]); + +function f6([a, [b, c]]): void { + console.log(a, b, c); +} +f6([1, [2, 3]]); + +function f7([a, b, ...c]) { // NOT OK + console.log(a, b, c); +} +f7([1, 2, 3, 4]) + +function f8([a, b, [c, ...d]]) { // NOT OK + console.log(a, b, c, d); +} +f8([1, 2, [3, 4, 5]]) + +function f9([a, {b, c}]): void { // NOT OK + console.log(a, b, c); +} +f9([1, {b: 2, c: 3}]); + +function f10([a, [b, {c: d, e: f}]]): void { // NOT OK + console.log(a, b, d, f); +} +f10([1, [2, {c : 3, e: 4}]]); + +function f11([a, b]: Set): void { // NOT OK + console.log(a, b); +} +f11(new Set([1, 2, 3, 4])); diff --git a/ets2panda/linter/test/main/destructuring_parameters.sts b/ets2panda/linter/test/main/destructuring_parameters.sts deleted file mode 100644 index 2b1862795be66dc531caa7a15632eacda7cc06b2..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/destructuring_parameters.sts +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function drawText({ text = '', position: [x, y] = [0, 0] }): void { // NOT OK - // Draw text -} -drawText({ text: 'Figure 1', position: [10, 20] }); - -const hello = ({ // NOT OK - firstName, - lastName, -}: { - firstName: string; - lastName: string; -}): string => `Hello ${firstName} ${lastName}`; -console.log(hello({ firstName: 'Karl', lastName: 'Marks' })); - -const person = { firstName: 'Adam', lastName: 'Smith' }; -console.log(hello(person)); - -interface I { - d: number -} - -function f1({d = 1}: I) { // NOT OK -} -f1({d:2}) - -function f2({d = 1}: {d: number}) { // NOT OK -} -f2({d:2}) - -function f3({x, ...y}) { // NOT OK - console.log(x); - Object.keys(y).forEach(k => console.log(k, y[k])) -} -f3({x: 1, b: 2, c: 3}) - -function f4([a, b]): void { - console.log(a, b); -} -f4(['Hello', 'Wolrd']); - -function f5([a, b]: number[]): void { - console.log(a, b); -} -f5([1, 2, 3]); - -function f6([a, [b, c]]): void { - console.log(a, b, c); -} -f6([1, [2, 3]]); - -function f7([a, b, ...c]) { // NOT OK - console.log(a, b, c); -} -f7([1, 2, 3, 4]) - -function f8([a, b, [c, ...d]]) { // NOT OK - console.log(a, b, c, d); -} -f8([1, 2, [3, 4, 5]]) - -function f9([a, {b, c}]): void { // NOT OK - console.log(a, b, c); -} -f9([1, {b: 2, c: 3}]); - -function f10([a, [b, {c: d, e: f}]]): void { // NOT OK - console.log(a, b, d, f); -} -f10([1, [2, {c : 3, e: 4}]]); - -function f11([a, b]: Set): void { // NOT OK - console.log(a, b); -} -f11(new Set([1, 2, 3, 4])); diff --git a/ets2panda/linter/test/main/dollar_binding_1.sts b/ets2panda/linter/test/main/dollar_binding_1.ets similarity index 100% rename from ets2panda/linter/test/main/dollar_binding_1.sts rename to ets2panda/linter/test/main/dollar_binding_1.ets diff --git a/ets2panda/linter/test/main/dollar_binding_2.sts b/ets2panda/linter/test/main/dollar_binding_2.ets similarity index 100% rename from ets2panda/linter/test/main/dollar_binding_2.sts rename to ets2panda/linter/test/main/dollar_binding_2.ets diff --git a/ets2panda/linter/test/main/double_dollar_binding_1.sts b/ets2panda/linter/test/main/double_dollar_binding_1.ets similarity index 100% rename from ets2panda/linter/test/main/double_dollar_binding_1.sts rename to ets2panda/linter/test/main/double_dollar_binding_1.ets diff --git a/ets2panda/linter/test/main/double_dollar_binding_2.sts b/ets2panda/linter/test/main/double_dollar_binding_2.ets similarity index 100% rename from ets2panda/linter/test/main/double_dollar_binding_2.sts rename to ets2panda/linter/test/main/double_dollar_binding_2.ets diff --git a/ets2panda/linter/test/main/double_excla_binding_1.sts b/ets2panda/linter/test/main/double_excla_binding_1.ets similarity index 100% rename from ets2panda/linter/test/main/double_excla_binding_1.sts rename to ets2panda/linter/test/main/double_excla_binding_1.ets diff --git a/ets2panda/linter/test/main/double_excla_binding_2.sts b/ets2panda/linter/test/main/double_excla_binding_2.ets similarity index 100% rename from ets2panda/linter/test/main/double_excla_binding_2.sts rename to ets2panda/linter/test/main/double_excla_binding_2.ets diff --git a/ets2panda/linter/test/main/dynamic_ctor_call.ets b/ets2panda/linter/test/main/dynamic_ctor_call.ets new file mode 100644 index 0000000000000000000000000000000000000000..6921955e1fc0dc1d025e6010adb73cec3d5e1549 --- /dev/null +++ b/ets2panda/linter/test/main/dynamic_ctor_call.ets @@ -0,0 +1,50 @@ +/* + * 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. + */ + +function FunctionCtor() {} +let c = new FunctionCtor(); + +interface I {} + +type IConstructor = new (x: number, y: number) => I; + +function createI( + ctor: IConstructor +): I { + return new ctor(1, 2); +} + +function createI2( + ctor: new (x: number, y: number) => I +): I { + return new ctor(1, 2); +} + +interface IConstructor3 { + new (s: string): I; +} +function createI3(ctor: IConstructor3) { + return new ctor("hello"); +} + +class C {} + +type Constructor = { + new (s: string): C; +}; + +function createC(ctor: Constructor) { + return new ctor("hello"); +} diff --git a/ets2panda/linter/test/main/dynamic_ctor_call.sts b/ets2panda/linter/test/main/dynamic_ctor_call.sts deleted file mode 100644 index d44537fca3cf55ea04bf54206cff6bfcfb3a4199..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/dynamic_ctor_call.sts +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function FunctionCtor() {} -let c = new FunctionCtor(); - -interface I {} - -type IConstructor = new (x: number, y: number) => I; - -function createI( - ctor: IConstructor -): I { - return new ctor(1, 2); -} - -function createI2( - ctor: new (x: number, y: number) => I -): I { - return new ctor(1, 2); -} - -interface IConstructor3 { - new (s: string): I; -} -function createI3(ctor: IConstructor3) { - return new ctor("hello"); -} - -class C {} - -type Constructor = { - new (s: string): C; -}; - -function createC(ctor: Constructor) { - return new ctor("hello"); -} diff --git a/ets2panda/linter/test/main/dynamic_import.sts b/ets2panda/linter/test/main/dynamic_import.ets similarity index 100% rename from ets2panda/linter/test/main/dynamic_import.sts rename to ets2panda/linter/test/main/dynamic_import.ets diff --git a/ets2panda/linter/test/main/dynamic_object_literals.ets b/ets2panda/linter/test/main/dynamic_object_literals.ets new file mode 100644 index 0000000000000000000000000000000000000000..a9e925c4707922813b25ed0519484a3a0454fc54 --- /dev/null +++ b/ets2panda/linter/test/main/dynamic_object_literals.ets @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2022-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. + */ + +import { + I, + foo, + I2, + I3, + bar, + C, + getDynamicObject, + dynamic_array, + padding, + margin, + position, + IndexedSignatureType, + postCardAction +} from "./dynamic_lib" + +function main(): void { + let obj: I = { + f1: {a: 10, b: 20}, + f2: [{c: 30}, {d: 40}], + "prop.xxx": 0 + } + + obj = { + f1: {e: "abc"}, + f2: [{g: 50}] + } + + obj.f1 = {f: 100} + obj.f1 = [{a1: 1}, {a2: 2, a3: 3}] + + foo({f2: 'abc', f3: 30}) + foo([{b1: 1, b2: 2}, {b3: '3'}]) + + let obj2: I2 = { + f1: {a: 10, b: 20}, + f2: [{c: 30}, {d: 40}], + f3: {a: '11', b: 444, c: {f: {d: [1, 2]}}} + } + + bar({f2: 'abc', f3: 30}) + bar([{b1: 1, b2: 2}, {b3: '3'}], {c: 4, d: 5}) + + let obj3: I3 = { + f1: {a: 10, b: 20}, + f2: {a: '11', b: 444, c: {f: {d: [1, 2]}}}, + f3: [{c: 30}, {d: 40}] + } +} + +// #13412 - assign dynamic object (interop) to some static type from standard library +interface InitProps { + p?: number; + q?: Record | undefined; + r?: Object | undefined | Record; +} +function createInitProps(c: C) { + let a: InitProps = { + p: c.a, + q: c.b + } + + let b: InitProps = { + r: getDynamicObject() + } +} + +// #13483 - pass object literal to method call of exported variable +dynamic_array.splice(2, 0, {a: 1, b: '2'}); + +// #13550 - allow literals as property names in dynamic context +padding({'top': '0px', 'right': '5px', 'bottom': '10px', 'left': '15px'}); +margin({'top': '10px', 'right': '20px', 'bottom': '30px', 'left': '40px'}); +position({'x': '20', 'y': '40'}); + +// allow literal as property name for type aliases that come from interop +function typeAliasLitAsPropName(): IndexedSignatureType { + return { + 'a': '1', + 'b': '2', + 'c': '3' + } +} + +// #14399 +postCardAction({}, { + "action": 'router', + "abilityName": 'SomeAbility', + "params": { + "message": 'add detail' + } +}); \ No newline at end of file diff --git a/ets2panda/linter/test/main/dynamic_object_literals.sts b/ets2panda/linter/test/main/dynamic_object_literals.sts deleted file mode 100644 index 3c6220218d48ce6e2f29304dfa058bd581b4cdd5..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/dynamic_object_literals.sts +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { - I, - foo, - I2, - I3, - bar, - C, - getDynamicObject, - dynamic_array, - padding, - margin, - position, - IndexedSignatureType, - postCardAction -} from "./dynamic_lib" - -function main(): void { - let obj: I = { - f1: {a: 10, b: 20}, - f2: [{c: 30}, {d: 40}], - "prop.xxx": 0 - } - - obj = { - f1: {e: "abc"}, - f2: [{g: 50}] - } - - obj.f1 = {f: 100} - obj.f1 = [{a1: 1}, {a2: 2, a3: 3}] - - foo({f2: 'abc', f3: 30}) - foo([{b1: 1, b2: 2}, {b3: '3'}]) - - let obj2: I2 = { - f1: {a: 10, b: 20}, - f2: [{c: 30}, {d: 40}], - f3: {a: '11', b: 444, c: {f: {d: [1, 2]}}} - } - - bar({f2: 'abc', f3: 30}) - bar([{b1: 1, b2: 2}, {b3: '3'}], {c: 4, d: 5}) - - let obj3: I3 = { - f1: {a: 10, b: 20}, - f2: {a: '11', b: 444, c: {f: {d: [1, 2]}}}, - f3: [{c: 30}, {d: 40}] - } -} - -// #13412 - assign dynamic object (interop) to some static type from standard library -interface InitProps { - p?: number; - q?: Record | undefined; - r?: Object | undefined | Record; -} -function createInitProps(c: C) { - let a: InitProps = { - p: c.a, - q: c.b - } - - let b: InitProps = { - r: getDynamicObject() - } -} - -// #13483 - pass object literal to method call of exported variable -dynamic_array.splice(2, 0, {a: 1, b: '2'}); - -// #13550 - allow literals as property names in dynamic context -padding({'top': '0px', 'right': '5px', 'bottom': '10px', 'left': '15px'}); -margin({'top': '10px', 'right': '20px', 'bottom': '30px', 'left': '40px'}); -position({'x': '20', 'y': '40'}); - -// allow literal as property name for type aliases that come from interop -function typeAliasLitAsPropName(): IndexedSignatureType { - return { - 'a': '1', - 'b': '2', - 'c': '3' - } -} - -// #14399 -postCardAction({}, { - "action": 'router', - "abilityName": 'SomeAbility', - "params": { - "message": 'add detail' - } -}); \ No newline at end of file diff --git a/ets2panda/linter/test/main/enum_indexing.ets b/ets2panda/linter/test/main/enum_indexing.ets new file mode 100644 index 0000000000000000000000000000000000000000..b0179e8014c4a8dc51b1800eb74f7a883e9d9ac4 --- /dev/null +++ b/ets2panda/linter/test/main/enum_indexing.ets @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022-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. + */ + +enum E { a = 1, b = 2 }; + +let ill = E; + +// legal +E[E.a]; +(e: E.a) => E[e]; +(e: E) => E[e]; + +// check required in future +E[1]; +(e: E.a | E.b) => E[e]; +(e: number) => E[e]; diff --git a/ets2panda/linter/test/main/enum_indexing.sts b/ets2panda/linter/test/main/enum_indexing.sts deleted file mode 100644 index bd08c0acd72cda9caa06de8dc44eac7d542100c3..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/enum_indexing.sts +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -enum E { a = 1, b = 2 }; - -let ill = E; - -// legal -E[E.a]; -(e: E.a) => E[e]; -(e: E) => E[e]; - -// check required in future -E[1]; -(e: E.a | E.b) => E[e]; -(e: number) => E[e]; diff --git a/ets2panda/linter/test/main/enum_member_non_constant.ets b/ets2panda/linter/test/main/enum_member_non_constant.ets new file mode 100644 index 0000000000000000000000000000000000000000..5b30e24be3d920469fddf7ddcdc0e469ae60d2fe --- /dev/null +++ b/ets2panda/linter/test/main/enum_member_non_constant.ets @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022-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. + */ + +enum Colors { + Red, + Orange, + Yellow +} + +enum Numbers { + A = 10, + B = 2.57, + C = 0x2B7F, +} + +enum Direction { + Up = 'UP', + Down = 'DOWN', + Left = 'LEFT', + Right = 'RIGHT', + Across = 'UP'+'LEFT', +} + +enum FileAccess { + None, + Read = 1 << 1, + Write = 1 << 2, + ReadWrite = Read | Write, +} + +const constVar = 25; +let letVar = 50; + +enum ExprInits { + A = constVar, + B = letVar, + C = A, + D = Direction.Right, + E = Numbers.C, + H = '123'.length, + I = 1 && 2, +} diff --git a/ets2panda/linter/test/main/enum_member_non_constant.sts b/ets2panda/linter/test/main/enum_member_non_constant.sts deleted file mode 100644 index 4ae90fde17ad97fea7356b7896d1f9ebda35fcb2..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/enum_member_non_constant.sts +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -enum Colors { - Red, - Orange, - Yellow -} - -enum Numbers { - A = 10, - B = 2.57, - C = 0x2B7F, -} - -enum Direction { - Up = 'UP', - Down = 'DOWN', - Left = 'LEFT', - Right = 'RIGHT', - Across = 'UP'+'LEFT', -} - -enum FileAccess { - None, - Read = 1 << 1, - Write = 1 << 2, - ReadWrite = Read | Write, -} - -const constVar = 25; -let letVar = 50; - -enum ExprInits { - A = constVar, - B = letVar, - C = A, - D = Direction.Right, - E = Numbers.C, - H = '123'.length, - I = 1 && 2, -} diff --git a/ets2panda/linter/test/main/enum_not_support_float.sts b/ets2panda/linter/test/main/enum_not_support_float.ets similarity index 100% rename from ets2panda/linter/test/main/enum_not_support_float.sts rename to ets2panda/linter/test/main/enum_not_support_float.ets diff --git a/ets2panda/linter/test/main/es_object.ets b/ets2panda/linter/test/main/es_object.ets new file mode 100644 index 0000000000000000000000000000000000000000..573c6a523a8f4324ac87ca9460a87897b11f3c56 --- /dev/null +++ b/ets2panda/linter/test/main/es_object.ets @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2022-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. + */ +import { fooOh, barOh } from './oh_modules/ohos_lib' +type ESObject = any + +class A {} + +let g1: ESObject +let g2: ESObject[] +let g3: A + +class B { + f1: ESObject + f2: ESObject[] + f3: A + + constructor(p1: ESObject, p2: ESObject[], p3: A) { + this.f1 = p1 + this.f2 = p2 + this.f3 = p3 + } + + foo1(p1: ESObject, p2: ESObject[], p3: A): ESObject { + return p1 + } + + foo2(p1: ESObject, p2: ESObject[], p3: A): ESObject[] { + return p2 + } + + foo3(p1: ESObject, p2: ESObject[], p3: A): A { + return p3 + } +} + +function bar1(p1: ESObject, p2: ESObject[], p3: A): ESObject { + return p1 +} + +function bar2(p1: ESObject, p2: ESObject[], p3: A): ESObject[] { + return p2 +} + +function bar3(p1: ESObject, p2: ESObject[], p3: A): A { + return p3 +} + +function ff(): {x: number} { + return {x: 10} +} + +function baz(p1: ESObject, p2: ESObject[], p3: A): void { + const c1: ESObject = p1; + const c2: ESObject[] = p2 + const c3: A = p3 + + let v1: ESObject = p1 + let v2: ESObject[] = p2 + let v3: A = p3 + + v1 = c1 + v2 = c2 + v3 = c3 + + v1.x = 10 + v1.foo() + v1[10] = 20 + v1(20) + + v1 = {} + v1 = "abc" + v1 = ff() + v1 = [1, 2] + v1 = [p1, c1] + v1 = [p1, c1, "abc"] + v1 = new A() + + let v11: ESObject = {} + let v12: ESObject = "abc" + let v13: ESObject = ff() + let v14: ESObject = [1, 2] + let v15: ESObject = [p1, c1] + let v16: ESObject = [p1, c1, "abc"] + let v17: ESObject = new A() + + let n1: number = v1 + n1 = v1 + let n2: number = p1 as number +} + +export let obj = new ESObject(); + +type t1 = ESObject +type t2 = ESObject[] + +export type t3 = ESObject +export type t4 = ESObject[] + +export type t5 = t3 +export type t6 = t4[] + +export function foo1(): any { + let a: ESObject = "STRING"; + return a +} + +export function foo2(a: ESObject): ESObject { + return a; +} + +export function foo3(a: t3): t3 { + return a; +} + +foo2(5) +foo3(5) +foo2("asd") +foo3("asd") +foo2(null) +foo3(null) +foo2(undefined) +foo3(undefined) + +export function foo4(a: ESObject[]): ESObject { + return a; +} + +export function foo5(a: t3[]): t3 { + return a; +} + +foo4([2, 3]) +foo5([2, 3]) +foo4(["str1", "str2"]) +foo5(["str1", "str2"]) +let n: ESObject +n = null + +foo4(n) +foo5(n) + +export function foo6(a: ESObject[]): ESObject { + return a; +} + +export function foo7(a: t3[]): t3 { + return a; +} + +export function foo8(a: ESObject[]): ESObject { + return a; +} + +export function foo9(a: t3[]): t3 { + return a; +} + +export class Cls {} + +interface CL extends ESObject {} + +export interface CLS extends ESObject {} + +foo2({ k: 'k', h: {t: 1}}) // we can assign anything to the esobject, even untyped literal +let q1: ESObject = 1; // CTE - ``ESObject`` typed variable can only be local +let q2: ESObject = fooOh(); // CTE - ``ESObject`` typed variable can only be local +let q3: ESObject = q2; // CTE - ``ESObject`` typed variable can only be local +function f() { + let e1 = fooOh(); // CTE - type of e1 is `any` + let e2: ESObject = 1; // CTE - can't initialize ESObject with not dynamic values + let e3: ESObject = {}; // CTE - can't initialize ESObject with not dynamic values + let e4: ESObject = []; // CTE - can't initialize ESObject with not dynamic values + let e5: ESObject = ""; // CTE - can't initialize ESObject with not dynamic values + let e6: ESObject = fooOh(); // OK - explicitly annotaded as ESObject + let e7: ESObject = e6; // OK - initialize ESObject with ESObject + e6['prop'] // CTE - can't access dynamic properties of ESObject + e6[1] // CTE - can't access dynamic properties of ESObject + e6.prop // CTE - can't access dynamic properties of ESObject + barOh(e6) // OK - ESObject is passed to interop call + e6 = e7 // OK - ESObject is assigned to ESObject +} diff --git a/ets2panda/linter/test/main/es_object.sts b/ets2panda/linter/test/main/es_object.sts deleted file mode 100644 index 8de84814ced305708e3546056f99b7d22e3e0752..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/es_object.sts +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import { fooOh, barOh } from './oh_modules/ohos_lib' -type ESObject = any - -class A {} - -let g1: ESObject -let g2: ESObject[] -let g3: A - -class B { - f1: ESObject - f2: ESObject[] - f3: A - - constructor(p1: ESObject, p2: ESObject[], p3: A) { - this.f1 = p1 - this.f2 = p2 - this.f3 = p3 - } - - foo1(p1: ESObject, p2: ESObject[], p3: A): ESObject { - return p1 - } - - foo2(p1: ESObject, p2: ESObject[], p3: A): ESObject[] { - return p2 - } - - foo3(p1: ESObject, p2: ESObject[], p3: A): A { - return p3 - } -} - -function bar1(p1: ESObject, p2: ESObject[], p3: A): ESObject { - return p1 -} - -function bar2(p1: ESObject, p2: ESObject[], p3: A): ESObject[] { - return p2 -} - -function bar3(p1: ESObject, p2: ESObject[], p3: A): A { - return p3 -} - -function ff(): {x: number} { - return {x: 10} -} - -function baz(p1: ESObject, p2: ESObject[], p3: A): void { - const c1: ESObject = p1; - const c2: ESObject[] = p2 - const c3: A = p3 - - let v1: ESObject = p1 - let v2: ESObject[] = p2 - let v3: A = p3 - - v1 = c1 - v2 = c2 - v3 = c3 - - v1.x = 10 - v1.foo() - v1[10] = 20 - v1(20) - - v1 = {} - v1 = "abc" - v1 = ff() - v1 = [1, 2] - v1 = [p1, c1] - v1 = [p1, c1, "abc"] - v1 = new A() - - let v11: ESObject = {} - let v12: ESObject = "abc" - let v13: ESObject = ff() - let v14: ESObject = [1, 2] - let v15: ESObject = [p1, c1] - let v16: ESObject = [p1, c1, "abc"] - let v17: ESObject = new A() - - let n1: number = v1 - n1 = v1 - let n2: number = p1 as number -} - -export let obj = new ESObject(); - -type t1 = ESObject -type t2 = ESObject[] - -export type t3 = ESObject -export type t4 = ESObject[] - -export type t5 = t3 -export type t6 = t4[] - -export function foo1(): any { - let a: ESObject = "STRING"; - return a -} - -export function foo2(a: ESObject): ESObject { - return a; -} - -export function foo3(a: t3): t3 { - return a; -} - -foo2(5) -foo3(5) -foo2("asd") -foo3("asd") -foo2(null) -foo3(null) -foo2(undefined) -foo3(undefined) - -export function foo4(a: ESObject[]): ESObject { - return a; -} - -export function foo5(a: t3[]): t3 { - return a; -} - -foo4([2, 3]) -foo5([2, 3]) -foo4(["str1", "str2"]) -foo5(["str1", "str2"]) -let n: ESObject -n = null - -foo4(n) -foo5(n) - -export function foo6(a: ESObject[]): ESObject { - return a; -} - -export function foo7(a: t3[]): t3 { - return a; -} - -export function foo8(a: ESObject[]): ESObject { - return a; -} - -export function foo9(a: t3[]): t3 { - return a; -} - -export class Cls {} - -interface CL extends ESObject {} - -export interface CLS extends ESObject {} - -foo2({ k: 'k', h: {t: 1}}) // we can assign anything to the esobject, even untyped literal -let q1: ESObject = 1; // CTE - ``ESObject`` typed variable can only be local -let q2: ESObject = fooOh(); // CTE - ``ESObject`` typed variable can only be local -let q3: ESObject = q2; // CTE - ``ESObject`` typed variable can only be local -function f() { - let e1 = fooOh(); // CTE - type of e1 is `any` - let e2: ESObject = 1; // CTE - can't initialize ESObject with not dynamic values - let e3: ESObject = {}; // CTE - can't initialize ESObject with not dynamic values - let e4: ESObject = []; // CTE - can't initialize ESObject with not dynamic values - let e5: ESObject = ""; // CTE - can't initialize ESObject with not dynamic values - let e6: ESObject = fooOh(); // OK - explicitly annotaded as ESObject - let e7: ESObject = e6; // OK - initialize ESObject with ESObject - e6['prop'] // CTE - can't access dynamic properties of ESObject - e6[1] // CTE - can't access dynamic properties of ESObject - e6.prop // CTE - can't access dynamic properties of ESObject - barOh(e6) // OK - ESObject is passed to interop call - e6 = e7 // OK - ESObject is assigned to ESObject -} diff --git a/ets2panda/linter/test/main/explicit_function_type.sts b/ets2panda/linter/test/main/explicit_function_type.ets similarity index 100% rename from ets2panda/linter/test/main/explicit_function_type.sts rename to ets2panda/linter/test/main/explicit_function_type.ets diff --git a/ets2panda/linter/test/main/exponent.sts b/ets2panda/linter/test/main/exponent.ets similarity index 100% rename from ets2panda/linter/test/main/exponent.sts rename to ets2panda/linter/test/main/exponent.ets diff --git a/ets2panda/linter/test/main/extend_decorator_1.sts b/ets2panda/linter/test/main/extend_decorator_1.ets similarity index 100% rename from ets2panda/linter/test/main/extend_decorator_1.sts rename to ets2panda/linter/test/main/extend_decorator_1.ets diff --git a/ets2panda/linter/test/main/extend_decorator_2.sts b/ets2panda/linter/test/main/extend_decorator_2.ets similarity index 100% rename from ets2panda/linter/test/main/extend_decorator_2.sts rename to ets2panda/linter/test/main/extend_decorator_2.ets diff --git a/ets2panda/linter/test/main/extends_expression.sts b/ets2panda/linter/test/main/extends_expression.ets similarity index 100% rename from ets2panda/linter/test/main/extends_expression.sts rename to ets2panda/linter/test/main/extends_expression.ets diff --git a/ets2panda/linter/test/main/for_stmts.ets b/ets2panda/linter/test/main/for_stmts.ets new file mode 100644 index 0000000000000000000000000000000000000000..b48fa6873b9101baca90d53978e68a7a7bd41854 --- /dev/null +++ b/ets2panda/linter/test/main/for_stmts.ets @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2022-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. + */ + +enum Mammals { + Humans = 'Humans', + Bats = 'Bats', + Dolphins = 'Dolphins', +} + +for (const m in Mammals) { + if (true) console.log(m); +} + +/** + * OK, spec 7.5.2 Type inference from Types of Elements + * type is (number | string | boolean)[], it is inferred from types of the elements. + */ +const someArray = [1, 'string', false]; +for (const entry of someArray) { + console.log(entry); // 1, "string", false +} + +for (const entry of [1, 'string', false]) { + console.log(entry); // 1, "string", false +} + +/** + * NOT OK + * type is explicitly specified to be tuple + */ +const someTuple: [number, string, boolean] = [1, 'string', false]; +for (const entry of someTuple) { + console.log(entry); // 1, "string", false +} + +const list = [4, 5, 6]; +for (const i in list) { + if (true) console.log(i); // "0", "1", "2", +} + +for (const i of list) { + console.log(i); // 4, 5, 6 +} + +for (const i of [4, 5, 6]) { + console.log(i); // 4, 5, 6 +} + +const s = 'abc'; // type is 'abc' +for (const c of s) { + console.log(c); +} + +let s2 = 'abs' // type is string +for (const c of s2) { + console.log(c); +} + +for (const c of 'abc') { + console.log(c); +} + +for (const i in s) { + if (true) console.log(i); +} + +const arr = ['z', 'x', 'y']; + +for (const c of arr) { + console.log(c); +} + +let i: number, j: number, k: number; +// legal 'comma' operator +for (i = 1, j = 2, k = 3; i * j * k > 0; i++, k--, j += 2) { + continue; +} +// illegal 'comma' operator +for (i = 1, ((j = 2), (k = 3)); i * j * k > 0; i--, (k++, j--)) { + continue; +} + +const a1 = [0, 1]; +const a2 = [1, 2, 3, 4, 5]; +const a3: number[] = [1, 2, 3, 4, 5]; +const a4: Array = [1, 2, 3, 4, 5]; +const a5 = new Array(10); +const a6 = new Int8Array(10); +const a7 = new Uint8Array(10); +const a8 = new Uint8ClampedArray(10); +const a9 = new Int16Array(10); +const a10 = new Uint16Array(10); +const a11 = new Int32Array(10); +const a12 = new Uint32Array(10); +const a13 = new Float32Array(10); +const a14 = new Float64Array(10); +const a15 = new BigInt64Array(10); +const a16 = new BigUint64Array(10); + +for (const i of a1) { + console.log(i); +} +for (const i of a2) { + console.log(i); +} +for (const i of a3) { + console.log(i); +} +for (const i of a4) { + console.log(i); +} +for (const i of a5) { + console.log(i); +} +for (const i of a6) { + console.log(i); +} +for (const i of a7) { + console.log(i); +} +for (const i of a8) { + console.log(i); +} +for (const i of a9) { + console.log(i); +} +for (const i of a10) { + console.log(i); +} +for (const i of a11) { + console.log(i); +} +for (const i of a12) { + console.log(i); +} +for (const i of a13) { + console.log(i); +} +for (const i of a14) { + console.log(i); +} +for (const i of a15) { + console.log(i); +} +for (const i of a16) { + console.log(i); +} + +const a17: (number | string)[] = [] + +for (const i of a17) { + console.log(i) +} + +class P extends Array { + foo() { + for (let t of this) { + console.log(t) + } + } +} + +let a18 = new P(1, 2, 3) +a18.foo() +for (let t of a18) { + console.log(t) +} + +class PN extends Array { + foo() { + for (let t of this) { + console.log(t) + } + } +} + +let a19 = new PN(1, 2, 3) +a19.foo() +for (let t of a19) { + console.log(t) +} + +class MyInt8Array extends Int8Array {} +class MyUint8Array extends Uint8Array {} +class MyUint8ClampedArray extends Uint8ClampedArray {} +class MyInt16Array extends Int16Array {} +class MyUint16Array extends Uint16Array {} +class MyInt32Array extends Int32Array {} +class MyUint32Array extends Uint32Array {} +class MyFloat32Array extends Float32Array {} +class MyFloat64Array extends Float64Array {} +class MyBigInt64Array extends BigInt64Array {} +class MyBigUint64Array extends BigUint64Array {} + +const a20 = new MyInt8Array(10); +const a21 = new MyUint8Array(10); +const a22 = new MyUint8ClampedArray(10); +const a23 = new MyInt16Array(10); +const a24 = new MyUint16Array(10); +const a25 = new MyInt32Array(10); +const a26 = new MyUint32Array(10); +const a27 = new MyFloat32Array(10); +const a28 = new MyFloat64Array(10); +const a29 = new MyBigInt64Array(10); +const a30 = new MyBigUint64Array(10); + +for (const i of a20) { + console.log(i); +} +for (const i of a21) { + console.log(i); +} +for (const i of a22) { + console.log(i); +} +for (const i of a23) { + console.log(i); +} +for (const i of a24) { + console.log(i); +} +for (const i of a25) { + console.log(i); +} +for (const i of a26) { + console.log(i); +} +for (const i of a27) { + console.log(i); +} +for (const i of a28) { + console.log(i); +} +for (const i of a29) { + console.log(i); +} +for (const i of a30) { + console.log(i); +} + +class A extends Map {} +class B extends Set {} +class C extends String {} + +let map = new Map([["a", 1], ["b", 2], ["c", 3]]); +let derivedFromMap = new A([["d", 4], ["e", 5], ["f", 6]]); +let set = new Set([1, 2, 3]); +let derivedFromSet = new B([4, 5, 6]); +let str = "hello"; +let derivedFromString = new C("world") + +for (let i of map) { + console.log(i); +} + +for (let i of derivedFromMap) { + console.log(i); +} + +for (let i of set) { + console.log(i); +} + +for (let i of derivedFromSet) { + console.log(i); +} + +for (let i of str) { + console.log(i); +} + +for (let i of derivedFromString) { + console.log(i); +} diff --git a/ets2panda/linter/test/main/for_stmts.sts b/ets2panda/linter/test/main/for_stmts.sts deleted file mode 100644 index 9722daf3d4f450e57b0f21a48dffbc79b16b200f..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/for_stmts.sts +++ /dev/null @@ -1,286 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -enum Mammals { - Humans = 'Humans', - Bats = 'Bats', - Dolphins = 'Dolphins', -} - -for (const m in Mammals) { - if (true) console.log(m); -} - -/** - * OK, spec 7.5.2 Type inference from Types of Elements - * type is (number | string | boolean)[], it is inferred from types of the elements. - */ -const someArray = [1, 'string', false]; -for (const entry of someArray) { - console.log(entry); // 1, "string", false -} - -for (const entry of [1, 'string', false]) { - console.log(entry); // 1, "string", false -} - -/** - * NOT OK - * type is explicitly specified to be tuple - */ -const someTuple: [number, string, boolean] = [1, 'string', false]; -for (const entry of someTuple) { - console.log(entry); // 1, "string", false -} - -const list = [4, 5, 6]; -for (const i in list) { - if (true) console.log(i); // "0", "1", "2", -} - -for (const i of list) { - console.log(i); // 4, 5, 6 -} - -for (const i of [4, 5, 6]) { - console.log(i); // 4, 5, 6 -} - -const s = 'abc'; // type is 'abc' -for (const c of s) { - console.log(c); -} - -let s2 = 'abs' // type is string -for (const c of s2) { - console.log(c); -} - -for (const c of 'abc') { - console.log(c); -} - -for (const i in s) { - if (true) console.log(i); -} - -const arr = ['z', 'x', 'y']; - -for (const c of arr) { - console.log(c); -} - -let i: number, j: number, k: number; -// legal 'comma' operator -for (i = 1, j = 2, k = 3; i * j * k > 0; i++, k--, j += 2) { - continue; -} -// illegal 'comma' operator -for (i = 1, ((j = 2), (k = 3)); i * j * k > 0; i--, (k++, j--)) { - continue; -} - -const a1 = [0, 1]; -const a2 = [1, 2, 3, 4, 5]; -const a3: number[] = [1, 2, 3, 4, 5]; -const a4: Array = [1, 2, 3, 4, 5]; -const a5 = new Array(10); -const a6 = new Int8Array(10); -const a7 = new Uint8Array(10); -const a8 = new Uint8ClampedArray(10); -const a9 = new Int16Array(10); -const a10 = new Uint16Array(10); -const a11 = new Int32Array(10); -const a12 = new Uint32Array(10); -const a13 = new Float32Array(10); -const a14 = new Float64Array(10); -const a15 = new BigInt64Array(10); -const a16 = new BigUint64Array(10); - -for (const i of a1) { - console.log(i); -} -for (const i of a2) { - console.log(i); -} -for (const i of a3) { - console.log(i); -} -for (const i of a4) { - console.log(i); -} -for (const i of a5) { - console.log(i); -} -for (const i of a6) { - console.log(i); -} -for (const i of a7) { - console.log(i); -} -for (const i of a8) { - console.log(i); -} -for (const i of a9) { - console.log(i); -} -for (const i of a10) { - console.log(i); -} -for (const i of a11) { - console.log(i); -} -for (const i of a12) { - console.log(i); -} -for (const i of a13) { - console.log(i); -} -for (const i of a14) { - console.log(i); -} -for (const i of a15) { - console.log(i); -} -for (const i of a16) { - console.log(i); -} - -const a17: (number | string)[] = [] - -for (const i of a17) { - console.log(i) -} - -class P extends Array { - foo() { - for (let t of this) { - console.log(t) - } - } -} - -let a18 = new P(1, 2, 3) -a18.foo() -for (let t of a18) { - console.log(t) -} - -class PN extends Array { - foo() { - for (let t of this) { - console.log(t) - } - } -} - -let a19 = new PN(1, 2, 3) -a19.foo() -for (let t of a19) { - console.log(t) -} - -class MyInt8Array extends Int8Array {} -class MyUint8Array extends Uint8Array {} -class MyUint8ClampedArray extends Uint8ClampedArray {} -class MyInt16Array extends Int16Array {} -class MyUint16Array extends Uint16Array {} -class MyInt32Array extends Int32Array {} -class MyUint32Array extends Uint32Array {} -class MyFloat32Array extends Float32Array {} -class MyFloat64Array extends Float64Array {} -class MyBigInt64Array extends BigInt64Array {} -class MyBigUint64Array extends BigUint64Array {} - -const a20 = new MyInt8Array(10); -const a21 = new MyUint8Array(10); -const a22 = new MyUint8ClampedArray(10); -const a23 = new MyInt16Array(10); -const a24 = new MyUint16Array(10); -const a25 = new MyInt32Array(10); -const a26 = new MyUint32Array(10); -const a27 = new MyFloat32Array(10); -const a28 = new MyFloat64Array(10); -const a29 = new MyBigInt64Array(10); -const a30 = new MyBigUint64Array(10); - -for (const i of a20) { - console.log(i); -} -for (const i of a21) { - console.log(i); -} -for (const i of a22) { - console.log(i); -} -for (const i of a23) { - console.log(i); -} -for (const i of a24) { - console.log(i); -} -for (const i of a25) { - console.log(i); -} -for (const i of a26) { - console.log(i); -} -for (const i of a27) { - console.log(i); -} -for (const i of a28) { - console.log(i); -} -for (const i of a29) { - console.log(i); -} -for (const i of a30) { - console.log(i); -} - -class A extends Map {} -class B extends Set {} -class C extends String {} - -let map = new Map([["a", 1], ["b", 2], ["c", 3]]); -let derivedFromMap = new A([["d", 4], ["e", 5], ["f", 6]]); -let set = new Set([1, 2, 3]); -let derivedFromSet = new B([4, 5, 6]); -let str = "hello"; -let derivedFromString = new C("world") - -for (let i of map) { - console.log(i); -} - -for (let i of derivedFromMap) { - console.log(i); -} - -for (let i of set) { - console.log(i); -} - -for (let i of derivedFromSet) { - console.log(i); -} - -for (let i of str) { - console.log(i); -} - -for (let i of derivedFromString) { - console.log(i); -} diff --git a/ets2panda/linter/test/main/func_inferred_type_args.sts b/ets2panda/linter/test/main/func_inferred_type_args.ets similarity index 100% rename from ets2panda/linter/test/main/func_inferred_type_args.sts rename to ets2panda/linter/test/main/func_inferred_type_args.ets diff --git a/ets2panda/linter/test/main/func_return_type.ets b/ets2panda/linter/test/main/func_return_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..591bb0b87903489f7619c2ca02fe7b2bd6085d69 --- /dev/null +++ b/ets2panda/linter/test/main/func_return_type.ets @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2022-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. + */ + +function q(x: number) { // Need fix + return w(x) +} +function w(x: number) { + return x; +} + +function e(x: string) { // Need fix + return r(x) +} +function r(x: string) { + return t(x); +} +function t(x: string): string { + return x; +} + +function y() { // Need fix + return u(); +} +function u() { + return [1, 2, 3]; +} + +function f(x: number) { // Not fixable + if (x <= 0) { + return x; + } + return g(x); +} +function g(x: number) { // Not fixable + return f(x - 1); +} + +function h(x: number, y: number) { + return x + y; +} + +function x() { // Not fixable + return z(1); +} +function z(a: any) { // Not fixable + return a; +} + +function functionExpressions() { + let f1 = function (c: C) { // Need fix + return f2(c); + }; + let f2 = function (c: C) { + return new C(); + }; + + let f3 = function (x: number, y: number) { + return x + y; + }; + + let f4 = function () { // Need fix + return f5(); + }; + let f5 = function () { + return new Map(); + }; + + let f6 = function () { // Not fixable + return z(0); + }; +} + +function lambdas() { + let l1 = (t: T) => { // Need fix + return l2(t); + }; + let l2 = (t: T) => { + return t; + }; + + let l3 = (x: number, y: number) => { + return x + y; + }; + + let l4 = () => { // Need fix + return l5(); + }; + let l5 = () => { + return (x: number) => x.toString(); + }; + + let l6 = () => { // Not fixable + return z(-1); + }; +} + +class C { + m(x: number) { // Need fix + return q(x) + } + + m2(x: number) { // Need fix + return this.m(x) + } + + m3(x: number) { + return 10; + } + + m4() { // Need fix + return this.m2(20); + } + + m5() { // Not fixable + return z(30); + } +} + +function no_space_before_body(x: number){ // Need fix + return w(x) +} + +declare function fooImplicitDeclaration(); // CTE - no type in declaration +function fooImplicitImplementation() {}; // OK - deduces void + +interface ImplicitTest { + bar1(); // CTE - no type in declaration + bar2(): number; // OK - type is specified explicitly +} + +declare class ImplicitTestClass { + bar1(); // CTE - no type in declaration + bar2(): number; // OK - type is specified explicitly +} diff --git a/ets2panda/linter/test/main/func_return_type.sts b/ets2panda/linter/test/main/func_return_type.sts deleted file mode 100644 index 3574103e76a7d942a3599d50a272ad443a99a8e0..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/func_return_type.sts +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function q(x: number) { // Need fix - return w(x) -} -function w(x: number) { - return x; -} - -function e(x: string) { // Need fix - return r(x) -} -function r(x: string) { - return t(x); -} -function t(x: string): string { - return x; -} - -function y() { // Need fix - return u(); -} -function u() { - return [1, 2, 3]; -} - -function f(x: number) { // Not fixable - if (x <= 0) { - return x; - } - return g(x); -} -function g(x: number) { // Not fixable - return f(x - 1); -} - -function h(x: number, y: number) { - return x + y; -} - -function x() { // Not fixable - return z(1); -} -function z(a: any) { // Not fixable - return a; -} - -function functionExpressions() { - let f1 = function (c: C) { // Need fix - return f2(c); - }; - let f2 = function (c: C) { - return new C(); - }; - - let f3 = function (x: number, y: number) { - return x + y; - }; - - let f4 = function () { // Need fix - return f5(); - }; - let f5 = function () { - return new Map(); - }; - - let f6 = function () { // Not fixable - return z(0); - }; -} - -function lambdas() { - let l1 = (t: T) => { // Need fix - return l2(t); - }; - let l2 = (t: T) => { - return t; - }; - - let l3 = (x: number, y: number) => { - return x + y; - }; - - let l4 = () => { // Need fix - return l5(); - }; - let l5 = () => { - return (x: number) => x.toString(); - }; - - let l6 = () => { // Not fixable - return z(-1); - }; -} - -class C { - m(x: number) { // Need fix - return q(x) - } - - m2(x: number) { // Need fix - return this.m(x) - } - - m3(x: number) { - return 10; - } - - m4() { // Need fix - return this.m2(20); - } - - m5() { // Not fixable - return z(30); - } -} - -function no_space_before_body(x: number){ // Need fix - return w(x) -} - -declare function fooImplicitDeclaration(); // CTE - no type in declaration -function fooImplicitImplementation() {}; // OK - deduces void - -interface ImplicitTest { - bar1(); // CTE - no type in declaration - bar2(): number; // OK - type is specified explicitly -} - -declare class ImplicitTestClass { - bar1(); // CTE - no type in declaration - bar2(): number; // OK - type is specified explicitly -} diff --git a/ets2panda/linter/test/main/function_expression.ets b/ets2panda/linter/test/main/function_expression.ets new file mode 100644 index 0000000000000000000000000000000000000000..54495ce43093f6ca202556e42e004e54ae669cc7 --- /dev/null +++ b/ets2panda/linter/test/main/function_expression.ets @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2022-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. + */ + +const empty = function () {}; + +const multiply = function (x: number, y): number { + return x * y; +}; + +function createFunc(): () => number { + return function () { + return 100; + }; +} + +const foobar = (function () { + return 'get result immediately'; +})(); + +(function () { + console.log('foo!'); +})(); + +void (function () { + console.log('bar!'); +})(); + +const array = [1, 2, 3, 4, 5, 6]; +const double = array.map(function (e) { + return e * 2; +}); +const even = array.filter(function (x) { + return x % 2 === 0; +}); + +const retTypeInfer = function (p: any) { + return p; +}; + +const generator = function * () { + yield 1; +}; + +const generic = function (t: T, e: E) { + return t; +}; + +const asyncFun = async function() { + console.log('baz!'); +}; + +const factorial = function f(n: number): number { + return n == 1 ? 1 : n * f(n - 1); +}; + +class C { + m() {} +} +const noRecursiveCall = function f(p: () => number): void { // Doesn't have recursive call, should be autofixable + let a = factorial(3); + let b = p(); + let c = new C() + c.m(); +}; + +let iife = function() { + console.log('called immediately'); +}(); + +let indexAccess = function() { + console.log('index access'); +}[0]; + +void function() { + console.log('void'); +}; + +async function awaitFun() { + await function() { + console.log('async'); + }; +} + +let typeofFunc = typeof function() { + console.log('typeof'); +} + +class BindFuncExpr { + foo() { + let bar = function(p: boolean) { + console.log('Function.bind(this)'); + }.bind(this); + } +} + +let callback = function() { console.log('callback'); } +callback = callback || function() { console.log('expr || function(){}'); }; + +let ternaryExpr = !!callback + ? function() { console.log('ternary 1'); } || 2 + : 3 && function() { console.log('ternary 2'); }; \ No newline at end of file diff --git a/ets2panda/linter/test/main/function_expression.sts b/ets2panda/linter/test/main/function_expression.sts deleted file mode 100644 index f3e7eb8fb5381c806063cf867b2d928e71817b21..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/function_expression.sts +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const empty = function () {}; - -const multiply = function (x: number, y): number { - return x * y; -}; - -function createFunc(): () => number { - return function () { - return 100; - }; -} - -const foobar = (function () { - return 'get result immediately'; -})(); - -(function () { - console.log('foo!'); -})(); - -void (function () { - console.log('bar!'); -})(); - -const array = [1, 2, 3, 4, 5, 6]; -const double = array.map(function (e) { - return e * 2; -}); -const even = array.filter(function (x) { - return x % 2 === 0; -}); - -const retTypeInfer = function (p: any) { - return p; -}; - -const generator = function * () { - yield 1; -}; - -const generic = function (t: T, e: E) { - return t; -}; - -const asyncFun = async function() { - console.log('baz!'); -}; - -const factorial = function f(n: number): number { - return n == 1 ? 1 : n * f(n - 1); -}; - -class C { - m() {} -} -const noRecursiveCall = function f(p: () => number): void { // Doesn't have recursive call, should be autofixable - let a = factorial(3); - let b = p(); - let c = new C() - c.m(); -}; - -let iife = function() { - console.log('called immediately'); -}(); - -let indexAccess = function() { - console.log('index access'); -}[0]; - -void function() { - console.log('void'); -}; - -async function awaitFun() { - await function() { - console.log('async'); - }; -} - -let typeofFunc = typeof function() { - console.log('typeof'); -} - -class BindFuncExpr { - foo() { - let bar = function(p: boolean) { - console.log('Function.bind(this)'); - }.bind(this); - } -} - -let callback = function() { console.log('callback'); } -callback = callback || function() { console.log('expr || function(){}'); }; - -let ternaryExpr = !!callback - ? function() { console.log('ternary 1'); } || 2 - : 3 && function() { console.log('ternary 2'); }; \ No newline at end of file diff --git a/ets2panda/linter/test/main/function_object_methods.ets b/ets2panda/linter/test/main/function_object_methods.ets new file mode 100644 index 0000000000000000000000000000000000000000..f951f25db319bf691acc3f52966cec2108be5317 --- /dev/null +++ b/ets2panda/linter/test/main/function_object_methods.ets @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2022-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 Author { + firstName: string; + lastName: string; + anotherName: Function; + asyncName: Function; +} + +class C { + firstName: string; + lastName: string; + constructor(firstName: string, lastName: string) { + this.firstName = firstName; + this.lastName = lastName; + this.fullName = this.fullName.bind(this); + this.asyncFullName = this.asyncFullName.bind(this); + } + + author(): Author { + let author: Author = { + firstName: this.firstName, + lastName: this.lastName, + anotherName: this.fullName.bind(this), + asyncName: this.asyncFullName.bind(this) + } + return author + } + + fullName(): string { + return this.firstName + " " + this.lastName; + } + + async asyncFullName(): Promise { + return Promise(fullName()); + } + + sayHello(greet: string) { + console.log(greet, this.fullName()); + } + + async asyncSayHello(greet: string) { + const afn = await this.asyncFullName(); + console.log(greet, afn); + } +} + +async function asyncFoo(): Promise { + const person1 = new C("Arthur", "Clarke"); + const person2 = new C("Ray", "Bradbury"); + + await person1.asyncFullName(); + const afn = await person1.asyncFullName(); + + const afn2 = await person1.asyncFullName.apply(person2); + + const f = person2.asyncFullName.bind(person1); + await f(); + + await asyncBar(person1); + + const ash1: Function = person1.asyncSayHello.bind(person1, "Hello"); + await ash1() + + const ash2: Function = person1.asyncSayHello.bind(person1); + await ash2("Hello") + + await person1.asyncSayHello.apply(person2, "Hello"); + await person1.asyncSayHello.call(person2, "Hello") +} + +async function asyncBar(c: C): Promise { + const person = new C("Stanislaw", "Lem"); + return await c.asyncFullName.call(person); +} + +function foo(): void { + const person1 = new C("Arthur", "Clarke"); + const person2 = new C("Ray", "Bradbury"); + + const fullName = person1.fullName.apply(person2); + + const f = person2.fullName.bind(person1); + f(); + + bar(person1); + + const sh1: Function = person1.sayHello.bind(person1, "Hello"); + sh1() + + const sh2: Function = person1.sayHello.bind(person1); + sh2("Hello") + + person1.sayHello.apply(person2, "Hello"); + person1.sayHello.call(person2, "Hello") +} + +function bar(c: C): string { + const person = new C("Stanislaw", "Lem"); + return c.fullName.call(person); +} + +const person = { + fn: "Ben", + f1: function () { + return this.fn; // here `this` is the current obj + }, + f2: function (): string { + return this.fn; // here `this` is the current obj + }, + f3: () => { + return this.fo; // here `this` is `globalThis` + }, + f4: (): string => { + return this.fo; // here `this` is `globalThis` + }, +}; + +const person1 = { + fn: "Mary", +}; + +console.log(person.f1.apply(person1)); +console.log(person.f2.apply(person1)); +console.log(person.f3.apply(person1)); +console.log(person.f4.apply(person1)); + +foo.apply(undefined); diff --git a/ets2panda/linter/test/main/function_object_methods.sts b/ets2panda/linter/test/main/function_object_methods.sts deleted file mode 100644 index fdf4ec2dabe41e3b35277bc2d17ad552e9a9ed8a..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/function_object_methods.sts +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -interface Author { - firstName: string; - lastName: string; - anotherName: Function; - asyncName: Function; -} - -class C { - firstName: string; - lastName: string; - constructor(firstName: string, lastName: string) { - this.firstName = firstName; - this.lastName = lastName; - this.fullName = this.fullName.bind(this); - this.asyncFullName = this.asyncFullName.bind(this); - } - - author(): Author { - let author: Author = { - firstName: this.firstName, - lastName: this.lastName, - anotherName: this.fullName.bind(this), - asyncName: this.asyncFullName.bind(this) - } - return author - } - - fullName(): string { - return this.firstName + " " + this.lastName; - } - - async asyncFullName(): Promise { - return Promise(fullName()); - } - - sayHello(greet: string) { - console.log(greet, this.fullName()); - } - - async asyncSayHello(greet: string) { - const afn = await this.asyncFullName(); - console.log(greet, afn); - } -} - -async function asyncFoo(): Promise { - const person1 = new C("Arthur", "Clarke"); - const person2 = new C("Ray", "Bradbury"); - - await person1.asyncFullName(); - const afn = await person1.asyncFullName(); - - const afn2 = await person1.asyncFullName.apply(person2); - - const f = person2.asyncFullName.bind(person1); - await f(); - - await asyncBar(person1); - - const ash1: Function = person1.asyncSayHello.bind(person1, "Hello"); - await ash1() - - const ash2: Function = person1.asyncSayHello.bind(person1); - await ash2("Hello") - - await person1.asyncSayHello.apply(person2, "Hello"); - await person1.asyncSayHello.call(person2, "Hello") -} - -async function asyncBar(c: C): Promise { - const person = new C("Stanislaw", "Lem"); - return await c.asyncFullName.call(person); -} - -function foo(): void { - const person1 = new C("Arthur", "Clarke"); - const person2 = new C("Ray", "Bradbury"); - - const fullName = person1.fullName.apply(person2); - - const f = person2.fullName.bind(person1); - f(); - - bar(person1); - - const sh1: Function = person1.sayHello.bind(person1, "Hello"); - sh1() - - const sh2: Function = person1.sayHello.bind(person1); - sh2("Hello") - - person1.sayHello.apply(person2, "Hello"); - person1.sayHello.call(person2, "Hello") -} - -function bar(c: C): string { - const person = new C("Stanislaw", "Lem"); - return c.fullName.call(person); -} - -const person = { - fn: "Ben", - f1: function () { - return this.fn; // here `this` is the current obj - }, - f2: function (): string { - return this.fn; // here `this` is the current obj - }, - f3: () => { - return this.fo; // here `this` is `globalThis` - }, - f4: (): string => { - return this.fo; // here `this` is `globalThis` - }, -}; - -const person1 = { - fn: "Mary", -}; - -console.log(person.f1.apply(person1)); -console.log(person.f2.apply(person1)); -console.log(person.f3.apply(person1)); -console.log(person.f4.apply(person1)); - -foo.apply(undefined); diff --git a/ets2panda/linter/test/main/function_overload.ets b/ets2panda/linter/test/main/function_overload.ets new file mode 100644 index 0000000000000000000000000000000000000000..ae947753a675741d59a6e6340f8885460f2988ad --- /dev/null +++ b/ets2panda/linter/test/main/function_overload.ets @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2023-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. + */ + +function makeDate(timestamp: number): Date; +function makeDate(m: number, d: number, y: number): Date; +function makeDate(mOrTimestamp: number, d?: number, y?: number): Date { + if (d !== undefined && y !== undefined) { + return new Date(y, mOrTimestamp, d); + } else { + return new Date(mOrTimestamp); + } +} +const d1 = makeDate(12345678); +const d2 = makeDate(5, 5, 5); + +function fn(x: string): string; +function fn(x: number): string; +function fn(x: string | number): string { + return 'foo'; +} + +class C { + constructor(x: number, y: string); + constructor(s: string); + constructor(xs: any, y?: any) { + if (typeof xs === 'string') { + const lower = xs.toLowerCase(); + } + } + + m(n: number): void; + m(s: string): void; + m(): void { + const n = 100 + 200; + } +} +let c = new C(10, 'foo'); +c = new C('bar'); +c.m(100); +c.m('bazz'); + +abstract class AbstractClass { + abstract foo(n: number): void; + + bar(s: string): void { + console.log(s); + } + + abstract baz(s: string): void; // Overload + abstract baz(n: number): number; // Overload +} + +declare class DeclareClass { + constructor(); + + foo(): void; + + bar(s: string): number; + + baz(s: string): void; // Overload + baz(n: number): number; // Overload +} + +declare function foobar(n: number): void; // Overload +declare function foobar(s: string): string; // Overload + +declare function barbaz(b: boolean): void; + +namespace X { + function foo(x: number): void; // Overload + function foo(): void { + const n = 300 + 400; + } // Overload + + export function bar(s: string): string; // Overload + export function bar(n: number): string; // Overload + export function bar(arg: number | string): string { + // Overload + return arg.toString(); + } +} + +function f(): void { + function localFun(n: number): void; + function localFun(s: string): void; + function localFun(): void { + const n = 500 + 600; + } +} +interface I { + foo(x: number): void; // Overload + foo(s: string): number; // Overload +} + +class StaticBlock { + static { + function foo(x: number): void; // Overload + function foo(s: string): void; // Overload + function foo(): void { + const n = 700 + 800; + } + } +} diff --git a/ets2panda/linter/test/main/function_overload.sts b/ets2panda/linter/test/main/function_overload.sts deleted file mode 100644 index 55e33bb5e8838e48051569e99777dd94745e7ae0..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/function_overload.sts +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function makeDate(timestamp: number): Date; -function makeDate(m: number, d: number, y: number): Date; -function makeDate(mOrTimestamp: number, d?: number, y?: number): Date { - if (d !== undefined && y !== undefined) { - return new Date(y, mOrTimestamp, d); - } else { - return new Date(mOrTimestamp); - } -} -const d1 = makeDate(12345678); -const d2 = makeDate(5, 5, 5); - -function fn(x: string): string; -function fn(x: number): string; -function fn(x: string | number): string { - return 'foo'; -} - -class C { - constructor(x: number, y: string); - constructor(s: string); - constructor(xs: any, y?: any) { - if (typeof xs === 'string') { - const lower = xs.toLowerCase(); - } - } - - m(n: number): void; - m(s: string): void; - m(): void { - const n = 100 + 200; - } -} -let c = new C(10, 'foo'); -c = new C('bar'); -c.m(100); -c.m('bazz'); - -abstract class AbstractClass { - abstract foo(n: number): void; - - bar(s: string): void { - console.log(s); - } - - abstract baz(s: string): void; // Overload - abstract baz(n: number): number; // Overload -} - -declare class DeclareClass { - constructor(); - - foo(): void; - - bar(s: string): number; - - baz(s: string): void; // Overload - baz(n: number): number; // Overload -} - -declare function foobar(n: number): void; // Overload -declare function foobar(s: string): string; // Overload - -declare function barbaz(b: boolean): void; - -namespace X { - function foo(x: number): void; // Overload - function foo(): void { - const n = 300 + 400; - } // Overload - - export function bar(s: string): string; // Overload - export function bar(n: number): string; // Overload - export function bar(arg: number | string): string { - // Overload - return arg.toString(); - } -} - -function f(): void { - function localFun(n: number): void; - function localFun(s: string): void; - function localFun(): void { - const n = 500 + 600; - } -} -interface I { - foo(x: number): void; // Overload - foo(s: string): number; // Overload -} - -class StaticBlock { - static { - function foo(x: number): void; // Overload - function foo(s: string): void; // Overload - function foo(): void { - const n = 700 + 800; - } - } -} diff --git a/ets2panda/linter/test/main/function_spread_arg.ets b/ets2panda/linter/test/main/function_spread_arg.ets new file mode 100644 index 0000000000000000000000000000000000000000..374490e7cdf11989d57f94f4183cb3f5bc8eb8e6 --- /dev/null +++ b/ets2panda/linter/test/main/function_spread_arg.ets @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022-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. + */ + +function foo(x, y, z) { + console.log(x, y, z); +} + +const args: [number, string, boolean] = [0, 'str', false]; +foo(...args); + +function bar(x, y, ...z) { + console.log(x, y, z); +} + +bar(-1, 0, ...args); + +let arr: number[] = [ 1, 2, 3 ]; +bar(-1, 0, ...arr); + +function foo2(n:number, ...rest: number[]) { + + rest.values +} +function foo3(...rest: bigint[]) {} + +let a1 = [0,1] +let a2 = [1,2,3,4,5] +let a3: number[] = [1,2,3,4,5] +let a4: Array = [1,2,3,4,5] +let a5 = new Array(10) +let a6 = new Int8Array(10) +let a7 = new Uint8Array(10) +let a8 = new Uint8ClampedArray(10) +let a9 = new Int16Array(10) +let a10 = new Uint16Array(10) +let a11 = new Int32Array(10) +let a12 = new Uint32Array(10) +let a13 = new Float32Array(10) +let a14 = new Float64Array(10) +let a15 = new BigInt64Array(10) +let a16 = new BigUint64Array(10) + +foo2(1, ...a1) +foo2(1, ...a2) +foo2(1, ...a3) +foo2(1, ...a4) +foo2(1, ...a5) +foo2(1, ...a6) +foo2(1, ...a7) +foo2(1, ...a8) +foo2(1, ...a9) +foo2(1, ...a10) +foo2(1, ...a11) +foo2(1, ...a12) +foo2(1, ...a13) +foo2(1, ...a14) +foo3(...a15) +foo3(...a16) + +function fUnionArr(...a: (number | string)[]) {} +let x: (number | string)[] = []; +let y = [1, 'string', 10] +let z: [number, string, number] = [1, 'string', 10] + +fUnionArr(...x); +/** + * OK, spec 7.5.2 Type inference from Types of Elements + * type is (number | string)[], it is inferred from types of the elements. + */ +fUnionArr(...y); +/** + * NOT OK + * type is explicitly specified to be tuple + */ +fUnionArr(...z); diff --git a/ets2panda/linter/test/main/function_spread_arg.sts b/ets2panda/linter/test/main/function_spread_arg.sts deleted file mode 100644 index 58df922bb867e1031308acbd3cca11173179607e..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/function_spread_arg.sts +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function foo(x, y, z) { - console.log(x, y, z); -} - -const args: [number, string, boolean] = [0, 'str', false]; -foo(...args); - -function bar(x, y, ...z) { - console.log(x, y, z); -} - -bar(-1, 0, ...args); - -let arr: number[] = [ 1, 2, 3 ]; -bar(-1, 0, ...arr); - -function foo2(n:number, ...rest: number[]) { - - rest.values -} -function foo3(...rest: bigint[]) {} - -let a1 = [0,1] -let a2 = [1,2,3,4,5] -let a3: number[] = [1,2,3,4,5] -let a4: Array = [1,2,3,4,5] -let a5 = new Array(10) -let a6 = new Int8Array(10) -let a7 = new Uint8Array(10) -let a8 = new Uint8ClampedArray(10) -let a9 = new Int16Array(10) -let a10 = new Uint16Array(10) -let a11 = new Int32Array(10) -let a12 = new Uint32Array(10) -let a13 = new Float32Array(10) -let a14 = new Float64Array(10) -let a15 = new BigInt64Array(10) -let a16 = new BigUint64Array(10) - -foo2(1, ...a1) -foo2(1, ...a2) -foo2(1, ...a3) -foo2(1, ...a4) -foo2(1, ...a5) -foo2(1, ...a6) -foo2(1, ...a7) -foo2(1, ...a8) -foo2(1, ...a9) -foo2(1, ...a10) -foo2(1, ...a11) -foo2(1, ...a12) -foo2(1, ...a13) -foo2(1, ...a14) -foo3(...a15) -foo3(...a16) - -function fUnionArr(...a: (number | string)[]) {} -let x: (number | string)[] = []; -let y = [1, 'string', 10] -let z: [number, string, number] = [1, 'string', 10] - -fUnionArr(...x); -/** - * OK, spec 7.5.2 Type inference from Types of Elements - * type is (number | string)[], it is inferred from types of the elements. - */ -fUnionArr(...y); -/** - * NOT OK - * type is explicitly specified to be tuple - */ -fUnionArr(...z); diff --git a/ets2panda/linter/test/main/functions.ets b/ets2panda/linter/test/main/functions.ets new file mode 100644 index 0000000000000000000000000000000000000000..76057b59c668436a2395274250ba9ffcd0ae677a --- /dev/null +++ b/ets2panda/linter/test/main/functions.ets @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2022-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. + */ + +import { foo, bar } from "./oh_modules/ohos_lib" + +function restSpread() { + const arr = [1, 2, 3]; + function test(a, ...t) { + console.log(a); // 1 + console.log(t[0]); // 2 + console.log(t[1]); // 3 + } + test(1, ...arr); +} + +class MyGenerator { + public *getValues() { + // you can put the return type Generator, but it is ot necessary as ts will infer + let index = 1; + while (true) { + yield index; + index = index + 1; + + if (index > 10) { + break; + } + } + } +} + +function defaultTypeParam(i: t, j: tt) { + const c = i; + const s = j; +} + +function arrowFunctionTest() { + const empty = () => {}; // no return type + + const double = (x: number) => x * 2; // no return type + + const square = (x): number => x * x; // no param type + + const sqrt = (x) => Math.sqrt(x); // shortcut syntax + const even = [1, 2, 3, 4, 5, 6].filter((x) => x % 2 === 0); // shortcut syntax + + const foo = (x: number, y): boolean => x == y; // types are partly omitted + + const generic = (t: T, e: E) => t; // Generic lambda +} + +function fooThis(i: number): void { + this.c = 10; +} +class C { + c: number; + m = fooThis; +} + +function choose(x: T, y: T): T { + return Math.random() < 0.5 ? x : y; +} +const choice1 = choose(10, 20); +const choice2 = choose('apple', 'orange'); + +class Collection { + items: T[] = []; + + constructor(...args: T[]) { + if (!args) return; + + for (const arg of args) this.items.push(arg); + } +} +const col = new Collection(1, 2, 3); +const col2 = new Collection('a', 'b', 'c'); + +function f(a: string): number { + return 42; +} + +foo(f(null)); +foo(null); + +foo(() => { + f(null); +}); + +bar(() => { + f(null); +}, null, f(null)); + +bar(() => { + bar(() => { + f(null); + }, null, f(null)); +}, null, foo(f(null))); + +type PropDecorator = () => void; +let Builder: PropDecorator; + +// this test is useless until we use custom tsc +@Builder +function buildSwiper() { + f(null) + foo(null) { + f(null) + foo(null) { + f(null) + foo(() => { + f(null) + }) + } + .foo(null) + } +} diff --git a/ets2panda/linter/test/main/functions.sts b/ets2panda/linter/test/main/functions.sts deleted file mode 100644 index 1ab8e06691e44e3205fdbb08f29e04fb4c0e0162..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/functions.sts +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { foo, bar } from "./oh_modules/ohos_lib" - -function restSpread() { - const arr = [1, 2, 3]; - function test(a, ...t) { - console.log(a); // 1 - console.log(t[0]); // 2 - console.log(t[1]); // 3 - } - test(1, ...arr); -} - -class MyGenerator { - public *getValues() { - // you can put the return type Generator, but it is ot necessary as ts will infer - let index = 1; - while (true) { - yield index; - index = index + 1; - - if (index > 10) { - break; - } - } - } -} - -function defaultTypeParam(i: t, j: tt) { - const c = i; - const s = j; -} - -function arrowFunctionTest() { - const empty = () => {}; // no return type - - const double = (x: number) => x * 2; // no return type - - const square = (x): number => x * x; // no param type - - const sqrt = (x) => Math.sqrt(x); // shortcut syntax - const even = [1, 2, 3, 4, 5, 6].filter((x) => x % 2 === 0); // shortcut syntax - - const foo = (x: number, y): boolean => x == y; // types are partly omitted - - const generic = (t: T, e: E) => t; // Generic lambda -} - -function fooThis(i: number): void { - this.c = 10; -} -class C { - c: number; - m = fooThis; -} - -function choose(x: T, y: T): T { - return Math.random() < 0.5 ? x : y; -} -const choice1 = choose(10, 20); -const choice2 = choose('apple', 'orange'); - -class Collection { - items: T[] = []; - - constructor(...args: T[]) { - if (!args) return; - - for (const arg of args) this.items.push(arg); - } -} -const col = new Collection(1, 2, 3); -const col2 = new Collection('a', 'b', 'c'); - -function f(a: string): number { - return 42; -} - -foo(f(null)); -foo(null); - -foo(() => { - f(null); -}); - -bar(() => { - f(null); -}, null, f(null)); - -bar(() => { - bar(() => { - f(null); - }, null, f(null)); -}, null, foo(f(null))); - -type PropDecorator = () => void; -let Builder: PropDecorator; - -// this test is useless until we use custom tsc -@Builder -function buildSwiper() { - f(null) - foo(null) { - f(null) - foo(null) { - f(null) - foo(() => { - f(null) - }) - } - .foo(null) - } -} diff --git a/ets2panda/linter/test/main/generators.ets b/ets2panda/linter/test/main/generators.ets new file mode 100644 index 0000000000000000000000000000000000000000..1ea75b3e5765f94713bcc51c870fda86fc2b1182 --- /dev/null +++ b/ets2panda/linter/test/main/generators.ets @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023-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. + */ + +function* countAppleSales() { + const saleList = [3, 7, 5]; + for (let i = 0; i < saleList.length; i++) { + yield saleList[i]; + } +} + +const foo = function* () { + yield 'a'; + yield 'b'; + yield 'c'; +}; +let str = ''; +for (const val of foo()) { + str = str + val; +} +console.log(str); // Expected output: "abc" + +class MyGenerator { + public *getValues() { + // you can put the return type Generator, but it is ot necessary as ts will infer + let index = 1; + while (true) { + yield index; + index = index + 1; + + if (index > 10) { + break; + } + } + } +} + +function* func1() { + yield 42; +} +function* func2() { + yield* func1(); +} +const iterator = func2(); +console.log(iterator.next().value); // Expected output: 42 + +function* g1() { + yield 2; + yield 3; + yield 4; +} +function* g2() { + yield 1; + yield* g1(); + yield 5; +} +const gen = g2(); +console.log(gen.next()); // {value: 1, done: false} +console.log(gen.next()); // {value: 2, done: false} +console.log(gen.next()); // {value: 3, done: false} +console.log(gen.next()); // {value: 4, done: false} +console.log(gen.next()); // {value: 5, done: false} +console.log(gen.next()); // {value: undefined, done: true} diff --git a/ets2panda/linter/test/main/generators.sts b/ets2panda/linter/test/main/generators.sts deleted file mode 100644 index 7a9d3a547d2f8bbf4bf10b6149bfca450e7847fb..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/generators.sts +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -function* countAppleSales() { - const saleList = [3, 7, 5]; - for (let i = 0; i < saleList.length; i++) { - yield saleList[i]; - } -} - -const foo = function* () { - yield 'a'; - yield 'b'; - yield 'c'; -}; -let str = ''; -for (const val of foo()) { - str = str + val; -} -console.log(str); // Expected output: "abc" - -class MyGenerator { - public *getValues() { - // you can put the return type Generator, but it is ot necessary as ts will infer - let index = 1; - while (true) { - yield index; - index = index + 1; - - if (index > 10) { - break; - } - } - } -} - -function* func1() { - yield 42; -} -function* func2() { - yield* func1(); -} -const iterator = func2(); -console.log(iterator.next().value); // Expected output: 42 - -function* g1() { - yield 2; - yield 3; - yield 4; -} -function* g2() { - yield 1; - yield* g1(); - yield 5; -} -const gen = g2(); -console.log(gen.next()); // {value: 1, done: false} -console.log(gen.next()); // {value: 2, done: false} -console.log(gen.next()); // {value: 3, done: false} -console.log(gen.next()); // {value: 4, done: false} -console.log(gen.next()); // {value: 5, done: false} -console.log(gen.next()); // {value: undefined, done: true} diff --git a/ets2panda/linter/test/main/global_this.sts b/ets2panda/linter/test/main/global_this.ets similarity index 100% rename from ets2panda/linter/test/main/global_this.sts rename to ets2panda/linter/test/main/global_this.ets diff --git a/ets2panda/linter/test/main/implements_class.ets b/ets2panda/linter/test/main/implements_class.ets new file mode 100644 index 0000000000000000000000000000000000000000..92144146d23da6251053f3fcd49c495a33f35258 --- /dev/null +++ b/ets2panda/linter/test/main/implements_class.ets @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022-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. + */ + +class A { + foo(): void { + console.log('Class A foo'); + } +} + +class B {} + +interface I { + f: number; +} + +class C extends A implements I, B { + f = 100; +} + +const c = new C(); +c.foo(); diff --git a/ets2panda/linter/test/main/implements_class.sts b/ets2panda/linter/test/main/implements_class.sts deleted file mode 100644 index 928f8714a6b89075b208e4950d2b191c14ff1018..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/implements_class.sts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class A { - foo(): void { - console.log('Class A foo'); - } -} - -class B {} - -interface I { - f: number; -} - -class C extends A implements I, B { - f = 100; -} - -const c = new C(); -c.foo(); diff --git a/ets2panda/linter/test/main/import_after_statement.ets b/ets2panda/linter/test/main/import_after_statement.ets new file mode 100644 index 0000000000000000000000000000000000000000..0a1b371766873a7b4258e86fac4a1f8fcd2196b7 --- /dev/null +++ b/ets2panda/linter/test/main/import_after_statement.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022-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. + */ + +import X1 from "X"; +import { X2 } from "X"; +import { X3 as X4 } from "X"; + +function f() {}; + +import X5 from "X"; +import { X6 } from "X"; +import { X7 as X8 } from "X"; diff --git a/ets2panda/linter/test/main/import_after_statement.sts b/ets2panda/linter/test/main/import_after_statement.sts deleted file mode 100644 index 34e71ad4ecd4e8980529d06b57a678cb3bfac8dc..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/import_after_statement.sts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import X1 from "X"; -import { X2 } from "X"; -import { X3 as X4 } from "X"; - -function f() {}; - -import X5 from "X"; -import { X6 } from "X"; -import { X7 as X8 } from "X"; diff --git a/ets2panda/linter/test/main/import_assertion.ets b/ets2panda/linter/test/main/import_assertion.ets new file mode 100644 index 0000000000000000000000000000000000000000..33424359371283a0779fbe307305ff793ecc0337 --- /dev/null +++ b/ets2panda/linter/test/main/import_assertion.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022-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. + */ + +import { obj } from 'something.json' assert { type: 'json' }; + +function foo(): void { + const obj1 = import('something.json', { assert: { type: 'json' } }); + const obj2 = await import('something.json', { assert: { type: 'json' } }); + + const assert = { type: 'json' }; + const obj3 = import('something.json', { assert }); +} diff --git a/ets2panda/linter/test/main/import_assertion.sts b/ets2panda/linter/test/main/import_assertion.sts deleted file mode 100644 index 35a5e2c81c90a4f08c3a498d8c472538c99142e9..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/import_assertion.sts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { obj } from 'something.json' assert { type: 'json' }; - -function foo(): void { - const obj1 = import('something.json', { assert: { type: 'json' } }); - const obj2 = await import('something.json', { assert: { type: 'json' } }); - - const assert = { type: 'json' }; - const obj3 = import('something.json', { assert }); -} diff --git a/ets2panda/linter/test/main/import_type.ets b/ets2panda/linter/test/main/import_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..b25ba8934a1f4da4be91a294e9a25ae33726dd1f --- /dev/null +++ b/ets2panda/linter/test/main/import_type.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023-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. + */ + +// Access an exported type +type WriteFileOptions = import('fs').WriteFileOptions; + +// access the type of an exported value +type WriteFileFunction = typeof import('fs').writeFile; + + \ No newline at end of file diff --git a/ets2panda/linter/test/main/import_type.sts b/ets2panda/linter/test/main/import_type.sts deleted file mode 100644 index 425c1ac1a2f3b8ef7b59271d39e5fb66eaf76d71..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/import_type.sts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Access an exported type -type WriteFileOptions = import('fs').WriteFileOptions; - -// access the type of an exported value -type WriteFileFunction = typeof import('fs').writeFile; - - \ No newline at end of file diff --git a/ets2panda/linter/test/main/imported_use_as_object.ets b/ets2panda/linter/test/main/imported_use_as_object.ets new file mode 100644 index 0000000000000000000000000000000000000000..4ae0f1611694b532e4607920fcd44589fb73afb6 --- /dev/null +++ b/ets2panda/linter/test/main/imported_use_as_object.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022-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. + */ + +import { OuterC, OuterN, OuterE } from './imported_use_as_object_module' + +class InnerC { } +namespace InnerN { let x = 1; } +enum InnerE { e = 1 } + +function foo1(f: Function) { } +function foo2(o: Object) { } + +foo1(() => OuterC); +foo2(OuterC); +typeof OuterC; + +foo1(() => OuterN); +foo2(OuterN); +typeof OuterN; + +foo1(() => OuterE); +foo2(OuterE); +typeof OuterE; + +foo1(() => InnerC); +foo2(InnerC); +typeof InnerC; + +foo1(() => InnerN); +foo2(InnerN); +typeof InnerN; + +foo1(() => InnerE); +foo2(InnerE); +typeof InnerE; + +OuterE[OuterE.e]; +InnerE[InnerE.e]; diff --git a/ets2panda/linter/test/main/imported_use_as_object.sts b/ets2panda/linter/test/main/imported_use_as_object.sts deleted file mode 100644 index dee6bb3086b3427694fdffa7a5cb8aab8efd84b5..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/imported_use_as_object.sts +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { OuterC, OuterN, OuterE } from './imported_use_as_object_module' - -class InnerC { } -namespace InnerN { let x = 1; } -enum InnerE { e = 1 } - -function foo1(f: Function) { } -function foo2(o: Object) { } - -foo1(() => OuterC); -foo2(OuterC); -typeof OuterC; - -foo1(() => OuterN); -foo2(OuterN); -typeof OuterN; - -foo1(() => OuterE); -foo2(OuterE); -typeof OuterE; - -foo1(() => InnerC); -foo2(InnerC); -typeof InnerC; - -foo1(() => InnerN); -foo2(InnerN); -typeof InnerN; - -foo1(() => InnerE); -foo2(InnerE); -typeof InnerE; - -OuterE[OuterE.e]; -InnerE[InnerE.e]; diff --git a/ets2panda/linter/test/main/index_negative.sts b/ets2panda/linter/test/main/index_negative.ets similarity index 100% rename from ets2panda/linter/test/main/index_negative.sts rename to ets2panda/linter/test/main/index_negative.ets diff --git a/ets2panda/linter/test/main/indexable_type_element_access.ets b/ets2panda/linter/test/main/indexable_type_element_access.ets new file mode 100644 index 0000000000000000000000000000000000000000..161da91473119e9006622a36ebfc4bf5fcfab88e --- /dev/null +++ b/ets2panda/linter/test/main/indexable_type_element_access.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023-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. + */ + +class ClassWithGet { + $_get(index: number): ClassWithGet { + return this; + } +} +let getClass = new ClassWithGet(); +getClass = getClass[0]; +getClass[0] = getClass; // Error - no $_set function available + +class ClassWithSet { + $_set(index: number, value: ClassWithSet) {} +} +let setClass = new ClassWithSet(); +setClass = setClass[0]; // Error - no $_get function available +setClass[0] = setClass; + +class ClassWithGetAndSet { + $_get(index: number): ClassWithGetAndSet { + return this; + } + + $_set(index: number, value: ClassWithGetAndSet) {} +} +let getSetClass = new ClassWithGetAndSet(); +getSetClass = getSetClass[0]; +getSetClass[0] = getSetClass; diff --git a/ets2panda/linter/test/main/indexable_type_element_access.sts b/ets2panda/linter/test/main/indexable_type_element_access.sts deleted file mode 100644 index a5ff84bca39f9eb4f46b38bfa0bfb36cd24d0263..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/indexable_type_element_access.sts +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class ClassWithGet { - $_get(index: number): ClassWithGet { - return this; - } -} -let getClass = new ClassWithGet(); -getClass = getClass[0]; -getClass[0] = getClass; // Error - no $_set function available - -class ClassWithSet { - $_set(index: number, value: ClassWithSet) {} -} -let setClass = new ClassWithSet(); -setClass = setClass[0]; // Error - no $_get function available -setClass[0] = setClass; - -class ClassWithGetAndSet { - $_get(index: number): ClassWithGetAndSet { - return this; - } - - $_set(index: number, value: ClassWithGetAndSet) {} -} -let getSetClass = new ClassWithGetAndSet(); -getSetClass = getSetClass[0]; -getSetClass[0] = getSetClass; diff --git a/ets2panda/linter/test/main/indexable_type_string_element_access.ets b/ets2panda/linter/test/main/indexable_type_string_element_access.ets new file mode 100644 index 0000000000000000000000000000000000000000..ba10ef59ea39883e105986fb1f6dbe6f06246b28 --- /dev/null +++ b/ets2panda/linter/test/main/indexable_type_string_element_access.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023-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. + */ + +class SomeClass { + $_get (index: string): SomeClass { return this } + $_set (index: string, value: SomeClass) { } +} +let x = new SomeClass +x = x["index string"] +// This notation implies a call: x = x.$_get ("index string") +x["index string"] = x +// This notation implies a call: x.$_set ("index string", x) diff --git a/ets2panda/linter/test/main/indexable_type_string_element_access.sts b/ets2panda/linter/test/main/indexable_type_string_element_access.sts deleted file mode 100644 index 4483199f36b66d95cbb3ded8fb39be628ed91280..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/indexable_type_string_element_access.sts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class SomeClass { - $_get (index: string): SomeClass { return this } - $_set (index: string, value: SomeClass) { } -} -let x = new SomeClass -x = x["index string"] -// This notation implies a call: x = x.$_get ("index string") -x["index string"] = x -// This notation implies a call: x.$_set ("index string", x) diff --git a/ets2panda/linter/test/main/indexed_derived_from_array.ets b/ets2panda/linter/test/main/indexed_derived_from_array.ets new file mode 100644 index 0000000000000000000000000000000000000000..05a354b5ae091e4b88f43698828645ed906f3e98 --- /dev/null +++ b/ets2panda/linter/test/main/indexed_derived_from_array.ets @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022-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. + */ + +let nextId: number = 0 +class ObservedArray extends Array {} + +let arr1 = new ObservedArray(1, 2, 3) +console.log(arr1[0]) +console.log(arr1[nextId]) + +class NumberArray extends Array {} +let arr2 = new NumberArray(4, 5, 6) +console.log(arr2[0]) +console.log(arr2[nextId]) + +class NumberTypedArray extends Uint32Array {} +let arr3 = new NumberTypedArray([7, 8, 9]) +console.log(arr3[0]) +console.log(arr3[nextId]) + +class A extends Uint32Array {} +class B extends A {} +class C extends B {} +let arr4 = new C([10, 11, 12]) +console.log(arr4[0]) +console.log(arr4[nextId]) + +class Point extends Array { + constructor(x: T, y: T) { + super(x, y) + } + + public add(rhs: Point): Point { + this[0] = this.compare(this[0], rhs[0], true); + return this; + } + + private compare(left: T, right: T, flag:boolean): T { + return flag ? left : right + } +} + +class PointNumber extends Array { + constructor(x: number, y: number) { + super(x, y) + } + + public add(rhs: PointNumber): PointNumber { + this[0] = this.compare(this[0], rhs[0], true); + return this; + } + + private compare(left: number, right: number, flag: boolean): number { + return flag ? left : right + } +} diff --git a/ets2panda/linter/test/main/indexed_derived_from_array.sts b/ets2panda/linter/test/main/indexed_derived_from_array.sts deleted file mode 100644 index 91ddc4ee2177967223f54498eda92d7ec9282b1f..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/indexed_derived_from_array.sts +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -let nextId: number = 0 -class ObservedArray extends Array {} - -let arr1 = new ObservedArray(1, 2, 3) -console.log(arr1[0]) -console.log(arr1[nextId]) - -class NumberArray extends Array {} -let arr2 = new NumberArray(4, 5, 6) -console.log(arr2[0]) -console.log(arr2[nextId]) - -class NumberTypedArray extends Uint32Array {} -let arr3 = new NumberTypedArray([7, 8, 9]) -console.log(arr3[0]) -console.log(arr3[nextId]) - -class A extends Uint32Array {} -class B extends A {} -class C extends B {} -let arr4 = new C([10, 11, 12]) -console.log(arr4[0]) -console.log(arr4[nextId]) - -class Point extends Array { - constructor(x: T, y: T) { - super(x, y) - } - - public add(rhs: Point): Point { - this[0] = this.compare(this[0], rhs[0], true); - return this; - } - - private compare(left: T, right: T, flag:boolean): T { - return flag ? left : right - } -} - -class PointNumber extends Array { - constructor(x: number, y: number) { - super(x, y) - } - - public add(rhs: PointNumber): PointNumber { - this[0] = this.compare(this[0], rhs[0], true); - return this; - } - - private compare(left: number, right: number, flag: boolean): number { - return flag ? left : right - } -} diff --git a/ets2panda/linter/test/main/inferred_type.ets b/ets2panda/linter/test/main/inferred_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..7ae129997a325e8234d8cfff41a1b8c63f533e68 --- /dev/null +++ b/ets2panda/linter/test/main/inferred_type.ets @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022-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. + */ + +const [a, b, c] = [1, 'hello', true]; + +console.log(a); +console.log(b); +console.log(c); + +function foo(): any {} + +let x; // is implicitly 'Any' +let y = foo(); // is implicitly 'Any' + +function bar(x) {} // 'x' is implicitly 'Any' +let baz = (x: number, y) => {}; // 'y' is implicitly 'Any' + +class C { + f; // is implicitly 'Any' + g = foo(); // is implicitly 'Any' + + h(x, y, ...z) {} // 'x', 'y' and 'z' are implicitly 'Any' +} + +let s1 = new Array(10); +let s2 = new Array(10); +let s3 = new Array(10); + +class D { + f; // is implicitly 'Any' + constructor() { + this.f = 1; + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/inferred_type.sts b/ets2panda/linter/test/main/inferred_type.sts deleted file mode 100644 index b89c594ebca3f85fb8058fe94b185905e5280c30..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/inferred_type.sts +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const [a, b, c] = [1, 'hello', true]; - -console.log(a); -console.log(b); -console.log(c); - -function foo(): any {} - -let x; // is implicitly 'Any' -let y = foo(); // is implicitly 'Any' - -function bar(x) {} // 'x' is implicitly 'Any' -let baz = (x: number, y) => {}; // 'y' is implicitly 'Any' - -class C { - f; // is implicitly 'Any' - g = foo(); // is implicitly 'Any' - - h(x, y, ...z) {} // 'x', 'y' and 'z' are implicitly 'Any' -} - -let s1 = new Array(10); -let s2 = new Array(10); -let s3 = new Array(10); - -class D { - f; // is implicitly 'Any' - constructor() { - this.f = 1; - } -} \ No newline at end of file diff --git a/ets2panda/linter/test/main/instanceof.ets b/ets2panda/linter/test/main/instanceof.ets new file mode 100644 index 0000000000000000000000000000000000000000..6c94781fa4bf62c0fda9ae594417d61d88980a55 --- /dev/null +++ b/ets2panda/linter/test/main/instanceof.ets @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022-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. + */ + +class X {} + +const a = new X() instanceof Object; // true +const b = new X() instanceof X; // true + +// left operand is a type: +const c = X instanceof Object; // Compile-time error +const d = X instanceof X; // Compile-time error + +// left operand may be of any reference type, like number +const e = (5.0 as Number) instanceof Number; // false + +const g = new Number(3) instanceof Number; + +let bad2Number = 5.0 as Number; +let pi = 3.14; +bad2Number = pi as Number; + +let bad2Boolean = true as Boolean; +bad2Boolean = a as Boolean; + +const aa = this instanceof Object; +const bb = this instanceof X; + +class A extends Error { + constructor(message?: string) { + super(message) + console.log(this instanceof Error) + } +} + +let ce = new A() + +// left operand is a type: +const fc = 3 instanceof Object; // Compile-time error +const fd = "s" instanceof Object; // Compile-time error + +const ff = new String("d") instanceof Number; + +var le = () => { return String instanceof Object }; + +class SomeClass { + static readonly field = SomeClass instanceof Object; + + methodRet() { + return SomeClass instanceof Object; + } + + methodTestCondition(): boolean { + if (SomeClass instanceof SomeClass) return true; + return false; + } + + methodWhileCondition() { + while (SomeClass instanceof SomeClass) { + } + } + + methodDoWhileNegativeCondition() { + do { + } while (!SomeClass instanceof SomeClass) + } +} diff --git a/ets2panda/linter/test/main/instanceof.sts b/ets2panda/linter/test/main/instanceof.sts deleted file mode 100644 index 3e7f2dec92850070f4ceec30ebab56c5cafbf9ba..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/instanceof.sts +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class X {} - -const a = new X() instanceof Object; // true -const b = new X() instanceof X; // true - -// left operand is a type: -const c = X instanceof Object; // Compile-time error -const d = X instanceof X; // Compile-time error - -// left operand may be of any reference type, like number -const e = (5.0 as Number) instanceof Number; // false - -const g = new Number(3) instanceof Number; - -let bad2Number = 5.0 as Number; -let pi = 3.14; -bad2Number = pi as Number; - -let bad2Boolean = true as Boolean; -bad2Boolean = a as Boolean; - -const aa = this instanceof Object; -const bb = this instanceof X; - -class A extends Error { - constructor(message?: string) { - super(message) - console.log(this instanceof Error) - } -} - -let ce = new A() - -// left operand is a type: -const fc = 3 instanceof Object; // Compile-time error -const fd = "s" instanceof Object; // Compile-time error - -const ff = new String("d") instanceof Number; - -var le = () => { return String instanceof Object }; - -class SomeClass { - static readonly field = SomeClass instanceof Object; - - methodRet() { - return SomeClass instanceof Object; - } - - methodTestCondition(): boolean { - if (SomeClass instanceof SomeClass) return true; - return false; - } - - methodWhileCondition() { - while (SomeClass instanceof SomeClass) { - } - } - - methodDoWhileNegativeCondition() { - do { - } while (!SomeClass instanceof SomeClass) - } -} diff --git a/ets2panda/linter/test/main/interface_literal_prop_name.ets b/ets2panda/linter/test/main/interface_literal_prop_name.ets new file mode 100644 index 0000000000000000000000000000000000000000..621e98d58d8f77cbc0b250a0248943d8c5c163f0 --- /dev/null +++ b/ets2panda/linter/test/main/interface_literal_prop_name.ets @@ -0,0 +1,60 @@ +/* + * 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 enum E { + str = 'e5' +} + +interface I { + one: string, + 2: string, // ERROR v1.1 + '3': string, // OK v1.1 rule relax case + [E.str]: string; // OK v1.1 rule relax case +} + +let vInterface1: I = { + one: 'i1', + 2: 'i2', // ERROR v1.1 + '3': 'i3', // OK v1.1 + [E.str]: 'e5' // OK v1.1 +} +let vInterface2: I = { + one: 'i1', + 2: 'i2', // ERROR v1.1 + '3': 'i3', // OK v1.1 + 'e5': 'e5' // OK v1.1 +} + +class C { + public one?: string = 'c1'; + public 3?: string = 'c3'; // ERROR v1.1 + public '4': string = 'c4'; // OK v1.1 rule relax case + public [E.str]: string = 'e5'; // OK v1.1 rule relax case +} + +let vClass1: C = { + one: 'c1', + 3: 'c3', // ERROR v1.1 + '4': 'c4', // OK v1.1 + 'e5': 'e5' // OK v1.1 +} + +let vClass2: C = { + one: 'c1', + 3: 'c3', // ERROR v1.1 + '4': 'c4', // OK v1.1 + [E.str]: 'e5' // OK v1.1 +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/interface_literal_prop_name.sts b/ets2panda/linter/test/main/interface_literal_prop_name.sts deleted file mode 100644 index eeef044e3eca2b09acb8f37ca514773e8b54427d..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/interface_literal_prop_name.sts +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -export enum E { - str = 'e5' -} - -interface I { - one: string, - 2: string, // ERROR v1.1 - '3': string, // OK v1.1 rule relax case - [E.str]: string; // OK v1.1 rule relax case -} - -let vInterface1: I = { - one: 'i1', - 2: 'i2', // ERROR v1.1 - '3': 'i3', // OK v1.1 - [E.str]: 'e5' // OK v1.1 -} -let vInterface2: I = { - one: 'i1', - 2: 'i2', // ERROR v1.1 - '3': 'i3', // OK v1.1 - 'e5': 'e5' // OK v1.1 -} - -class C { - public one?: string = 'c1'; - public 3?: string = 'c3'; // ERROR v1.1 - public '4': string = 'c4'; // OK v1.1 rule relax case - public [E.str]: string = 'e5'; // OK v1.1 rule relax case -} - -let vClass1: C = { - one: 'c1', - 3: 'c3', // ERROR v1.1 - '4': 'c4', // OK v1.1 - 'e5': 'e5' // OK v1.1 -} - -let vClass2: C = { - one: 'c1', - 3: 'c3', // ERROR v1.1 - '4': 'c4', // OK v1.1 - [E.str]: 'e5' // OK v1.1 -} \ No newline at end of file diff --git a/ets2panda/linter/test/main/interfaces_props.ets b/ets2panda/linter/test/main/interfaces_props.ets new file mode 100644 index 0000000000000000000000000000000000000000..3d1b66f7ffefb188b42f9f1362c79e4e05170a1b --- /dev/null +++ b/ets2panda/linter/test/main/interfaces_props.ets @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022-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 Mover { + move(): void; + getStatus(): { speed: number }; +} + +interface Shaker { + shake(): void; + getStatus(): { frequency: number }; +} + +interface MoverShaker extends Mover, Shaker { + getStatus(): { speed: number; frequency: number }; +} + +class C implements MoverShaker { + move(): void { + console.log("move"); + } + + shake(): void { + console.log("shake"); + } + + getStatus() { + const speed = 100; + const frequency = 300; + return { speed, frequency }; + } +} + +const c = new C(); +c.move(); +c.shake(); + +const st = c.getStatus(); +console.log(st); + +const obj: Object = c; + +if (obj && obj.move && obj.shake && obj.getStatus) { + const moverShaker = obj as MoverShaker; + + console.log(moverShaker); +} diff --git a/ets2panda/linter/test/main/interfaces_props.sts b/ets2panda/linter/test/main/interfaces_props.sts deleted file mode 100644 index 8c90a6cd98fe6b3de650ae21bf87525b5087d92b..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/interfaces_props.sts +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -interface Mover { - move(): void; - getStatus(): { speed: number }; -} - -interface Shaker { - shake(): void; - getStatus(): { frequency: number }; -} - -interface MoverShaker extends Mover, Shaker { - getStatus(): { speed: number; frequency: number }; -} - -class C implements MoverShaker { - move(): void { - console.log("move"); - } - - shake(): void { - console.log("shake"); - } - - getStatus() { - const speed = 100; - const frequency = 300; - return { speed, frequency }; - } -} - -const c = new C(); -c.move(); -c.shake(); - -const st = c.getStatus(); -console.log(st); - -const obj: Object = c; - -if (obj && obj.move && obj.shake && obj.getStatus) { - const moverShaker = obj as MoverShaker; - - console.log(moverShaker); -} diff --git a/ets2panda/linter/test/main/interop_import.ets b/ets2panda/linter/test/main/interop_import.ets new file mode 100644 index 0000000000000000000000000000000000000000..3c46d326b213e4d4d4332d4c3755db4799024d97 --- /dev/null +++ b/ets2panda/linter/test/main/interop_import.ets @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022-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. + */ + +import { CPreview } from "./.preview/preview_import"; +import { CBuild } from "./build/build_import"; +import { CNode } from "./node_modules/node_import"; +import { COh } from "./oh_modules/oh_import"; +import { CGood } from "./ignore_files/good"; +import { CHvigor } from "./ignore_files/hvigorfile"; +import { Cjs2 } from "./node_modules/2"; +import { Cjs1 } from "./node_modules/node_lib/1"; + +CPreview.prototype; +CBuild.prototype; +CNode.prototype; +COh.prototype; +CGood.prototype; +CHvigor.prototype; +Cjs1.prototype; +Cjs2.prototype; + +class B { + f: number = 1; +} +let CJS1: Cjs1 = new Cjs1(); +let cjs1 = new Cjs1(); +let f1: B = cjs1; +let CJS2: Cjs2 = new Cjs2(); +let cjs2 = new Cjs2(); +let f2: B = cjs2; diff --git a/ets2panda/linter/test/main/interop_import.sts b/ets2panda/linter/test/main/interop_import.sts deleted file mode 100644 index 312f1ba11b541acca997d9bbca9c05093792403e..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/interop_import.sts +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { CPreview } from "./.preview/preview_import"; -import { CBuild } from "./build/build_import"; -import { CNode } from "./node_modules/node_import"; -import { COh } from "./oh_modules/oh_import"; -import { CGood } from "./ignore_files/good"; -import { CHvigor } from "./ignore_files/hvigorfile"; -import { Cjs2 } from "./node_modules/2"; -import { Cjs1 } from "./node_modules/node_lib/1"; - -CPreview.prototype; -CBuild.prototype; -CNode.prototype; -COh.prototype; -CGood.prototype; -CHvigor.prototype; -Cjs1.prototype; -Cjs2.prototype; - -class B { - f: number = 1; -} -let CJS1: Cjs1 = new Cjs1(); -let cjs1 = new Cjs1(); -let f1: B = cjs1; -let CJS2: Cjs2 = new Cjs2(); -let cjs2 = new Cjs2(); -let f2: B = cjs2; diff --git a/ets2panda/linter/test/main/interop_imports.ets b/ets2panda/linter/test/main/interop_imports.ets new file mode 100644 index 0000000000000000000000000000000000000000..8ac3ade976b7ec3a58d7ddfa92ae9c0974f0c82f --- /dev/null +++ b/ets2panda/linter/test/main/interop_imports.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022-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. + */ + +import { f, ff } from "./oh_modules/ohos_lib"; + +f({ + onAction: () => { + console.log("g"); + }, +}); + +ff({}); diff --git a/ets2panda/linter/test/main/interop_imports.sts b/ets2panda/linter/test/main/interop_imports.sts deleted file mode 100644 index 1a3c3c255fe9f94b9e59b698a8aa1059faad8bf4..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/interop_imports.sts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { f, ff } from "./oh_modules/ohos_lib"; - -f({ - onAction: () => { - console.log("g"); - }, -}); - -ff({}); diff --git a/ets2panda/linter/test/main/invalid_identifier.sts b/ets2panda/linter/test/main/invalid_identifier.ets similarity index 100% rename from ets2panda/linter/test/main/invalid_identifier.sts rename to ets2panda/linter/test/main/invalid_identifier.ets diff --git a/ets2panda/linter/test/main/lazy_import.sts b/ets2panda/linter/test/main/lazy_import.ets similarity index 100% rename from ets2panda/linter/test/main/lazy_import.sts rename to ets2panda/linter/test/main/lazy_import.ets diff --git a/ets2panda/linter/test/main/library_type_call.ets b/ets2panda/linter/test/main/library_type_call.ets new file mode 100644 index 0000000000000000000000000000000000000000..70641634b50e757c7ed7476ab7a231af3370addd --- /dev/null +++ b/ets2panda/linter/test/main/library_type_call.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + + /* + * test case issue 17357 + * After change extention .sts to .ets got an errors #23897 + */ + +import { C } from './classC'; + +class D { + c?: C = undefined; + + foo() { + this.c?.bar(0).then(); // No errors, method came from '.ts' file + this.c?.bar(undefined).then(); // Neither 'overload-error' nor 'object-is-possibly-undefined' errors + } +} diff --git a/ets2panda/linter/test/main/library_type_call.sts b/ets2panda/linter/test/main/library_type_call.sts deleted file mode 100644 index d32617bbf0bda9aca8a8f68d005ad3f3764cf876..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/library_type_call.sts +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - /* - * test case issue 17357 - */ - -import { C } from './classC'; - -class D { - c?: C = undefined; - - foo() { - this.c?.bar(0).then(); // No errors, method came from '.ts' file - this.c?.bar(undefined).then(); // Neither 'overload-error' nor 'object-is-possibly-undefined' errors - } -} diff --git a/ets2panda/linter/test/main/library_type_call.ts.json b/ets2panda/linter/test/main/library_type_call.ts.json index 167c48a33a21c1fc3895d8e8830b9cf767800a97..c91747ea9898fd460a15ac70057079460d738c87 100644 --- a/ets2panda/linter/test/main/library_type_call.ts.json +++ b/ets2panda/linter/test/main/library_type_call.ts.json @@ -13,5 +13,26 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 28, + "column": 9, + "endLine": 28, + "endColumn": 31, + "problem": "StrictDiagnostic", + "suggest": "Object is possibly 'undefined'.", + "rule": "Object is possibly 'undefined'.", + "severity": "ERROR" + }, + { + "line": 28, + "column": 21, + "endLine": 28, + "endColumn": 30, + "problem": "StrictDiagnostic", + "suggest": "No overload matches this call.\n Overload 1 of 2, '(id: number, name?: string | undefined): Promise | undefined', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'number'.\n Overload 2 of 2, '(id: number): void | undefined', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'number'.", + "rule": "No overload matches this call.\n Overload 1 of 2, '(id: number, name?: string | undefined): Promise | undefined', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'number'.\n Overload 2 of 2, '(id: number): void | undefined', gave the following error.\n Argument of type 'undefined' is not assignable to parameter of type 'number'.", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/limit_void_type.sts b/ets2panda/linter/test/main/limit_void_type.ets similarity index 100% rename from ets2panda/linter/test/main/limit_void_type.sts rename to ets2panda/linter/test/main/limit_void_type.ets diff --git a/ets2panda/linter/test/main/limited_literal_type.sts b/ets2panda/linter/test/main/limited_literal_type.ets similarity index 100% rename from ets2panda/linter/test/main/limited_literal_type.sts rename to ets2panda/linter/test/main/limited_literal_type.ets diff --git a/ets2panda/linter/test/main/limited_stdlib_api.ets b/ets2panda/linter/test/main/limited_stdlib_api.ets new file mode 100644 index 0000000000000000000000000000000000000000..15e2f493e161ea574402f67c64c7431fc2568bee --- /dev/null +++ b/ets2panda/linter/test/main/limited_stdlib_api.ets @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2022-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. + */ + +/// Global +eval('console.log("foo")'); +let inf = Infinity; +let nan = NaN; +isFinite(1); +isNaN(2); +parseFloat('3'); +parseInt('4', 10); +encodeURI(''); +encodeURIComponent(''); +decodeURI(''); +decodeURIComponent(''); +escape(''); +unescape(''); + +global.eval('console.log("foo")'); +globalThis.eval('console.log("foo")'); +const evl = "eval('console.log(1)')"; +const res: void = Function(evl)(); +const arr1 = ['foo', 'foo', 'foo']; + +class C {} +let c = new C(); +interface Obj { + a: string; + b: number; + c: boolean; +} +const object1: Obj = { + a: 'somestring', + b: 42, + c: false, +}; + +/// Object +/// Object . __proto__ () +/// Object . __defineGetter__ () +/// Object . __defineSetter__ () +/// Object . __lookupGetter__ () +/// Object . __lookupSetter__ () +Object.assign(c, c); +Object.create(c); +Object.defineProperties(c, {}); +Object.defineProperty(c, 'p', c); +Object.entries([]); +Object.freeze(() => {}); +Object.fromEntries([]); // OK +Object.getOwnPropertyDescriptor(c, 'p'); +Object.getOwnPropertyDescriptors(c); +Object.getOwnPropertySymbols(c); +Object.getPrototypeOf(c); +Object.hasOwnProperty('p'); +Object.is(c, c); +Object.isExtensible(c); +Object.isFrozen(c); +Object.isPrototypeOf(c); +Object.isSealed(c); +Object.preventExtensions(c); +Object.propertyIsEnumerable('p'); +Object.seal(c); +Object.setPrototypeOf(c, c); +console.log(Object.getOwnPropertyNames(object1)); +console.log(Object.hasOwn(object1, 'a')); +console.log(Object.hasOwn(object1, 'toString')); +console.log(Object.keys(object1)); +console.log(Object.values(object1)); +console.log(Object.entries(object1)); + +/// Reflect +Reflect.apply(() => {}, c, []); +Reflect.construct(C, []); +Reflect.defineProperty(c, 'p', {}); +Reflect.deleteProperty(c, 'p', ); +Reflect.get(c, 'p'); +Reflect.getOwnPropertyDescriptor(c, 'p'); +Reflect.getPrototypeOf(c); +Reflect.isExtensible(c); +Reflect.preventExtensions(c); +Reflect.setPrototypeOf(c, c); +console.log(Reflect.has(object1, 'a')); +console.log(Reflect.has(object1, 'toString')); +console.log(Reflect.get(object1, 'a')); +console.log(Reflect.get(object1, 'd')); +console.log(Reflect.ownKeys(object1)); +Reflect.set(object1, 'b', 43); +Reflect.set(arr1, 2, 'bar'); + +/// Proxy +let handler: ProxyHandler = {}; +if (handler.apply) handler.apply(c, c, []); +if (handler.construct) handler.construct(c, [], () => {}); +if (handler.defineProperty) handler.defineProperty(c, "prop", {}); +if (handler.deleteProperty) handler.deleteProperty(c, "prop"); +if (handler.get) handler.get(c, "prop", {}); +if (handler.getOwnPropertyDescriptor) handler.getOwnPropertyDescriptor(c, "prop"); +if (handler.getPrototypeOf) handler.getPrototypeOf(c); +if (handler.has) handler.has(c, "prop"); +if (handler.isExtensible) handler.isExtensible(c); +if (handler.ownKeys) handler.ownKeys(c); +if (handler.preventExtensions) handler.preventExtensions(c); +if (handler.set) handler.set(c, "prop", 1, c); +if (handler.setPrototypeOf) handler.setPrototypeOf(c, null); + +/// Array +ArrayBuffer.isView({}); +let a: number[] = []; +let b = new ArrayBuffer(1); +Array.isArray(a); + +// 'Object.assign' is allowed only with signature like: 'assign(target: Record, ...source: Object[]): Record' +class C2 { a: number } +class C3 { [k: string]: Object }; +const rec: Record = {'1': 1, '2': 2}; +const rec2: Record = Object.assign(rec, new C2()); // OK +const rec3: C2 = Object.assign(rec, new C2()); // NOT OK, return type is 'C2' +const rec4: Object = Object.assign(rec, new C2()); // NOT OK, return type is 'Object' +const rec5 = Object.assign(rec, new C2()); // NOT OK, return type is intersection type 'Record & C2' +const rec6: Record = Object.assign(new C2(), new C3()); // NOT OK, target type is 'C2' +Object.assign(rec, new C2()); // NOT OK, no return (context) type diff --git a/ets2panda/linter/test/main/limited_stdlib_api.sts b/ets2panda/linter/test/main/limited_stdlib_api.sts deleted file mode 100644 index fbc378260ffb510abb27bafbf77513f6ad15caa3..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/limited_stdlib_api.sts +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/// Global -eval('console.log("foo")'); -let inf = Infinity; -let nan = NaN; -isFinite(1); -isNaN(2); -parseFloat('3'); -parseInt('4', 10); -encodeURI(''); -encodeURIComponent(''); -decodeURI(''); -decodeURIComponent(''); -escape(''); -unescape(''); - -global.eval('console.log("foo")'); -globalThis.eval('console.log("foo")'); -const evl = "eval('console.log(1)')"; -const res: void = Function(evl)(); -const arr1 = ['foo', 'foo', 'foo']; - -class C {} -let c = new C(); -interface Obj { - a: string; - b: number; - c: boolean; -} -const object1: Obj = { - a: 'somestring', - b: 42, - c: false, -}; - -/// Object -/// Object . __proto__ () -/// Object . __defineGetter__ () -/// Object . __defineSetter__ () -/// Object . __lookupGetter__ () -/// Object . __lookupSetter__ () -Object.assign(c, c); -Object.create(c); -Object.defineProperties(c, {}); -Object.defineProperty(c, 'p', c); -Object.entries([]); -Object.freeze(() => {}); -Object.fromEntries([]); // OK -Object.getOwnPropertyDescriptor(c, 'p'); -Object.getOwnPropertyDescriptors(c); -Object.getOwnPropertySymbols(c); -Object.getPrototypeOf(c); -Object.hasOwnProperty('p'); -Object.is(c, c); -Object.isExtensible(c); -Object.isFrozen(c); -Object.isPrototypeOf(c); -Object.isSealed(c); -Object.preventExtensions(c); -Object.propertyIsEnumerable('p'); -Object.seal(c); -Object.setPrototypeOf(c, c); -console.log(Object.getOwnPropertyNames(object1)); -console.log(Object.hasOwn(object1, 'a')); -console.log(Object.hasOwn(object1, 'toString')); -console.log(Object.keys(object1)); -console.log(Object.values(object1)); -console.log(Object.entries(object1)); - -/// Reflect -Reflect.apply(() => {}, c, []); -Reflect.construct(C, []); -Reflect.defineProperty(c, 'p', {}); -Reflect.deleteProperty(c, 'p', ); -Reflect.get(c, 'p'); -Reflect.getOwnPropertyDescriptor(c, 'p'); -Reflect.getPrototypeOf(c); -Reflect.isExtensible(c); -Reflect.preventExtensions(c); -Reflect.setPrototypeOf(c, c); -console.log(Reflect.has(object1, 'a')); -console.log(Reflect.has(object1, 'toString')); -console.log(Reflect.get(object1, 'a')); -console.log(Reflect.get(object1, 'd')); -console.log(Reflect.ownKeys(object1)); -Reflect.set(object1, 'b', 43); -Reflect.set(arr1, 2, 'bar'); - -/// Proxy -let handler: ProxyHandler = {}; -if (handler.apply) handler.apply(c, c, []); -if (handler.construct) handler.construct(c, [], () => {}); -if (handler.defineProperty) handler.defineProperty(c, "prop", {}); -if (handler.deleteProperty) handler.deleteProperty(c, "prop"); -if (handler.get) handler.get(c, "prop", {}); -if (handler.getOwnPropertyDescriptor) handler.getOwnPropertyDescriptor(c, "prop"); -if (handler.getPrototypeOf) handler.getPrototypeOf(c); -if (handler.has) handler.has(c, "prop"); -if (handler.isExtensible) handler.isExtensible(c); -if (handler.ownKeys) handler.ownKeys(c); -if (handler.preventExtensions) handler.preventExtensions(c); -if (handler.set) handler.set(c, "prop", 1, c); -if (handler.setPrototypeOf) handler.setPrototypeOf(c, null); - -/// Array -ArrayBuffer.isView({}); -let a: number[] = []; -let b = new ArrayBuffer(1); -Array.isArray(a); - -// 'Object.assign' is allowed only with signature like: 'assign(target: Record, ...source: Object[]): Record' -class C2 { a: number } -class C3 { [k: string]: Object }; -const rec: Record = {'1': 1, '2': 2}; -const rec2: Record = Object.assign(rec, new C2()); // OK -const rec3: C2 = Object.assign(rec, new C2()); // NOT OK, return type is 'C2' -const rec4: Object = Object.assign(rec, new C2()); // NOT OK, return type is 'Object' -const rec5 = Object.assign(rec, new C2()); // NOT OK, return type is intersection type 'Record & C2' -const rec6: Record = Object.assign(new C2(), new C3()); // NOT OK, target type is 'C2' -Object.assign(rec, new C2()); // NOT OK, no return (context) type diff --git a/ets2panda/linter/test/main/literals_as_prop_names.ets b/ets2panda/linter/test/main/literals_as_prop_names.ets new file mode 100755 index 0000000000000000000000000000000000000000..f95f51cfe0368bed1c81d82ea39e6fff800d71eb --- /dev/null +++ b/ets2panda/linter/test/main/literals_as_prop_names.ets @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2022-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. + */ + +import { ExportLitAsPropName } from './ignore_files/good'; + +enum LiteralAsPropertyNameEnum { + One = "one", + "PrivateTwo" = "_2", + Two = "Two" +} + +class LiteralAsPropertyName { + public one: string = "1111111111"; + private 2: string; + 'Two': number; +} + +const litAsPropName: LiteralAsPropertyName = { + one: "1", + 2: 'two', + 'Two': 2, +}; + +console.log(litAsPropName["one"]); +console.log(litAsPropName[2]); +console.log(litAsPropName["Two"]); + +class LiteralAsPropertyName_fix { + public one: string = "1111111111"; + private _2: string; + Two: number; +} + +const litAsPropName_fix: LiteralAsPropertyName_fix = { + one: "1111111111", + _2: 'two', + Two: 2, +}; + +console.log("Fixed listAsPropName:"); +console.log(litAsPropName_fix.one); +console.log(litAsPropName_fix._2); +console.log(litAsPropName_fix.Two); + +let x = {"name": 20, 2: 30} + +console.log(x["name"]); +console.log(x[2]); + +class X_class { + public name: number; + public _2: number; +} + +let x_fix = {name: 20, _2: 20}; + +console.log("Fixed x object literal:"); +console.log(x_fix.name); +console.log(x_fix._2); + +interface litAsPropNameIface { + one: string; + 2: string; + '__2': number; +} +const int: litAsPropNameIface = { + one: '12321', + 2: 'weqwewq', + '__2': 123 +}; + +const imp: ExportLitAsPropName = { 1: 234 }; + +LiteralAsPropertyNameEnum['One'] + +LiteralAsPropertyNameEnum['PrivateTwo'] \ No newline at end of file diff --git a/ets2panda/linter/test/main/literals_as_prop_names.sts b/ets2panda/linter/test/main/literals_as_prop_names.sts deleted file mode 100755 index 563114aacb96c802ee28bff55b4a719c61e366c8..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/literals_as_prop_names.sts +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { ExportLitAsPropName } from './ignore_files/good'; - -enum LiteralAsPropertyNameEnum { - One = "one", - "PrivateTwo" = "_2", - Two = "Two" -} - -class LiteralAsPropertyName { - public one: string = "1111111111"; - private 2: string; - 'Two': number; -} - -const litAsPropName: LiteralAsPropertyName = { - one: "1", - 2: 'two', - 'Two': 2, -}; - -console.log(litAsPropName["one"]); -console.log(litAsPropName[2]); -console.log(litAsPropName["Two"]); - -class LiteralAsPropertyName_fix { - public one: string = "1111111111"; - private _2: string; - Two: number; -} - -const litAsPropName_fix: LiteralAsPropertyName_fix = { - one: "1111111111", - _2: 'two', - Two: 2, -}; - -console.log("Fixed listAsPropName:"); -console.log(litAsPropName_fix.one); -console.log(litAsPropName_fix._2); -console.log(litAsPropName_fix.Two); - -let x = {"name": 20, 2: 30} - -console.log(x["name"]); -console.log(x[2]); - -class X_class { - public name: number; - public _2: number; -} - -let x_fix = {name: 20, _2: 20}; - -console.log("Fixed x object literal:"); -console.log(x_fix.name); -console.log(x_fix._2); - -interface litAsPropNameIface { - one: string; - 2: string; - '__2': number; -} -const int: litAsPropNameIface = { - one: '12321', - 2: 'weqwewq', - '__2': 123 -}; - -const imp: ExportLitAsPropName = { 1: 234 }; - -LiteralAsPropertyNameEnum['One'] - -LiteralAsPropertyNameEnum['PrivateTwo'] \ No newline at end of file diff --git a/ets2panda/linter/test/main/loop_over_set.ets b/ets2panda/linter/test/main/loop_over_set.ets new file mode 100644 index 0000000000000000000000000000000000000000..a10c98cb7233c26c488c49dc2175aea2f4539461 --- /dev/null +++ b/ets2panda/linter/test/main/loop_over_set.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023-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. + */ + +enum BarCodeFormat { A, B, C } + +for (let item in BarCodeFormat) { + console.log(item); +} + +for (let i = 0; i < Object.keys(BarCodeFormat).length; i++) { + console.log(BarCodeFormat[i]); +} + +for (let i = 0; i < BarCodeFormat['C']; i++) { + console.log(BarCodeFormat[i]); +} + +for(let i = 0; i < BarCodeFormat.C; i++) { + console.log(BarCodeFormat[i]); +} diff --git a/ets2panda/linter/test/main/loop_over_set.sts b/ets2panda/linter/test/main/loop_over_set.sts deleted file mode 100644 index 38487b854bb99f206fb821ae97c38bed0d03ff19..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/loop_over_set.sts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -enum BarCodeFormat { A, B, C } - -for (let item in BarCodeFormat) { - console.log(item); -} - -for (let i = 0; i < Object.keys(BarCodeFormat).length; i++) { - console.log(BarCodeFormat[i]); -} - -for (let i = 0; i < BarCodeFormat['C']; i++) { - console.log(BarCodeFormat[i]); -} - -for(let i = 0; i < BarCodeFormat.C; i++) { - console.log(BarCodeFormat[i]); -} diff --git a/ets2panda/linter/test/main/mapped_types.ets b/ets2panda/linter/test/main/mapped_types.ets new file mode 100644 index 0000000000000000000000000000000000000000..867e5d851d6007192c5f987f1fa756df40d464e5 --- /dev/null +++ b/ets2panda/linter/test/main/mapped_types.ets @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023-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. + */ + +type Flags = { + [P in keyof T]: boolean; +}; +type MyFlags = { + firstFlag: () => void; + secondFlag: () => void; +}; +type MyOptions = Flags; + +// Removes 'readonly' attributes from a type's properties +type RemoveReadonly = { + -readonly [P in keyof T]: T[P]; +}; +type MutableObject = { + readonly id: string; + readonly name: string; +}; +type ImmutableObject = RemoveReadonly; + +// Removes 'optional' attributes from a type's properties +type RemoveOptional = { + [P in keyof T]-?: T[P]; +}; +type PartialObject = { + id: string; + name?: string; + age?: number; +}; +type CompleteObject = RemoveOptional; + +// Creates new property names from prior ones: +type PropertiesToGetters = { + [P in keyof T as `get${Capitalize< + string & P + >}`]: () => T[P]; +}; +interface User { + login: string; + email: string; + data: string; +} +type LazyUser = PropertiesToGetters; + +// Combine with Conditional type: +type ExtractBar = { + [Prop in keyof T]: T[Prop] extends { bar: true } + ? true + : false; +}; +type Entity = { + identity: { name: 'Object' }; + details: { foo: string; bar: true }; +}; +type ExtractedProps = ExtractBar; diff --git a/ets2panda/linter/test/main/mapped_types.sts b/ets2panda/linter/test/main/mapped_types.sts deleted file mode 100644 index 19a0dddd9acb1ac3ef2c19375c8b37c40f662018..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/mapped_types.sts +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -type Flags = { - [P in keyof T]: boolean; -}; -type MyFlags = { - firstFlag: () => void; - secondFlag: () => void; -}; -type MyOptions = Flags; - -// Removes 'readonly' attributes from a type's properties -type RemoveReadonly = { - -readonly [P in keyof T]: T[P]; -}; -type MutableObject = { - readonly id: string; - readonly name: string; -}; -type ImmutableObject = RemoveReadonly; - -// Removes 'optional' attributes from a type's properties -type RemoveOptional = { - [P in keyof T]-?: T[P]; -}; -type PartialObject = { - id: string; - name?: string; - age?: number; -}; -type CompleteObject = RemoveOptional; - -// Creates new property names from prior ones: -type PropertiesToGetters = { - [P in keyof T as `get${Capitalize< - string & P - >}`]: () => T[P]; -}; -interface User { - login: string; - email: string; - data: string; -} -type LazyUser = PropertiesToGetters; - -// Combine with Conditional type: -type ExtractBar = { - [Prop in keyof T]: T[Prop] extends { bar: true } - ? true - : false; -}; -type Entity = { - identity: { name: 'Object' }; - details: { foo: string; bar: true }; -}; -type ExtractedProps = ExtractBar; diff --git a/ets2panda/linter/test/main/method_overriding.sts b/ets2panda/linter/test/main/method_overriding.ets similarity index 100% rename from ets2panda/linter/test/main/method_overriding.sts rename to ets2panda/linter/test/main/method_overriding.ets diff --git a/ets2panda/linter/test/main/modules.ets b/ets2panda/linter/test/main/modules.ets new file mode 100644 index 0000000000000000000000000000000000000000..8815adec0281a48a207640a2e87643df1fd583d7 --- /dev/null +++ b/ets2panda/linter/test/main/modules.ets @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2022-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. + */ + +import './short_test.ts'; // import from path + +import { test } from './short_test.ts'; + +function withStmt() { + const radius = 12; + with (Math) { + const area = PI * radius * radius; + } +} + +namespace X { + export class C {} + export interface I { + f: number; + } + export const n = 15; + export enum E {} + export function foo(a: number): number { + return a * a; + } + export type N = number; + + export namespace XX { + export class D {} + } + + console.log('Namespace X is initialized'); + { + console.log('Block statement'); + const blockVar = 10; + } +} +const xc = new X.C(); +class IImpl implements X.I { + f = 10; +} +const xn: X.N = X.foo(100); +const d: X.XX.D = new X.XX.D(); + +namespace Y.Z { + export function bar(): void { + const x = 200; + } +} +Y.Z.bar(); + +// Namespace used as an object or type. +const x = X; +console.log(x.n); + +const xxx = X.XX; +const dd = new xxx.D(); + +function xfoo(x: typeof X): void { + x.foo(25); +} +xfoo(X); + +function yzbar(yz: typeof Y.Z): void { + yz.bar(); +} +yzbar(Y.Z); + +import { default as def } from 'module'; // default import + +interface I { + f: number; +} +export default I; // default interface export +export default function (n: number) { + n++; +} // default function export +export default class MyClass {} // default class export + +// type-only import +import type { APIResponseType } from './api'; +import type * as P from 'foo'; +import { type T1, type T2 as T3 } from 'foobar'; + +// type-only export +export type { TypeA as TypeB }; +export { type TypeFoo as TypeBar }; + +// Re-exporting +export * from 'module1'; // Ok +export * as Utilities from 'module2'; // Ok +export { SomeFunction, SomeType as OtherType } from 'module3'; // Ok + +class Point {} +export = Point; + +import Validator = require('module'); +import Button = Components.Button; + +let m = require('moduleM'); +const k = require('moduleK'); + +export { Point }; +export { Point as P }; +export default Point; + +export { someFunction1 } from 'module3' +export { default as someFunction2 } from 'module3' diff --git a/ets2panda/linter/test/main/modules.sts b/ets2panda/linter/test/main/modules.sts deleted file mode 100644 index c567235134124303c62731270ab3194ea02faf93..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/modules.sts +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import './short_test.ts'; // import from path - -import { test } from './short_test.ts'; - -function withStmt() { - const radius = 12; - with (Math) { - const area = PI * radius * radius; - } -} - -namespace X { - export class C {} - export interface I { - f: number; - } - export const n = 15; - export enum E {} - export function foo(a: number): number { - return a * a; - } - export type N = number; - - export namespace XX { - export class D {} - } - - console.log('Namespace X is initialized'); - { - console.log('Block statement'); - const blockVar = 10; - } -} -const xc = new X.C(); -class IImpl implements X.I { - f = 10; -} -const xn: X.N = X.foo(100); -const d: X.XX.D = new X.XX.D(); - -namespace Y.Z { - export function bar(): void { - const x = 200; - } -} -Y.Z.bar(); - -// Namespace used as an object or type. -const x = X; -console.log(x.n); - -const xxx = X.XX; -const dd = new xxx.D(); - -function xfoo(x: typeof X): void { - x.foo(25); -} -xfoo(X); - -function yzbar(yz: typeof Y.Z): void { - yz.bar(); -} -yzbar(Y.Z); - -import { default as def } from 'module'; // default import - -interface I { - f: number; -} -export default I; // default interface export -export default function (n: number) { - n++; -} // default function export -export default class MyClass {} // default class export - -// type-only import -import type { APIResponseType } from './api'; -import type * as P from 'foo'; -import { type T1, type T2 as T3 } from 'foobar'; - -// type-only export -export type { TypeA as TypeB }; -export { type TypeFoo as TypeBar }; - -// Re-exporting -export * from 'module1'; // Ok -export * as Utilities from 'module2'; // Ok -export { SomeFunction, SomeType as OtherType } from 'module3'; // Ok - -class Point {} -export = Point; - -import Validator = require('module'); -import Button = Components.Button; - -let m = require('moduleM'); -const k = require('moduleK'); - -export { Point }; -export { Point as P }; -export default Point; - -export { someFunction1 } from 'module3' -export { default as someFunction2 } from 'module3' diff --git a/ets2panda/linter/test/main/namespace_export_assignment.ets b/ets2panda/linter/test/main/namespace_export_assignment.ets new file mode 100644 index 0000000000000000000000000000000000000000..db6283ff576bdf19a70507e4ba2f2a92d584601b --- /dev/null +++ b/ets2panda/linter/test/main/namespace_export_assignment.ets @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023-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. + */ + +namespace B {}; +namespace C {}; +export default B; +export { B as C }; diff --git a/ets2panda/linter/test/main/namespace_export_assignment.sts b/ets2panda/linter/test/main/namespace_export_assignment.sts deleted file mode 100644 index eb934443272a3e86a6dd5418fe335943af758cbb..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/namespace_export_assignment.sts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -namespace B {}; -namespace C {}; -export default B; -export { B as C }; diff --git a/ets2panda/linter/test/main/new_target.ets b/ets2panda/linter/test/main/new_target.ets new file mode 100644 index 0000000000000000000000000000000000000000..14ebbe4dbe96f3f58f814da0c9c73694ee19d894 --- /dev/null +++ b/ets2panda/linter/test/main/new_target.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023-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. + */ + +class CustomError extends Error { + constructor(message?: string) { + super(message); + Object.setPrototypeOf(this, new.target.prototype); + } +} + +function f() { + if (new.target) { + console.log("inside of 'if(new.target)'"); + } +} diff --git a/ets2panda/linter/test/main/new_target.sts b/ets2panda/linter/test/main/new_target.sts deleted file mode 100644 index f6fa650dc31cec26a67512aa63a3baf428c19611..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/new_target.sts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class CustomError extends Error { - constructor(message?: string) { - super(message); - Object.setPrototypeOf(this, new.target.prototype); - } -} - -function f() { - if (new.target) { - console.log("inside of 'if(new.target)'"); - } -} diff --git a/ets2panda/linter/test/main/no_side_effect_import.sts b/ets2panda/linter/test/main/no_side_effect_import.ets similarity index 100% rename from ets2panda/linter/test/main/no_side_effect_import.sts rename to ets2panda/linter/test/main/no_side_effect_import.ets diff --git a/ets2panda/linter/test/main/no_tuples_arrays.sts b/ets2panda/linter/test/main/no_tuples_arrays.ets similarity index 100% rename from ets2panda/linter/test/main/no_tuples_arrays.sts rename to ets2panda/linter/test/main/no_tuples_arrays.ets diff --git a/ets2panda/linter/test/main/non_initializable_prop_decorators.ets b/ets2panda/linter/test/main/non_initializable_prop_decorators.ets new file mode 100644 index 0000000000000000000000000000000000000000..a12109cacee619973a979b041ecac14b1330420f --- /dev/null +++ b/ets2panda/linter/test/main/non_initializable_prop_decorators.ets @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022-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. + */ + +type PropDecorator = (target: any, propertyKey: any) => void; +type PropDecoratorWithCall = PropDecorator & ((val: string) => PropDecorator); + +let Prop: PropDecorator; +let Link: PropDecorator; +let ObjectLink: PropDecorator; +let Consume: PropDecoratorWithCall; +let Provide: PropDecoratorWithCall; +let CustomDialog: PropDecorator = (x, y) => {}; + +class Decorators { + @Prop + p: number; + + @Link + l: number; + + @ObjectLink + public ol: number; + + @Consume + private c: number; + + @Consume('foo') + c2: number; + + @Provide + pr: number // Not ok + + @Provide('bar') + pr2: number; // Not ok + + @Prop + @Provide + @Link + f: number; + + f2: number; + + f3: any; + + f4; +} + +class CustomDialogController { +}; + +@CustomDialog +class A { + controller: CustomDialogController; + build() {} +} + +class B { + controller: CustomDialogController; + build() {} +} diff --git a/ets2panda/linter/test/main/non_initializable_prop_decorators.sts b/ets2panda/linter/test/main/non_initializable_prop_decorators.sts deleted file mode 100644 index c953e74540a71ce7be49cd97b1b1f71a68aa2324..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/non_initializable_prop_decorators.sts +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -type PropDecorator = (target: any, propertyKey: any) => void; -type PropDecoratorWithCall = PropDecorator & ((val: string) => PropDecorator); - -let Prop: PropDecorator; -let Link: PropDecorator; -let ObjectLink: PropDecorator; -let Consume: PropDecoratorWithCall; -let Provide: PropDecoratorWithCall; -let CustomDialog: PropDecorator = (x, y) => {}; - -class Decorators { - @Prop - p: number; - - @Link - l: number; - - @ObjectLink - public ol: number; - - @Consume - private c: number; - - @Consume('foo') - c2: number; - - @Provide - pr: number // Not ok - - @Provide('bar') - pr2: number; // Not ok - - @Prop - @Provide - @Link - f: number; - - f2: number; - - f3: any; - - f4; -} - -class CustomDialogController { -}; - -@CustomDialog -class A { - controller: CustomDialogController; - build() {} -} - -class B { - controller: CustomDialogController; - build() {} -} diff --git a/ets2panda/linter/test/main/not_support_decorator.sts b/ets2panda/linter/test/main/not_support_decorator.ets similarity index 100% rename from ets2panda/linter/test/main/not_support_decorator.sts rename to ets2panda/linter/test/main/not_support_decorator.ets diff --git a/ets2panda/linter/test/main/null_check_calls.ets b/ets2panda/linter/test/main/null_check_calls.ets new file mode 100644 index 0000000000000000000000000000000000000000..d3bc988ede9f3ef7042033f190cbddf49122f11d --- /dev/null +++ b/ets2panda/linter/test/main/null_check_calls.ets @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2022-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. + */ + +import { ff } from "./oh_modules/ohos_lib"; +import { f, f2, bar, applyToUnknown, fooExecute, C1, myFoo, myFoo2 } from "./dynamic_lib"; + +let a1: C1 = new C1() + +function g1(): C1 | null { + if (a1) { + return a1 + } + return null +} + +f2(g1()) + +bar(null); +bar(null, null); +applyToUnknown(null); +fooExecute(null); + +function fff(a: Array): void {} + +fff(null); +ff(null); +f(null); + + +((a: number) => a)(null); +(a: null | number) => ((b: number) => b)(a); + +function inc(a: number) { return a + 1 } +function neg2null(x: number) { return x > 0 ? x : null } +inc(neg2null(-1)); + +myFoo({x: "", y: null}) + +class A2 { + public a: Object = null + f() { + myFoo2( + () => { + this.a = null; + } + ) + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/null_check_calls.sts b/ets2panda/linter/test/main/null_check_calls.sts deleted file mode 100644 index 5c408340d48efee08e242e0d29849496e4304824..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/null_check_calls.sts +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { ff } from "./oh_modules/ohos_lib"; -import { f, f2, bar, applyToUnknown, fooExecute, C1, myFoo, myFoo2 } from "./dynamic_lib"; - -let a1: C1 = new C1() - -function g1(): C1 | null { - if (a1) { - return a1 - } - return null -} - -f2(g1()) - -bar(null); -bar(null, null); -applyToUnknown(null); -fooExecute(null); - -function fff(a: Array): void {} - -fff(null); -ff(null); -f(null); - - -((a: number) => a)(null); -(a: null | number) => ((b: number) => b)(a); - -function inc(a: number) { return a + 1 } -function neg2null(x: number) { return x > 0 ? x : null } -inc(neg2null(-1)); - -myFoo({x: "", y: null}) - -class A2 { - public a: Object = null - f() { - myFoo2( - () => { - this.a = null; - } - ) - } -} \ No newline at end of file diff --git a/ets2panda/linter/test/main/numeric_semantics.sts b/ets2panda/linter/test/main/numeric_semantics.ets similarity index 100% rename from ets2panda/linter/test/main/numeric_semantics.sts rename to ets2panda/linter/test/main/numeric_semantics.ets diff --git a/ets2panda/linter/test/main/object_literals.ets b/ets2panda/linter/test/main/object_literals.ets new file mode 100644 index 0000000000000000000000000000000000000000..411c0d3b4bda04a6bd012d3322852d5c47a32e17 --- /dev/null +++ b/ets2panda/linter/test/main/object_literals.ets @@ -0,0 +1,237 @@ +/* + * Copyright (c) 2023-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. + */ +import {ohFunction1, ohFunction2, OhosI} from './oh_modules/ohos_lib' +interface I { + a: number; + b: string; +} + +class C { + a: number; + b: string; +} + +class C2 { + q: { x: number; y: string }; + w: any; + e: I; + r: C; +} + +function localVariable(): void { + // Declaration + let a1 = { a: 1, b: 'a' }; // NOT OK + let a2: any = { a: 2, b: 'b' }; // OK - ASSIGNMENT TO ANY + let a3: { a: number; b: string } = { a: 30, b: 'c' }; // NOT OK + let a4: I = { a: 4, b: 'd' }; // OK + let a5: C = { a: 5, b: 'e' }; // OK + let a6: C2 = { + // OK + q: { x: 6, y: 'f' }, // NOT OK + w: { a: 7, b: 'g' }, // OK - ASSIGNMENT TO ANY + e: { a: 8, b: 'h' }, // OK + r: { a: 9, b: 'i' }, // OK + }; + + // Assignment + a1 = { a: 11, b: 'a' }; // NOT OK + a2 = { a: 12, b: 'b' }; // OK - ASSIGNMENT TO ANY + a3 = { a: 13, b: 'c' }; // NOT OK + a4 = { a: 14, b: 'd' }; // OK + a5 = { a: 15, b: 'e' }; // OK + a6 = { + // OK + q: { x: 16, y: 'f' }, // NOT OK + w: { a: 17, b: 'g' }, // OK - ASSIGNMENT TO ANY + e: { a: 18, b: 'h' }, // OK + r: { a: 19, b: 'i' }, // OK + }; +} + +function defaultParamValue(): void { + function foo(x = { a: 21, b: 'a' }) { + console.log(x.a, x.b); + } // NOT OK + function foo2(x: any = { a: 22, b: 'b' }) { + console.log(x.a, x.b); + } // NOT OK + function foo3(x: { a: number; b: string } = { a: 23, b: 'c' }) { + console.log(x.a, x.b); + } // NOT OK + function foo4(x: I = { a: 24, b: 'd' }) { + console.log(x.a, x.b); + } // OK + function foo5(x: C = { a: 25, b: 'e' }) { + console.log(x.a, x.b); + } // OK + + // Function call + foo({ a: 21, b: 'a' }); // NOT OK + foo2({ a: 22, b: 'b' }); // OK - ASSIGNMENT TO ANY + foo3({ a: 23, b: 'c' }); // NOT OK + foo4({ a: 24, b: 'd' }); // OK + foo5({ a: 25, b: 'e' }); // OK +} + +function returnFromFunction(): void { + function bar() { + return { a: 31, b: 'a' }; + } // NOT OK + function bar2(): any { + return { a: 32, b: 'b' }; + } // OK - ASSIGNMENT TO ANY + function bar3(): { a: number; b: string } { + return { a: 33, b: 'c' }; + } // NOT OK + function bar4(): I { + return { a: 34, b: 'd' }; + } // OK + function bar5(): C { + return { a: 35, b: 'e' }; + } // OK +} + +function ternaryOperator(): void { + // In ternary operator + const condition = true; + const a1 = condition ? { a: 41, b: 'a' } : { a: 42, b: 'b' }; // NOT OK + const a2: any = condition ? { a: 43, b: 'c' } : { a: 44, b: 'd' }; // OK - ASSIGNMENT TO ANY + const a3: { a: number; b: string } = condition + ? { a: 45, b: 'e' } + : { a: 46, b: 'f' }; // NOT OK + const a4: I = condition ? { a: 47, b: 'g' } : { a: 48, b: 'h' }; // OK + const a5: C = condition ? { a: 49, b: 'i' } : { a: 50, b: 'j' }; // OK +} + +function arrayLiteral(): void { + const arr1 = [ + { a: 51, b: 'a' }, + { a: 52, b: 'b' }, + ]; // NOT OK + const arr2: any[] = [ + { a: 53, b: 'c' }, + { a: 54, b: 'd' }, + ]; // OK - ASSIGNMENT TO ANY + const arr3: { a: number; b: string }[] = [ + { a: 55, b: 'e' }, + { a: 56, b: 'f' }, + ]; // NOT OK + const arr4: I[] = [ + { a: 57, b: 'g' }, + { a: 58, b: 'h' }, + ]; // OK + const arr5: C[] = [ + { a: 59, b: 'i' }, + { a: 60, b: 'j' }, + ]; // OK +} + +enum E { + OK, + NO_OK, +} +interface I1 { + v: E | number +} + +interface I2 { + v: E +} + +let i1: I1 = {v:E.OK} +let i2: I2 = {v:E.NO_OK} + +function g1(a: E) { + let ii1: I1 = {v:a} + let ii2: I2 = {v:a} +} + +function g(): boolean { + return true; +} +interface CondI { + a: number; +} +let a1: CondI = { + a: g() ? 0 : 1, +}; +let b1: CondI = { + a: (g() ? 0 : 1) as number, +}; +let c1 = g() ? 0 : 1; +let d1: CondI = { + a: c1, +}; +let e1: CondI = { +a: 0|1|2|3 +} +let f1: 0|1|2|3 = 3 +let ff : CondI = { + a: f1 +} + +let dict = new Map(); +dict.set('1', 123) + +interface III { + param?: string | number | boolean +} + +let test1: III = { param: dict.get('1') } as III +let test2: III = { param: dict.get('1')! } as III +let test3: III = { param: dict.get('1') as number } as III +let test4: III = { param: dict.get('1') as (number | string) } as III +export interface Style { +} +export class SwitchMenuStyle implements Style { +} +export class ProfileOneLineSwitchMenuStyle extends SwitchMenuStyle { +} +export class ProfileSwitchMenuStyle extends SwitchMenuStyle { +} +export let hfpProfileSwitchMenuStyle = new ProfileSwitchMenuStyle(); +export let hfpProfileOneLineSwitchMenuStyle = new ProfileOneLineSwitchMenuStyle(); + +export interface SettingsBaseMenuData { + style?: Style; +} + +function test(isDisConnected:boolean){ + let a={style: isDisConnected ? hfpProfileOneLineSwitchMenuStyle: hfpProfileSwitchMenuStyle} as SettingsBaseMenuData +} + +interface PPP { + x: number + y: number | undefined + z?: number +} + +let p1: PPP = {x: 10, y: 10} +let p2: PPP = {x: 10, y: undefined} +let p3: PPP = {x: 10, y: undefined, z: undefined} +let p4: PPP = {x: 10, y: undefined, z: 10} +let p5: PPP = {x: 10, y: 10, z: 10} +const cp1: PPP = {x: 10, y: 10} +const cp2: PPP = {x: 10, y: undefined} +const cp3: PPP = {x: 10, y: undefined, z: undefined} +const cp4: PPP = {x: 10, y: undefined, z: 10} +const cp5: PPP = {x: 10, y: 10, z: 10} + +const oi: OhosI = { f: 1 }; + +ohFunction1({d: oi}) +ohFunction1({d: {f: 1}}) +ohFunction2({d: oi}) +ohFunction2({d: {f: 1}}) diff --git a/ets2panda/linter/test/main/object_literals.sts b/ets2panda/linter/test/main/object_literals.sts deleted file mode 100644 index 4131caeb4d99b14965665536946e44e72daa2639..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_literals.sts +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import {ohFunction1, ohFunction2, OhosI} from './oh_modules/ohos_lib' -interface I { - a: number; - b: string; -} - -class C { - a: number; - b: string; -} - -class C2 { - q: { x: number; y: string }; - w: any; - e: I; - r: C; -} - -function localVariable(): void { - // Declaration - let a1 = { a: 1, b: 'a' }; // NOT OK - let a2: any = { a: 2, b: 'b' }; // OK - ASSIGNMENT TO ANY - let a3: { a: number; b: string } = { a: 30, b: 'c' }; // NOT OK - let a4: I = { a: 4, b: 'd' }; // OK - let a5: C = { a: 5, b: 'e' }; // OK - let a6: C2 = { - // OK - q: { x: 6, y: 'f' }, // NOT OK - w: { a: 7, b: 'g' }, // OK - ASSIGNMENT TO ANY - e: { a: 8, b: 'h' }, // OK - r: { a: 9, b: 'i' }, // OK - }; - - // Assignment - a1 = { a: 11, b: 'a' }; // NOT OK - a2 = { a: 12, b: 'b' }; // OK - ASSIGNMENT TO ANY - a3 = { a: 13, b: 'c' }; // NOT OK - a4 = { a: 14, b: 'd' }; // OK - a5 = { a: 15, b: 'e' }; // OK - a6 = { - // OK - q: { x: 16, y: 'f' }, // NOT OK - w: { a: 17, b: 'g' }, // OK - ASSIGNMENT TO ANY - e: { a: 18, b: 'h' }, // OK - r: { a: 19, b: 'i' }, // OK - }; -} - -function defaultParamValue(): void { - function foo(x = { a: 21, b: 'a' }) { - console.log(x.a, x.b); - } // NOT OK - function foo2(x: any = { a: 22, b: 'b' }) { - console.log(x.a, x.b); - } // NOT OK - function foo3(x: { a: number; b: string } = { a: 23, b: 'c' }) { - console.log(x.a, x.b); - } // NOT OK - function foo4(x: I = { a: 24, b: 'd' }) { - console.log(x.a, x.b); - } // OK - function foo5(x: C = { a: 25, b: 'e' }) { - console.log(x.a, x.b); - } // OK - - // Function call - foo({ a: 21, b: 'a' }); // NOT OK - foo2({ a: 22, b: 'b' }); // OK - ASSIGNMENT TO ANY - foo3({ a: 23, b: 'c' }); // NOT OK - foo4({ a: 24, b: 'd' }); // OK - foo5({ a: 25, b: 'e' }); // OK -} - -function returnFromFunction(): void { - function bar() { - return { a: 31, b: 'a' }; - } // NOT OK - function bar2(): any { - return { a: 32, b: 'b' }; - } // OK - ASSIGNMENT TO ANY - function bar3(): { a: number; b: string } { - return { a: 33, b: 'c' }; - } // NOT OK - function bar4(): I { - return { a: 34, b: 'd' }; - } // OK - function bar5(): C { - return { a: 35, b: 'e' }; - } // OK -} - -function ternaryOperator(): void { - // In ternary operator - const condition = true; - const a1 = condition ? { a: 41, b: 'a' } : { a: 42, b: 'b' }; // NOT OK - const a2: any = condition ? { a: 43, b: 'c' } : { a: 44, b: 'd' }; // OK - ASSIGNMENT TO ANY - const a3: { a: number; b: string } = condition - ? { a: 45, b: 'e' } - : { a: 46, b: 'f' }; // NOT OK - const a4: I = condition ? { a: 47, b: 'g' } : { a: 48, b: 'h' }; // OK - const a5: C = condition ? { a: 49, b: 'i' } : { a: 50, b: 'j' }; // OK -} - -function arrayLiteral(): void { - const arr1 = [ - { a: 51, b: 'a' }, - { a: 52, b: 'b' }, - ]; // NOT OK - const arr2: any[] = [ - { a: 53, b: 'c' }, - { a: 54, b: 'd' }, - ]; // OK - ASSIGNMENT TO ANY - const arr3: { a: number; b: string }[] = [ - { a: 55, b: 'e' }, - { a: 56, b: 'f' }, - ]; // NOT OK - const arr4: I[] = [ - { a: 57, b: 'g' }, - { a: 58, b: 'h' }, - ]; // OK - const arr5: C[] = [ - { a: 59, b: 'i' }, - { a: 60, b: 'j' }, - ]; // OK -} - -enum E { - OK, - NO_OK, -} -interface I1 { - v: E | number -} - -interface I2 { - v: E -} - -let i1: I1 = {v:E.OK} -let i2: I2 = {v:E.NO_OK} - -function g1(a: E) { - let ii1: I1 = {v:a} - let ii2: I2 = {v:a} -} - -function g(): boolean { - return true; -} -interface CondI { - a: number; -} -let a1: CondI = { - a: g() ? 0 : 1, -}; -let b1: CondI = { - a: (g() ? 0 : 1) as number, -}; -let c1 = g() ? 0 : 1; -let d1: CondI = { - a: c1, -}; -let e1: CondI = { -a: 0|1|2|3 -} -let f1: 0|1|2|3 = 3 -let ff : CondI = { - a: f1 -} - -let dict = new Map(); -dict.set('1', 123) - -interface III { - param?: string | number | boolean -} - -let test1: III = { param: dict.get('1') } as III -let test2: III = { param: dict.get('1')! } as III -let test3: III = { param: dict.get('1') as number } as III -let test4: III = { param: dict.get('1') as (number | string) } as III -export interface Style { -} -export class SwitchMenuStyle implements Style { -} -export class ProfileOneLineSwitchMenuStyle extends SwitchMenuStyle { -} -export class ProfileSwitchMenuStyle extends SwitchMenuStyle { -} -export let hfpProfileSwitchMenuStyle = new ProfileSwitchMenuStyle(); -export let hfpProfileOneLineSwitchMenuStyle = new ProfileOneLineSwitchMenuStyle(); - -export interface SettingsBaseMenuData { - style?: Style; -} - -function test(isDisConnected:boolean){ - let a={style: isDisConnected ? hfpProfileOneLineSwitchMenuStyle: hfpProfileSwitchMenuStyle} as SettingsBaseMenuData -} - -interface PPP { - x: number - y: number | undefined - z?: number -} - -let p1: PPP = {x: 10, y: 10} -let p2: PPP = {x: 10, y: undefined} -let p3: PPP = {x: 10, y: undefined, z: undefined} -let p4: PPP = {x: 10, y: undefined, z: 10} -let p5: PPP = {x: 10, y: 10, z: 10} -const cp1: PPP = {x: 10, y: 10} -const cp2: PPP = {x: 10, y: undefined} -const cp3: PPP = {x: 10, y: undefined, z: undefined} -const cp4: PPP = {x: 10, y: undefined, z: 10} -const cp5: PPP = {x: 10, y: 10, z: 10} - -const oi: OhosI = { f: 1 }; - -ohFunction1({d: oi}) -ohFunction1({d: {f: 1}}) -ohFunction2({d: oi}) -ohFunction2({d: {f: 1}}) diff --git a/ets2panda/linter/test/main/object_literals_2.ets b/ets2panda/linter/test/main/object_literals_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..47e38a8109ce43f5239e5dd072b6b09782e19985 --- /dev/null +++ b/ets2panda/linter/test/main/object_literals_2.ets @@ -0,0 +1,285 @@ +/* + * Copyright (c) 2023-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. + */ + +let obj = {n: 42, s: 'foo'} // OK in TypeScript, CTE in ArkTS: unknown type of obj. + +class C { + n: number = 0; + s: string = ""; +} + +let c1: C = {n: 42, s: 'foo'} // Declaration + initialization: type of the literla is inferred from the type of c1. + +let c2: C; +c2 = {n: 42, s: 'foo'}; // Initialization after declaration: type of the literal is inferred from the type of c2. + +let c3: Object = {n: 42, s: 'foo'} as C as Object; // Type of the literal is inferred from the 'as' cast. +console.log(c3 instanceof C); // NB! Output is true in ArkTS, but is false in TS. + +function foo(c: C) { + console.log('foo is called'); +} + +foo({n: 42, s: 'foo'}); // Parsing as an argument: type of the literal is inferred from the type of parameter 'c' + +function bar(): C { + return {n: 42, s: 'foo'}; // Returning from function: type of the literal is inferred from the bar's return type. +} + +let cc: C[] = [{n: 1, s: '1'}, {n: 2, s: '2'}]; // Type of the literal is inferred from the type of the array. + +class D { + b: boolean = false; + c: C = {n: 0, s: ""}; +} + +let d: D = { + b: true, + c: { // Initialization of a field with a literal: type of the literal is inferred from the definition of class D. + n: 42, + s: 'foo' + } +} + +// Restrictions of classes that can be initialized with literal +// Default initializable class. +class C1 { + n: number = 0; + s?: string; +} + +let c4: C1 = {n: 42}; // OK in TS, OK in ArkTS, c.s is null + +class C2 { + s: string; + constructor(s: string) { + this.s = "s = " + s; + } +} + +let c5: C2 = {s: 'foo'} // OK in TS, CTE in ArkTS + +// All class fields are accessible at the point of initialization. +class C3 { + private n: number = 0; + public s: string = ''; +} + +// CTE in TypeScript, CTE in ArkTS // let c6: C3 = {n: 42, s: 'foo'}, + +class C4 { + readonly n: number = 0; + readonly s: string = ''; +} + +let c7: C4 = {n: 42, s: 'foo'}; // OK in TS, CTE in ArkTS + +// Class is non-abstract +abstract class A {} +let a: A = {}; // OK in TS, CTE in ArkTS + +// Class declares no methods, apart from optionally declared constructors and setters. +class C5 { + n: number = 0; + s: string = ''; + f() { + console.log('C5.f is called'); + } +} + +let c8: C5 = {n: 42, s: 'foo', f: () => {}} // OK in TS, CTE in ArkTS + +// NB! If a class has getters/setters the semantics of initialization differs: +class C6 { + n: number = 0; + _s: string = ''; + get s(): string { return this._s; } + set s(s: string) { this._s = s; } +} + +let c9: C6 = {n: 42, _s: 'foo', s: 'bar'} +console.log(c9.s); // TS: 'bar', ArkTS: 'bar' +console.log(c9._s); // TS: 'foo', ArkTS: 'bar' + +// Extra fields are not allowed (eventually it means that it's not possible to assign literals to Object / object): +class C7 { + n: number = 0; + s: string = ''; +} +// TS: CTE, ArtTS: CTE // let c10: C7 = {n: 42, s: '', extra: true}, +let o1: Object = {s: '', n: 42} // OK in TS, CTE in ArkTS: no fields 'n' and 's' in Object +let o2: object = {n: 42, s: ''} // OK in TS, CTE in ArkTS: no fields 'n' and 's' in object + +// If initialized class is inherited from another class, the base class must also be literal-initializable, +// and initialization should happen from the 'glattened' literal: +class Base { + n: number = 0; +} + +class Derived extends Base { + s: string = ''; +} + +let d2: Derived = {n: 42, s: ''}; + +// Interface should not declare methods, only properties are allowed. +interface I { + n: number; + s: string; + f(): void; +} + +let i: I = {n: 42, s: '', f: () => {console.log('I.f is called')}} // OK in TypeScript, CTE in ArkTS + +// Interface properties of reference types must be default-initializable: +interface I2 { + n: number; + s: string; // Assuming that 'string' is an alias for 'String', and there is String() constructor (what is true). +} + +let i2: I2 = {n: 42, s: ''}; + +interface CompilerOptions { + strict?: boolean; + sourcePath?: string; + targetPath?: string; +} + +const options: CompilerOptions = { // OK, as 'targetPath' field is optional + strict: true, + sourcePath: './src', +}; + +// Function parameter with union type. +function funcWithUnionParam(x: C | number): void { } +funcWithUnionParam({ n: 1, s: '2' }) // OK, union type is supported + +// issue 13022: property with union type +class UnionProperty { + a: number | string = 123; + b?: boolean | number; +} +let u: UnionProperty = { a: 1 }; // OK, union type is supported +u = { a: '2' }; // OK, union type is supported +u = { a: 3, b: true }; // OK, union type is supported + +// issue 13022: optional property +class OptionalProp { + a?: number; +} +let o: OptionalProp = {}; +o = {a: 1}; // OK + +class OptionalProp2 { + a?: number; + b: string; +} +function optProp(a: OptionalProp2) {} +optProp({b: ''}); // OK +optProp({a: 0, b: '1'}); // OK + +// Property with inheritance +class E1 { + x: number; + y: Base; +} +let e1 : E1 = { + x: 1, + y: new Derived() +} + +// Property with inheritance through generic type parameter +class E2 { + x: number; + y: T; +} +let e2 : E2 = { + x: 1, + y: new Derived() +} + +// Type alias chain to interface +interface ITypeAlias { a: number; b: T } +type ITA = ITypeAlias; +type ITA2 = ITA; +let ti: ITA2 = { // OK, 'ITA2' is an alias to interface 'ITypeAlias' + a: 12, + b: '34' +} + +// Type alias chain to class +class CTypeAlias { + a: number; + b: T; +} +type CTA = CTypeAlias; +type CTA2 = CTA; +let tc: CTA2 = { // OK, 'CTA' is an alias to class 'CTypeAlias' + a: 4, + b: '4' +} + +// issue 13114: Const enum value converted to string/number type. +const enum ATTRIBUTE { + ROW = 'Row', + COLUMN = 'Column', + COLUMN_REVERSE = 'ColumnReverse', +}; +const enum GROUP { + MAIN_DIRECTION = 'MAIN_DIRECTION', +}; +enum Orientation { + Horizontal, + Vertical +} +class ContainerModuleItem { + groupName: string = ''; + attributeList: string[] = []; + attribute: ATTRIBUTE = ATTRIBUTE.COLUMN; + orientation: number = 0; +} +const FLEX_MODULE: ContainerModuleItem[] = [ + { + groupName: GROUP.MAIN_DIRECTION, + attributeList: [ATTRIBUTE.COLUMN, ATTRIBUTE.ROW, ATTRIBUTE.COLUMN_REVERSE], + attribute: ATTRIBUTE.ROW, + orientation: Orientation.Horizontal + } +]; + +interface I3 {} + +class CCl implements I3 {} + +class CCl2 extends CCl implements I3 {} + +interface I4 { + a: I3; + b: I3; + c: CCl; + d: CCl2; +} + +class DCl { + constructor(a: I4) {} +} + +let c: I4 = {a: new CCl(), b: new CCl2(), c: new CCl2(), d: new CCl2()} + +new DCl({a: new CCl(), b: new CCl2(), c: new CCl2(), d: new CCl2()}) + +let oo1: Object = {} + +let oo2: Object = {a: 12} diff --git a/ets2panda/linter/test/main/object_literals_2.sts b/ets2panda/linter/test/main/object_literals_2.sts deleted file mode 100644 index d99df1124c6c1a912c32203f161915b054a0bcfb..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_literals_2.sts +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -let obj = {n: 42, s: 'foo'} // OK in TypeScript, CTE in ArkTS: unknown type of obj. - -class C { - n: number = 0; - s: string = ""; -} - -let c1: C = {n: 42, s: 'foo'} // Declaration + initialization: type of the literla is inferred from the type of c1. - -let c2: C; -c2 = {n: 42, s: 'foo'}; // Initialization after declaration: type of the literal is inferred from the type of c2. - -let c3: Object = {n: 42, s: 'foo'} as C as Object; // Type of the literal is inferred from the 'as' cast. -console.log(c3 instanceof C); // NB! Output is true in ArkTS, but is false in TS. - -function foo(c: C) { - console.log('foo is called'); -} - -foo({n: 42, s: 'foo'}); // Parsing as an argument: type of the literal is inferred from the type of parameter 'c' - -function bar(): C { - return {n: 42, s: 'foo'}; // Returning from function: type of the literal is inferred from the bar's return type. -} - -let cc: C[] = [{n: 1, s: '1'}, {n: 2, s: '2'}]; // Type of the literal is inferred from the type of the array. - -class D { - b: boolean = false; - c: C = {n: 0, s: ""}; -} - -let d: D = { - b: true, - c: { // Initialization of a field with a literal: type of the literal is inferred from the definition of class D. - n: 42, - s: 'foo' - } -} - -// Restrictions of classes that can be initialized with literal -// Default initializable class. -class C1 { - n: number = 0; - s?: string; -} - -let c4: C1 = {n: 42}; // OK in TS, OK in ArkTS, c.s is null - -class C2 { - s: string; - constructor(s: string) { - this.s = "s = " + s; - } -} - -let c5: C2 = {s: 'foo'} // OK in TS, CTE in ArkTS - -// All class fields are accessible at the point of initialization. -class C3 { - private n: number = 0; - public s: string = ''; -} - -// CTE in TypeScript, CTE in ArkTS // let c6: C3 = {n: 42, s: 'foo'}, - -class C4 { - readonly n: number = 0; - readonly s: string = ''; -} - -let c7: C4 = {n: 42, s: 'foo'}; // OK in TS, CTE in ArkTS - -// Class is non-abstract -abstract class A {} -let a: A = {}; // OK in TS, CTE in ArkTS - -// Class declares no methods, apart from optionally declared constructors and setters. -class C5 { - n: number = 0; - s: string = ''; - f() { - console.log('C5.f is called'); - } -} - -let c8: C5 = {n: 42, s: 'foo', f: () => {}} // OK in TS, CTE in ArkTS - -// NB! If a class has getters/setters the semantics of initialization differs: -class C6 { - n: number = 0; - _s: string = ''; - get s(): string { return this._s; } - set s(s: string) { this._s = s; } -} - -let c9: C6 = {n: 42, _s: 'foo', s: 'bar'} -console.log(c9.s); // TS: 'bar', ArkTS: 'bar' -console.log(c9._s); // TS: 'foo', ArkTS: 'bar' - -// Extra fields are not allowed (eventually it means that it's not possible to assign literals to Object / object): -class C7 { - n: number = 0; - s: string = ''; -} -// TS: CTE, ArtTS: CTE // let c10: C7 = {n: 42, s: '', extra: true}, -let o1: Object = {s: '', n: 42} // OK in TS, CTE in ArkTS: no fields 'n' and 's' in Object -let o2: object = {n: 42, s: ''} // OK in TS, CTE in ArkTS: no fields 'n' and 's' in object - -// If initialized class is inherited from another class, the base class must also be literal-initializable, -// and initialization should happen from the 'glattened' literal: -class Base { - n: number = 0; -} - -class Derived extends Base { - s: string = ''; -} - -let d2: Derived = {n: 42, s: ''}; - -// Interface should not declare methods, only properties are allowed. -interface I { - n: number; - s: string; - f(): void; -} - -let i: I = {n: 42, s: '', f: () => {console.log('I.f is called')}} // OK in TypeScript, CTE in ArkTS - -// Interface properties of reference types must be default-initializable: -interface I2 { - n: number; - s: string; // Assuming that 'string' is an alias for 'String', and there is String() constructor (what is true). -} - -let i2: I2 = {n: 42, s: ''}; - -interface CompilerOptions { - strict?: boolean; - sourcePath?: string; - targetPath?: string; -} - -const options: CompilerOptions = { // OK, as 'targetPath' field is optional - strict: true, - sourcePath: './src', -}; - -// Function parameter with union type. -function funcWithUnionParam(x: C | number): void { } -funcWithUnionParam({ n: 1, s: '2' }) // OK, union type is supported - -// issue 13022: property with union type -class UnionProperty { - a: number | string = 123; - b?: boolean | number; -} -let u: UnionProperty = { a: 1 }; // OK, union type is supported -u = { a: '2' }; // OK, union type is supported -u = { a: 3, b: true }; // OK, union type is supported - -// issue 13022: optional property -class OptionalProp { - a?: number; -} -let o: OptionalProp = {}; -o = {a: 1}; // OK - -class OptionalProp2 { - a?: number; - b: string; -} -function optProp(a: OptionalProp2) {} -optProp({b: ''}); // OK -optProp({a: 0, b: '1'}); // OK - -// Property with inheritance -class E1 { - x: number; - y: Base; -} -let e1 : E1 = { - x: 1, - y: new Derived() -} - -// Property with inheritance through generic type parameter -class E2 { - x: number; - y: T; -} -let e2 : E2 = { - x: 1, - y: new Derived() -} - -// Type alias chain to interface -interface ITypeAlias { a: number; b: T } -type ITA = ITypeAlias; -type ITA2 = ITA; -let ti: ITA2 = { // OK, 'ITA2' is an alias to interface 'ITypeAlias' - a: 12, - b: '34' -} - -// Type alias chain to class -class CTypeAlias { - a: number; - b: T; -} -type CTA = CTypeAlias; -type CTA2 = CTA; -let tc: CTA2 = { // OK, 'CTA' is an alias to class 'CTypeAlias' - a: 4, - b: '4' -} - -// issue 13114: Const enum value converted to string/number type. -const enum ATTRIBUTE { - ROW = 'Row', - COLUMN = 'Column', - COLUMN_REVERSE = 'ColumnReverse', -}; -const enum GROUP { - MAIN_DIRECTION = 'MAIN_DIRECTION', -}; -enum Orientation { - Horizontal, - Vertical -} -class ContainerModuleItem { - groupName: string = ''; - attributeList: string[] = []; - attribute: ATTRIBUTE = ATTRIBUTE.COLUMN; - orientation: number = 0; -} -const FLEX_MODULE: ContainerModuleItem[] = [ - { - groupName: GROUP.MAIN_DIRECTION, - attributeList: [ATTRIBUTE.COLUMN, ATTRIBUTE.ROW, ATTRIBUTE.COLUMN_REVERSE], - attribute: ATTRIBUTE.ROW, - orientation: Orientation.Horizontal - } -]; - -interface I3 {} - -class CCl implements I3 {} - -class CCl2 extends CCl implements I3 {} - -interface I4 { - a: I3; - b: I3; - c: CCl; - d: CCl2; -} - -class DCl { - constructor(a: I4) {} -} - -let c: I4 = {a: new CCl(), b: new CCl2(), c: new CCl2(), d: new CCl2()} - -new DCl({a: new CCl(), b: new CCl2(), c: new CCl2(), d: new CCl2()}) - -let oo1: Object = {} - -let oo2: Object = {a: 12} diff --git a/ets2panda/linter/test/main/object_literals_4.ets b/ets2panda/linter/test/main/object_literals_4.ets new file mode 100644 index 0000000000000000000000000000000000000000..19e21b00c1b27f394376f2093fa5778f975e8b37 --- /dev/null +++ b/ets2panda/linter/test/main/object_literals_4.ets @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023-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. + */ + +type T = () => void + +class A { + foo?: T + bar?: Function +} + +let a: A = { + foo: () => { + console.log("Hello") + }, + bar: () => { + console.log("Hello") + } +} + +//====================== +class Resource{} + +class ContainerModuleItem { + groupName: string = ""; + moduleName: Resource | null = null; + atributeList: string[] = []; +} + +const FLEX_MODULE: ContainerModuleItem[] = [{ + groupName: GROUP.MAIN_DIRECTION, + moduleName: "ASF", + atributeList: [ATTRIBUTE.COLUMN, ATTRIBUTE.ROW, ATTRIBUTE.COLUMN_REVERSE], +}] + +const enum ATTRIBUTE { + ROW = 'Row', + COLUMN = 'Column', + COLUMN_REVERSE = 'ColumnReverse' +} + +const enum GROUP { + MAIN_DIRECTION = 'MAIN_DIRECTION' +} + +//==================================== + +class C { + s: string = "" + n: number = 0 + l: () => void = () => {} +} + +let c : C = { + s: "foo", + n: 42, + l: () => { + console.log("Hi") + } +} + +c.l() \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_4.sts b/ets2panda/linter/test/main/object_literals_4.sts deleted file mode 100644 index 10617e1cb244e6c078e3d48b020ecffe4f6543ca..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_literals_4.sts +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -type T = () => void - -class A { - foo?: T - bar?: Function -} - -let a: A = { - foo: () => { - console.log("Hello") - }, - bar: () => { - console.log("Hello") - } -} - -//====================== -class Resource{} - -class ContainerModuleItem { - groupName: string = ""; - moduleName: Resource | null = null; - atributeList: string[] = []; -} - -const FLEX_MODULE: ContainerModuleItem[] = [{ - groupName: GROUP.MAIN_DIRECTION, - moduleName: "ASF", - atributeList: [ATTRIBUTE.COLUMN, ATTRIBUTE.ROW, ATTRIBUTE.COLUMN_REVERSE], -}] - -const enum ATTRIBUTE { - ROW = 'Row', - COLUMN = 'Column', - COLUMN_REVERSE = 'ColumnReverse' -} - -const enum GROUP { - MAIN_DIRECTION = 'MAIN_DIRECTION' -} - -//==================================== - -class C { - s: string = "" - n: number = 0 - l: () => void = () => {} -} - -let c : C = { - s: "foo", - n: 42, - l: () => { - console.log("Hi") - } -} - -c.l() \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_autofixes.ets b/ets2panda/linter/test/main/object_literals_autofixes.ets new file mode 100644 index 0000000000000000000000000000000000000000..87814c7da5d5feafdf064a64253db60213519298 --- /dev/null +++ b/ets2panda/linter/test/main/object_literals_autofixes.ets @@ -0,0 +1,175 @@ +/* + * 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. + */ + +// Following declarations are added to check that we don't generate new interfaces with names that already exist +import GeneratedObjectLiteralInterface_4 from 'x'; +import * as GeneratedObjectLiteralInterface_7 from 'x'; +import { + GeneratedObjectLiteralInterface_8, + foo as GeneratedObjectLiteralInterface_9 +} from 'x'; +interface GeneratedObjectLiteralInterface_11 {} +class GeneratedObjectLiteralInterface_12 {} +function GeneratedObjectLiteralInterface_15() {} + +class C {} + +function foo(x): number { + return 1; +} + +const o1 = {}; +const o2 = { hello: "world" }; +const o3! = {a: 1, b: 2}; +const o4 = { + field: "hello", + field1: 2, + field2: "world", + field3: 4, + field4: true, + field5: false +}; + +// Properties with various types. Nested object literals +const o5 = { + a: 1, + b: '2', + c: true, + d: new C(), + e: {}, + f: { a: 1, b: '2' }, + g: { + q: 10, + w: 20 + }, +}; +const o6 = { + a: 1, + b: '2', + c: { q: 10, w: 20 }, + d: true, + e: { q: 30, w: 40 } +}; + +// Object literals inside another expression +const o7 = { a:1, b:2 }.a + { a:3, b:4 }.b; +const o8 = { + a: 1, + b: 2, + c: ({x:1, y:2}), + d: foo({q:1, w:2}.q + {q:3, w:4}.w) +}; + +// Object literals inside class declaration +class D { + f1 = {a: 1, b: 2}; + f2? = {a: 1, b: 2}; + f3! = {a: 1, b: 2}; + f4 = ({c: 3, d: 4}); + f5 = {e: 5}.e + {f: 6, g: 7}.f; + + m() { + let x = {a:1, b:2}; + let y = {c:1, d:2, e:3}; + } +} + +// Object literals as function parameter initializer +function funInit(p = { a: 1, b: 2 }) {} +function funInit2({a, b} = { a: 3, b: 4 }) {} // Not fixable, as in case of destructuring parameters, the contextual type of expression is implied by the binding pattern + +// Object literals inside function declaration +function bar(): void { + let a = {a: 1, b: 2}; + let b = {c: 3, d: 4}; + + if (a.b > b.c) { + let c = {e: 5, f: 6}; + } + + let d = {g: 7, d: foo({q:1,w:2}.q + {q:3,w:4}.w)}; +} + +const o9 = { 1: '1', '2': 2 }; +const o10 = { [3]: 3 }; // Not fixable, computed property value +const o11 = { [o2.hello]: 'world' }; // Not fixable, computed property value + +const anyVal: any = 1; +const o12 = { a: anyVal }; // Not fixable, type of property 'a' is not supported + +let val = 1; +const o13 = { val }; // Not fixable, property is not 'key:value' pair +const o14 = { ...o1 }; // Not fixable, property is not 'key:value' pair +const o15 = { m() {} }; // Not fixable, property is not 'key:value' pair + +const o16 = { // Not fixable, property 'c' is initialized with non-fixable nested object literal, and thus will always have unsupported type (object type literal) + a: 1, + b: '2', + c: { + q: 1, + w: 2, + e: anyVal // Not fixable, unsupported type + } +} + +class X {} +class Y> {} +function captureFromLocalScope(t: T): void { + let v1 = {a: 1, b: '2', c: t}; // Not fixable, `c` references local type parameter `T` + let v2 = {a: 1, b: '2', c: new X()}; // Not fixable, `c` references local type parameter `T` + let v3 = {a: 1, b: '2', c: new Y>()}; // Not fixable, `c` references local type parameter `T` + + type LocalType = {a: number, b: string}; + let localTypeVar: LocalType = {a:1, b:'2'}; + let v4 = { x: localTypeVar }; // Non-fixable, `x` references type `LocalType` declared in local scope + + class LocalClass {x: number = 1}; + let v5 = { y: new LocalClass() }; // Non-fixable, `y` references type `LocalClass` declared in local scope + + let v6 = { z: LocalClass }; // Non-fixable, `z` references type `LocalClass` declared in local scope +} + +// Record object literals +let rec1: Record = { + a: 1, + b: 2, + c: 3 +} + +let rec2: Record = { + foo: 1, + bar: 2, + 10: 'foo', + 20: 'bar', + baz: 3, + 'daz': 4 +} + +let rec3: Record = { // Not fixable + f1: 1, + f2: 2, + f3: 3, + [val]: 4 // Not fixable, key is a computed value +} + +interface NullableRecord { + params?: Record; +} +let rec4: NullableRecord = { + params: { + key: '1', + message: '2' + } +}; \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_autofixes.sts b/ets2panda/linter/test/main/object_literals_autofixes.sts deleted file mode 100644 index ad9ac02ab95163f71f8279052f574177586f46e9..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_literals_autofixes.sts +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Following declarations are added to check that we don't generate new interfaces with names that already exist -import GeneratedObjectLiteralInterface_4 from 'x'; -import * as GeneratedObjectLiteralInterface_7 from 'x'; -import { - GeneratedObjectLiteralInterface_8, - foo as GeneratedObjectLiteralInterface_9 -} from 'x'; -interface GeneratedObjectLiteralInterface_11 {} -class GeneratedObjectLiteralInterface_12 {} -function GeneratedObjectLiteralInterface_15() {} - -class C {} - -function foo(x): number { - return 1; -} - -const o1 = {}; -const o2 = { hello: "world" }; -const o3! = {a: 1, b: 2}; -const o4 = { - field: "hello", - field1: 2, - field2: "world", - field3: 4, - field4: true, - field5: false -}; - -// Properties with various types. Nested object literals -const o5 = { - a: 1, - b: '2', - c: true, - d: new C(), - e: {}, - f: { a: 1, b: '2' }, - g: { - q: 10, - w: 20 - }, -}; -const o6 = { - a: 1, - b: '2', - c: { q: 10, w: 20 }, - d: true, - e: { q: 30, w: 40 } -}; - -// Object literals inside another expression -const o7 = { a:1, b:2 }.a + { a:3, b:4 }.b; -const o8 = { - a: 1, - b: 2, - c: ({x:1, y:2}), - d: foo({q:1, w:2}.q + {q:3, w:4}.w) -}; - -// Object literals inside class declaration -class D { - f1 = {a: 1, b: 2}; - f2? = {a: 1, b: 2}; - f3! = {a: 1, b: 2}; - f4 = ({c: 3, d: 4}); - f5 = {e: 5}.e + {f: 6, g: 7}.f; - - m() { - let x = {a:1, b:2}; - let y = {c:1, d:2, e:3}; - } -} - -// Object literals as function parameter initializer -function funInit(p = { a: 1, b: 2 }) {} -function funInit2({a, b} = { a: 3, b: 4 }) {} // Not fixable, as in case of destructuring parameters, the contextual type of expression is implied by the binding pattern - -// Object literals inside function declaration -function bar(): void { - let a = {a: 1, b: 2}; - let b = {c: 3, d: 4}; - - if (a.b > b.c) { - let c = {e: 5, f: 6}; - } - - let d = {g: 7, d: foo({q:1,w:2}.q + {q:3,w:4}.w)}; -} - -const o9 = { 1: '1', '2': 2 }; -const o10 = { [3]: 3 }; // Not fixable, computed property value -const o11 = { [o2.hello]: 'world' }; // Not fixable, computed property value - -const anyVal: any = 1; -const o12 = { a: anyVal }; // Not fixable, type of property 'a' is not supported - -let val = 1; -const o13 = { val }; // Not fixable, property is not 'key:value' pair -const o14 = { ...o1 }; // Not fixable, property is not 'key:value' pair -const o15 = { m() {} }; // Not fixable, property is not 'key:value' pair - -const o16 = { // Not fixable, property 'c' is initialized with non-fixable nested object literal, and thus will always have unsupported type (object type literal) - a: 1, - b: '2', - c: { - q: 1, - w: 2, - e: anyVal // Not fixable, unsupported type - } -} - -class X {} -class Y> {} -function captureFromLocalScope(t: T): void { - let v1 = {a: 1, b: '2', c: t}; // Not fixable, `c` references local type parameter `T` - let v2 = {a: 1, b: '2', c: new X()}; // Not fixable, `c` references local type parameter `T` - let v3 = {a: 1, b: '2', c: new Y>()}; // Not fixable, `c` references local type parameter `T` - - type LocalType = {a: number, b: string}; - let localTypeVar: LocalType = {a:1, b:'2'}; - let v4 = { x: localTypeVar }; // Non-fixable, `x` references type `LocalType` declared in local scope - - class LocalClass {x: number = 1}; - let v5 = { y: new LocalClass() }; // Non-fixable, `y` references type `LocalClass` declared in local scope - - let v6 = { z: LocalClass }; // Non-fixable, `z` references type `LocalClass` declared in local scope -} - -// Record object literals -let rec1: Record = { - a: 1, - b: 2, - c: 3 -} - -let rec2: Record = { - foo: 1, - bar: 2, - 10: 'foo', - 20: 'bar', - baz: 3, - 'daz': 4 -} - -let rec3: Record = { // Not fixable - f1: 1, - f2: 2, - f3: 3, - [val]: 4 // Not fixable, key is a computed value -} - -interface NullableRecord { - params?: Record; -} -let rec4: NullableRecord = { - params: { - key: '1', - message: '2' - } -}; \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_iface.ets b/ets2panda/linter/test/main/object_literals_iface.ets new file mode 100644 index 0000000000000000000000000000000000000000..eea378b6a8443f69816d98b0d99432be915e2c6f --- /dev/null +++ b/ets2panda/linter/test/main/object_literals_iface.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023-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. + */ + +import { Callback1 } from "./iface1" +import { Callback2 } from "./iface2" + +function main(): void { + let callback1: Callback1 = { + foo: (p: number): void => {} + } + + let callback2: Callback2 = { + bar: (p: number): void => {} + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_iface.sts b/ets2panda/linter/test/main/object_literals_iface.sts deleted file mode 100644 index c1b10f68a52947979182061587021f47b5e55988..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_literals_iface.sts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Callback1 } from "./iface1" -import { Callback2 } from "./iface2" - -function main(): void { - let callback1: Callback1 = { - foo: (p: number): void => {} - } - - let callback2: Callback2 = { - bar: (p: number): void => {} - } -} \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_prop_func_type.ets b/ets2panda/linter/test/main/object_literals_prop_func_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..d1901e50e01e4fb1463cdbfacc8798aa0f672bb4 --- /dev/null +++ b/ets2panda/linter/test/main/object_literals_prop_func_type.ets @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023-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. + */ + +type F = () => void; +interface FuncInterface { + (x: number): string + f?: number; +} + +class A { + a: F; + b: Function; + c: () => void; + d: FuncInterface; +} + +let a: A = { + a: () => { console.log('Hello'); }, + b: () => { console.log('Bye'); }, + c: () => { console.log('Apple'); }, + d: (x: number) => x.toString(), +} + +let q: F = () => {}; +let w: Function = (x: number) => x * x; +let e = () => { console.log('Orange'); }; +let r: FuncInterface = (x: number) => x.toString(); + +a = { + a: q, + b: w, + c: e, + d: r, +} + +// #14569 - initialize field with 'Function' object +class B { + f: Function = () => {}; +} +let b: B = { + f: Function +}; \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_prop_func_type.sts b/ets2panda/linter/test/main/object_literals_prop_func_type.sts deleted file mode 100644 index 1918e957c9accaa848766e4a6cd4be78aec91e03..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_literals_prop_func_type.sts +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -type F = () => void; -interface FuncInterface { - (x: number): string - f?: number; -} - -class A { - a: F; - b: Function; - c: () => void; - d: FuncInterface; -} - -let a: A = { - a: () => { console.log('Hello'); }, - b: () => { console.log('Bye'); }, - c: () => { console.log('Apple'); }, - d: (x: number) => x.toString(), -} - -let q: F = () => {}; -let w: Function = (x: number) => x * x; -let e = () => { console.log('Orange'); }; -let r: FuncInterface = (x: number) => x.toString(); - -a = { - a: q, - b: w, - c: e, - d: r, -} - -// #14569 - initialize field with 'Function' object -class B { - f: Function = () => {}; -} -let b: B = { - f: Function -}; \ No newline at end of file diff --git a/ets2panda/linter/test/main/object_literals_properties.ets b/ets2panda/linter/test/main/object_literals_properties.ets new file mode 100644 index 0000000000000000000000000000000000000000..e6956798332d731b6f0b637b84e97f85b8eb769a --- /dev/null +++ b/ets2panda/linter/test/main/object_literals_properties.ets @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023-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. + */ + +class MyClass { + a: string = "" + b: number = 0 +} + +let c: MyClass = {a: "a", b: 3} + +let b: MyClass = { + a: "Alice", + "c", // Error + c, // Error + b: "num", + method() { // Error + console.log(42) + }, +} + +let o: MyClass = { + a: "foo", + b: 42, + c: {}, + 1: "number literal property", // Error + "foo:bar": "string literal property", // Error + + get property() {}, // Error + set property(value) {}, // Error + + [expression]: "computed property", // Error +}; diff --git a/ets2panda/linter/test/main/object_literals_properties.sts b/ets2panda/linter/test/main/object_literals_properties.sts deleted file mode 100644 index 813369ff28e73a1a89e5b8164d94556567c1ebf2..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_literals_properties.sts +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class MyClass { - a: string = "" - b: number = 0 -} - -let c: MyClass = {a: "a", b: 3} - -let b: MyClass = { - a: "Alice", - "c", // Error - c, // Error - b: "num", - method() { // Error - console.log(42) - }, -} - -let o: MyClass = { - a: "foo", - b: 42, - c: {}, - 1: "number literal property", // Error - "foo:bar": "string literal property", // Error - - get property() {}, // Error - set property(value) {}, // Error - - [expression]: "computed property", // Error -}; diff --git a/ets2panda/linter/test/main/object_spread.ets b/ets2panda/linter/test/main/object_spread.ets new file mode 100644 index 0000000000000000000000000000000000000000..066613a671f27c51ac7507e14e1b7955d5bf25b6 --- /dev/null +++ b/ets2panda/linter/test/main/object_spread.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023-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. + */ + +const pt2D = { x: 1, y: 2 }; + +console.log(pt2D); + +const pt3D = { ...pt2D, z: 3 }; + +console.log(pt3D); diff --git a/ets2panda/linter/test/main/object_spread.sts b/ets2panda/linter/test/main/object_spread.sts deleted file mode 100644 index 35fe11e8abe2b44322c842c6ff0d74a5b9836cdb..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/object_spread.sts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const pt2D = { x: 1, y: 2 }; - -console.log(pt2D); - -const pt3D = { ...pt2D, z: 3 }; - -console.log(pt3D); diff --git a/ets2panda/linter/test/main/optional_library_types.ets b/ets2panda/linter/test/main/optional_library_types.ets new file mode 100644 index 0000000000000000000000000000000000000000..017d05531e658d0064ae252ce3ddaf6fa851c3d0 --- /dev/null +++ b/ets2panda/linter/test/main/optional_library_types.ets @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2023-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. + */ + +import { COh, Cb1Oh, Cb2Oh, F1Oh, F2Oh } from "./oh_modules/oh_import"; +import { CGood, Cb1Good, Cb2Good, F1Good, F2Good } from "./ignore_files/good"; +import { + CNode, + Cb1Node, + Cb2Node, + F1Node, + F2Node, +} from "./node_modules/node_import"; + +class CLoc {} + +export type Cb1Loc = (a: number, f?: string) => number; +export type Cb2Loc = (a: any, f?: string) => number; + +export function F1Loc(cb?: Cb1Loc) {} +export function F2Loc(cb?: Cb2Loc) {} + +// ==================== +// #13760.1 +let cb1: Cb1Oh = (arg) => arg; +let cb2: Cb2Oh = (arg) => arg; + +let cb3: Cb1Good = (arg) => arg; +let cb4: Cb2Good = (arg) => arg; + +let cb5: Cb1Node = (arg) => arg; +let cb6: Cb2Node = (arg) => arg; + +let cb7: Cb1Loc = (arg) => arg; +let cb8: Cb2Loc = (arg) => arg; + +F1Oh(cb1); +F1Oh((arg) => arg); +F2Oh(cb2); +F2Oh((arg) => arg); +F2Oh((arg: number) => arg); + +F1Good(cb3); +F1Good((arg) => arg); +F2Good(cb4); +F2Good((arg) => arg); +F2Good((arg: number) => arg); + +F1Node(cb5); +F1Node((arg) => arg); +F2Node(cb6); +F2Node((arg) => arg); +F2Node((arg: number) => arg); + +F1Loc(cb7); +F1Loc((arg) => arg); +F2Loc(cb8); +F2Loc((arg) => arg); +F2Loc((arg: number) => arg); +// #13760.1 +// ==================== + +// ==================== +// #13760.2 +export interface ILoc { + fld1: number; + fld2?: COh; + fld3?: CGood; + fld4?: CNode; + fld5?: CLoc; +} + +function f1(a: ILoc) { + let v1 = a.fld1; + let v2 = a.fld2; + let v3 = a.fld3; + let v4 = a.fld4; + let v5 = a.fld5; +} +// #13760.2 +// ==================== + +// additional tests + +function fg1l1(cb: Cb1Good | Cb1Loc) {} +function fg1l2(cb: Cb1Good | Cb2Loc) {} +function fg2l1(cb: Cb2Good | Cb1Loc) {} +function fg2l2(cb: Cb2Good | Cb2Loc) {} + +fg1l1((arg) => arg); +fg1l2((arg) => arg); +fg2l1((arg) => arg); +fg2l2((arg) => arg); +fg1l1((arg: number) => arg); +fg1l2((arg: number) => arg); +fg2l1((arg: number) => arg); +fg2l2((arg: number) => arg); + +function fo1l1(cb: Cb1Oh | Cb1Loc) {} +function fo1l2(cb: Cb1Oh | Cb2Loc) {} +function fo2l1(cb: Cb2Oh | Cb1Loc) {} +function fo2l2(cb: Cb2Oh | Cb2Loc) {} + +fo1l1((arg) => arg); +fo1l2((arg) => arg); +fo2l1((arg) => arg); +fo2l2((arg) => arg); +fo1l1((arg: number) => arg); +fo1l2((arg: number) => arg); +fo2l1((arg: number) => arg); +fo2l2((arg: number) => arg); + +function fo1n1(cb: Cb1Oh | Cb1Node) {} +function fo1n2(cb: Cb1Oh | Cb2Node) {} +function fo2n1(cb: Cb2Oh | Cb1Node) {} +function fo2n2(cb: Cb2Oh | Cb2Node) {} + +fo1n1((arg) => arg); +fo1n2((arg) => arg); +fo2n1((arg) => arg); +fo2n2((arg) => arg); +fo1n1((arg: number) => arg); +fo1n2((arg: number) => arg); +fo2n1((arg: number) => arg); +fo2n2((arg: number) => arg); + +function fg1l1opt(cb?: Cb1Good | Cb1Loc) {} +function fg1l2opt(cb?: Cb1Good | Cb2Loc) {} +function fg2l1opt(cb?: Cb2Good | Cb1Loc) {} +function fg2l2opt(cb?: Cb2Good | Cb2Loc) {} + +fg1l1opt((arg) => arg); +fg1l2opt((arg) => arg); +fg2l1opt((arg) => arg); +fg2l2opt((arg) => arg); +fg1l1opt((arg: number) => arg); +fg1l2opt((arg: number) => arg); +fg2l1opt((arg: number) => arg); +fg2l2opt((arg: number) => arg); + +function fo1l1opt(cb?: Cb1Oh | Cb1Loc) {} +function fo1l2opt(cb?: Cb1Oh | Cb2Loc) {} +function fo2l1opt(cb?: Cb2Oh | Cb1Loc) {} +function fo2l2opt(cb?: Cb2Oh | Cb2Loc) {} + +fo1l1opt((arg) => arg); +fo1l2opt((arg) => arg); +fo2l1opt((arg) => arg); +fo2l2opt((arg) => arg); +fo1l1opt((arg: number) => arg); +fo1l2opt((arg: number) => arg); +fo2l1opt((arg: number) => arg); +fo2l2opt((arg: number) => arg); + +function fo1n1opt(cb?: Cb1Oh | Cb1Node) {} +function fo1n2opt(cb?: Cb1Oh | Cb2Node) {} +function fo2n1opt(cb?: Cb2Oh | Cb1Node) {} +function fo2n2opt(cb?: Cb2Oh | Cb2Node) {} + +fo1n1opt((arg) => arg); +fo1n2opt((arg) => arg); +fo2n1opt((arg) => arg); +fo2n2opt((arg) => arg); +fo1n1opt((arg: number) => arg); +fo1n2opt((arg: number) => arg); +fo2n1opt((arg: number) => arg); +fo2n2opt((arg: number) => arg); diff --git a/ets2panda/linter/test/main/optional_library_types.sts b/ets2panda/linter/test/main/optional_library_types.sts deleted file mode 100644 index 5884cd823e79e2fbdcb6c111e3a779658a47fe0b..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/optional_library_types.sts +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { COh, Cb1Oh, Cb2Oh, F1Oh, F2Oh } from "./oh_modules/oh_import"; -import { CGood, Cb1Good, Cb2Good, F1Good, F2Good } from "./ignore_files/good"; -import { - CNode, - Cb1Node, - Cb2Node, - F1Node, - F2Node, -} from "./node_modules/node_import"; - -class CLoc {} - -export type Cb1Loc = (a: number, f?: string) => number; -export type Cb2Loc = (a: any, f?: string) => number; - -export function F1Loc(cb?: Cb1Loc) {} -export function F2Loc(cb?: Cb2Loc) {} - -// ==================== -// #13760.1 -let cb1: Cb1Oh = (arg) => arg; -let cb2: Cb2Oh = (arg) => arg; - -let cb3: Cb1Good = (arg) => arg; -let cb4: Cb2Good = (arg) => arg; - -let cb5: Cb1Node = (arg) => arg; -let cb6: Cb2Node = (arg) => arg; - -let cb7: Cb1Loc = (arg) => arg; -let cb8: Cb2Loc = (arg) => arg; - -F1Oh(cb1); -F1Oh((arg) => arg); -F2Oh(cb2); -F2Oh((arg) => arg); -F2Oh((arg: number) => arg); - -F1Good(cb3); -F1Good((arg) => arg); -F2Good(cb4); -F2Good((arg) => arg); -F2Good((arg: number) => arg); - -F1Node(cb5); -F1Node((arg) => arg); -F2Node(cb6); -F2Node((arg) => arg); -F2Node((arg: number) => arg); - -F1Loc(cb7); -F1Loc((arg) => arg); -F2Loc(cb8); -F2Loc((arg) => arg); -F2Loc((arg: number) => arg); -// #13760.1 -// ==================== - -// ==================== -// #13760.2 -export interface ILoc { - fld1: number; - fld2?: COh; - fld3?: CGood; - fld4?: CNode; - fld5?: CLoc; -} - -function f1(a: ILoc) { - let v1 = a.fld1; - let v2 = a.fld2; - let v3 = a.fld3; - let v4 = a.fld4; - let v5 = a.fld5; -} -// #13760.2 -// ==================== - -// additional tests - -function fg1l1(cb: Cb1Good | Cb1Loc) {} -function fg1l2(cb: Cb1Good | Cb2Loc) {} -function fg2l1(cb: Cb2Good | Cb1Loc) {} -function fg2l2(cb: Cb2Good | Cb2Loc) {} - -fg1l1((arg) => arg); -fg1l2((arg) => arg); -fg2l1((arg) => arg); -fg2l2((arg) => arg); -fg1l1((arg: number) => arg); -fg1l2((arg: number) => arg); -fg2l1((arg: number) => arg); -fg2l2((arg: number) => arg); - -function fo1l1(cb: Cb1Oh | Cb1Loc) {} -function fo1l2(cb: Cb1Oh | Cb2Loc) {} -function fo2l1(cb: Cb2Oh | Cb1Loc) {} -function fo2l2(cb: Cb2Oh | Cb2Loc) {} - -fo1l1((arg) => arg); -fo1l2((arg) => arg); -fo2l1((arg) => arg); -fo2l2((arg) => arg); -fo1l1((arg: number) => arg); -fo1l2((arg: number) => arg); -fo2l1((arg: number) => arg); -fo2l2((arg: number) => arg); - -function fo1n1(cb: Cb1Oh | Cb1Node) {} -function fo1n2(cb: Cb1Oh | Cb2Node) {} -function fo2n1(cb: Cb2Oh | Cb1Node) {} -function fo2n2(cb: Cb2Oh | Cb2Node) {} - -fo1n1((arg) => arg); -fo1n2((arg) => arg); -fo2n1((arg) => arg); -fo2n2((arg) => arg); -fo1n1((arg: number) => arg); -fo1n2((arg: number) => arg); -fo2n1((arg: number) => arg); -fo2n2((arg: number) => arg); - -function fg1l1opt(cb?: Cb1Good | Cb1Loc) {} -function fg1l2opt(cb?: Cb1Good | Cb2Loc) {} -function fg2l1opt(cb?: Cb2Good | Cb1Loc) {} -function fg2l2opt(cb?: Cb2Good | Cb2Loc) {} - -fg1l1opt((arg) => arg); -fg1l2opt((arg) => arg); -fg2l1opt((arg) => arg); -fg2l2opt((arg) => arg); -fg1l1opt((arg: number) => arg); -fg1l2opt((arg: number) => arg); -fg2l1opt((arg: number) => arg); -fg2l2opt((arg: number) => arg); - -function fo1l1opt(cb?: Cb1Oh | Cb1Loc) {} -function fo1l2opt(cb?: Cb1Oh | Cb2Loc) {} -function fo2l1opt(cb?: Cb2Oh | Cb1Loc) {} -function fo2l2opt(cb?: Cb2Oh | Cb2Loc) {} - -fo1l1opt((arg) => arg); -fo1l2opt((arg) => arg); -fo2l1opt((arg) => arg); -fo2l2opt((arg) => arg); -fo1l1opt((arg: number) => arg); -fo1l2opt((arg: number) => arg); -fo2l1opt((arg: number) => arg); -fo2l2opt((arg: number) => arg); - -function fo1n1opt(cb?: Cb1Oh | Cb1Node) {} -function fo1n2opt(cb?: Cb1Oh | Cb2Node) {} -function fo2n1opt(cb?: Cb2Oh | Cb1Node) {} -function fo2n2opt(cb?: Cb2Oh | Cb2Node) {} - -fo1n1opt((arg) => arg); -fo1n2opt((arg) => arg); -fo2n1opt((arg) => arg); -fo2n2opt((arg) => arg); -fo1n1opt((arg: number) => arg); -fo1n2opt((arg: number) => arg); -fo2n1opt((arg: number) => arg); -fo2n2opt((arg: number) => arg); diff --git a/ets2panda/linter/test/main/optional_methods.ets b/ets2panda/linter/test/main/optional_methods.ets new file mode 100644 index 0000000000000000000000000000000000000000..30e161d58108c2a4c6d91468a528eb05a848ce19 --- /dev/null +++ b/ets2panda/linter/test/main/optional_methods.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023-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. + */ + +class X { + throw?() {} +} + +interface Y { + throw?(): void +} diff --git a/ets2panda/linter/test/main/optional_methods.sts b/ets2panda/linter/test/main/optional_methods.sts deleted file mode 100644 index 05741c68306ae6c754447d19d59d6635798cc595..0000000000000000000000000000000000000000 --- a/ets2panda/linter/test/main/optional_methods.sts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -class X { - throw?() {} -} - -interface Y { - throw?(): void -} diff --git a/ets2panda/linter/test/main/parameter_properties.sts b/ets2panda/linter/test/main/parameter_properties.ets similarity index 100% rename from ets2panda/linter/test/main/parameter_properties.sts rename to ets2panda/linter/test/main/parameter_properties.ets diff --git a/ets2panda/linter/test/main/partial_type.ets b/ets2panda/linter/test/main/partial_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..f183c3e24fedc58143d67cd81834ba2f7c3c4434 --- /dev/null +++ b/ets2panda/linter/test/main/partial_type.ets @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023-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 Todo { + title: string; + description: string; +} +function updateTodo(todo: Todo, fieldsToUpdate: Partial) { // OK + return { ...todo, ...fieldsToUpdate }; +} +const todo1 = { + title: 'organize desk', + description: 'clear clutter', +}; +const todo2 = updateTodo(todo1, { + description: 'throw out trash', +}); + +let numberPartial: Partial = 1; // NOT OK +type StringPartial = Partial; // NOT OK + +// Initializing Partial +interface Style { + color: number; +} + +function applyStyle(arg: Partial