From ac9e11a1e516e7876344ae1a224050b2f17baeab Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 11 Jun 2024 14:26:43 -0400 Subject: [PATCH] fix: TS imports are now es6 imports Signed-off-by: Vincent Biret --- CHANGELOG.md | 7 ++++--- .../TypeScript/TypeScriptRelativeImportManager.cs | 13 +++++++------ .../Writers/TypeScriptRelativeImportManagerTests.cs | 12 ++++++------ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d354665a1f..01068b4d5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- TypeScript imports are now using ES6 imports with the .js extension. + ## [1.15.0] - 2024-06-06 ### Added @@ -56,7 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes a bug where warnings about discriminator not being inherited were generated [#4761](https://github.com/microsoft/kiota/issues/4761) - Fix scalar member composed type serialization in PHP [#2827](https://github.com/microsoft/kiota/issues/2827) - Trims unused components from output openApi document when generating plugins [#4672](https://github.com/microsoft/kiota/issues/4672) -- Fixes missing imports for UntypedNode when backingstore is enabled in Java. +- Fixes missing imports for UntypedNode when backingstore is enabled in Java. - Renames `name_to_be_defined` plugin type to `apiplugin` [#4713](https://github.com/microsoft/kiota/issues/4713) ## [1.14.0] - 2024-05-02 @@ -620,7 +622,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed unused generated import for PHP Generation. - Fixed a bug where long namespaces would make Ruby packaging fail. - Fixed a bug where classes with namespace names are generated outside namespace in Python. [#2188](https://github.com/microsoft/kiota/issues/2188) -- Changed signature of escaped reserved names from {x}_escaped to {x}_ in line with Python style guides. +- Changed signature of escaped reserved names from {x}*escaped to {x}* in line with Python style guides. - Add null checks in generated Shell language code. - Fixed a bug where Go indexers would fail to pass the index parameter. - Fixed a bug where path segments with parameters could be missing words. [#2209](https://github.com/microsoft/kiota/issues/2209) @@ -1369,4 +1371,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial GitHub release - diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptRelativeImportManager.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptRelativeImportManager.cs index e88efbb56b..98d310169d 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptRelativeImportManager.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptRelativeImportManager.cs @@ -2,12 +2,8 @@ using Kiota.Builder.Extensions; namespace Kiota.Builder.Writers.TypeScript; -public class TypescriptRelativeImportManager : RelativeImportManager - +public class TypescriptRelativeImportManager(string namespacePrefix, char namespaceSeparator) : RelativeImportManager(namespacePrefix, namespaceSeparator) { - public TypescriptRelativeImportManager(string namespacePrefix, char namespaceSeparator) : base(namespacePrefix, namespaceSeparator) - { - } /// /// Returns the relative import path for the given using and import context namespace. /// @@ -26,9 +22,14 @@ public override (string, string, string) GetRelativeImportPathForUsing(CodeUsing } : (codeUsing.Name, null); if (typeDef == null) - return (importSymbol, codeUsing.Alias, "./"); // it's relative to the folder, with no declaration (default failsafe) + return (importSymbol, codeUsing.Alias, $"./{IndexFileName}"); // it's relative to the folder, with no declaration (default failsafe) var importNamespace = typeDef.GetImmediateParentOfType(); var importPath = GetImportRelativePathFromNamespaces(currentNamespace, importNamespace); + if (importPath.EndsWith('/')) + importPath += IndexFileName; + else + importPath += ".js"; return (importSymbol, codeUsing.Alias, importPath); } + private const string IndexFileName = "index.js"; } diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScriptRelativeImportManagerTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScriptRelativeImportManagerTests.cs index 9dbaa393dc..727040bb9c 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScriptRelativeImportManagerTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScriptRelativeImportManagerTests.cs @@ -44,7 +44,7 @@ public void ReplacesImportsSubNamespace() }; declaration.AddUsings(nUsing); var result = importManager.GetRelativeImportPathForUsing(nUsing, graphNS); - Assert.Equal("./messages/", result.Item3); + Assert.Equal("./messages/index.js", result.Item3); } [Fact] public void ReplacesImportsParentNamespace() @@ -70,7 +70,7 @@ public void ReplacesImportsParentNamespace() }; declaration.AddUsings(nUsing); var result = importManager.GetRelativeImportPathForUsing(nUsing, modelsNS); - Assert.Equal("../messages/", result.Item3); + Assert.Equal("../messages/index.js", result.Item3); } [Fact] public void ReplacesImportsInOtherTrunk() @@ -118,8 +118,8 @@ public void ReplacesImportsInOtherTrunk() declaration2.AddUsings(nUsing2); var result = importManager.GetRelativeImportPathForUsing(nUsing, usedRangeNS1); var result2 = importManager.GetRelativeImportPathForUsing(nUsing2, usedRangeNS2); - Assert.Equal("../../../../", result.Item3); - Assert.Equal("../../", result2.Item3); + Assert.Equal("../../../../index.js", result.Item3); + Assert.Equal("../../index.js", result2.Item3); } [Fact] public void ReplacesImportsSameNamespace() @@ -141,7 +141,7 @@ public void ReplacesImportsSameNamespace() }; declaration.AddUsings(nUsing); var result = importManager.GetRelativeImportPathForUsing(nUsing, graphNS); - Assert.Equal("./", result.Item3); + Assert.Equal("./index.js", result.Item3); } [Fact] @@ -165,6 +165,6 @@ public void ReplacesImportsSameNamespaceIndex() }; declaration.AddUsings(nUsing); var result = importManager.GetRelativeImportPathForUsing(nUsing, graphNS); - Assert.Equal("./", result.Item3); + Assert.Equal("./index.js", result.Item3); } }