-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds typescript support #4
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
42f3d34
- adds the ability to specify the indent size
baywet d885184
- updates test settings to typescript
baywet bd3e037
- adds typescript generation
baywet 585f46c
- adds import support for typescript
baywet e726c60
- fixes an issue where java inner classes would be missing
baywet da1f555
- refactors refiners
baywet 8ec2d33
- fixes an issue where java refiner would introduce undesired usings
baywet e423bcf
Merge pull request #5 from microsoft/feature/refiners
baywet f3ab4d4
- moves decrease indent factor to a stack to avoid disreptencies and …
baywet 2cc4864
- adds failsafe when propping in case of wrong usage
baywet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace kiota.core | ||
{ | ||
public class TypeScriptWriter : LanguageWriter | ||
{ | ||
public override string GetFileSuffix() => ".ts"; | ||
|
||
public override string GetParameterSignature(CodeParameter parameter) | ||
{ | ||
return $"{parameter.Name}{(parameter.Optional ? "?" : string.Empty)}: {GetTypeString(parameter.Type)}{(parameter.Optional ? " | undefined": string.Empty)}"; | ||
} | ||
|
||
public override string GetTypeString(CodeType code) | ||
{ | ||
var typeName = TranslateType(code.Name, code.Schema); | ||
if (code.ActionOf) | ||
{ | ||
var indentFactor = 4; | ||
IncreaseIndent(indentFactor); | ||
var childElements = code.TypeDefinition | ||
.InnerChildElements | ||
.OfType<CodeProperty>() | ||
.Select(x => $"{x.Name}?: {GetTypeString(x.Type)}"); | ||
var innerDeclaration = childElements.Any() ? childElements | ||
.Aggregate((x, y) => $"{x};{Environment.NewLine}{GetIndent()}{y}") | ||
: string.Empty; | ||
DecreaseIndent(indentFactor); | ||
return $"(options?: {{{innerDeclaration}}}) => void"; | ||
} | ||
else | ||
{ | ||
return typeName; | ||
} | ||
} | ||
|
||
public override string TranslateType(string typeName, OpenApiSchema schema) | ||
{ | ||
switch (typeName) | ||
{//TODO we're probably missing a bunch of type mappings | ||
case "integer": return "number"; | ||
case "array": return $"{TranslateType(schema.Items.Type, schema.Items)}[]"; | ||
} // string, boolean, object : same casing | ||
|
||
return typeName; | ||
} | ||
|
||
public override void WriteCodeClassDeclaration(CodeClass.Declaration code) | ||
{ | ||
foreach (var codeUsing in code.Usings) | ||
{ | ||
WriteLine($"import {{{codeUsing.Name}}} from './{codeUsing.Name}';"); | ||
} | ||
WriteLine(); | ||
WriteLine($"export class {code.Name} {{"); | ||
IncreaseIndent(); | ||
} | ||
|
||
public override void WriteCodeClassEnd(CodeClass.End code) | ||
{ | ||
DecreaseIndent(); | ||
WriteLine("}"); | ||
} | ||
|
||
public override void WriteIndexer(CodeIndexer code) | ||
{ | ||
WriteMethod(new CodeMethod { | ||
Name = "get", | ||
Parameters = new List<CodeParameter> { | ||
new CodeParameter { | ||
Name = "position", | ||
Type = code.IndexType, | ||
Optional = false, | ||
} | ||
}, | ||
ReturnType = code.IndexType | ||
}); | ||
} | ||
|
||
public override void WriteMethod(CodeMethod code) | ||
{ | ||
WriteLine($"public readonly {code.Name} = ({string.Join(',', code.Parameters.Select(p=> GetParameterSignature(p)).ToList())}) : Promise<{GetTypeString(code.ReturnType)}> => {{ return Promise.resolve({(code.ReturnType.Name.Equals("string") ? "''" : "{}")}); }}"); | ||
} | ||
|
||
public override void WriteNamespaceDeclaration(CodeNamespace.BlockDeclaration code) => WriteLine(); | ||
|
||
public override void WriteNamespaceEnd(CodeNamespace.BlockEnd code) => WriteLine(); | ||
|
||
public override void WriteProperty(CodeProperty code) | ||
{ | ||
WriteLine($"public {code.Name}?: {GetTypeString(code.Type)}"); | ||
} | ||
|
||
public override void WriteType(CodeType code) | ||
{ | ||
Write(GetTypeString(code), includeIndent: false); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the indentfactor for? Why would some blocks have bigger indents than others? I understand that languages may have different indent values, but why allow it to be varied per language element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's for the anonymous type declaration in the methods parameters. We don't absolutely need it, but it looks nicer.