Use strongly-typed arrays for typescript definitions

Fixes #2584
This commit is contained in:
Sipke Schoorstra 2021-12-14 14:10:35 +01:00
parent ffbac88014
commit 888fa088a6
4 changed files with 34 additions and 7 deletions

View file

@ -1,6 +1,8 @@
import {Component, Host, h, Prop, State, Listen, Method, Watch, Event, EventEmitter} from '@stencil/core';
import {Component, Event, EventEmitter, h, Host, Method, Prop, Watch} from '@stencil/core';
import {initializeMonacoWorker} from "./elsa-monaco-utils";
import state from '../../../utils/store';
import {languages} from "monaco-editor";
import ScriptTarget = languages.typescript.ScriptTarget;
// Until I figure out why the ESM loader doesn't work properly, we need to include these scripts manually from index.html
// import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
@ -52,10 +54,20 @@ export class ElsaMonaco {
async addJavaScriptLib(libSource: string, libUri: string) {
const monaco = this.monaco;
monaco.languages.typescript.javascriptDefaults.setExtraLibs([{
content: "<reference lib=\"es5\" />",
filePath: "lib.d.ts"
}, {
content: libSource,
filePath: libUri
}]);
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
target: ScriptTarget.ES2020,
allowJs: true,
checkJs: true,
})
const oldModel = monaco.editor.getModel(libUri);
if (oldModel)

View file

@ -48,7 +48,7 @@ namespace Elsa.Scripting.JavaScript.Providers
CancellationToken cancellationToken = default)
{
var typeScriptType = notification.GetTypeScriptType(propertyType);
writer.AppendLine($"{propertyName}(): {typeScriptType}");
writer.AppendLine($"{propertyName}(): {typeScriptType};");
return new ValueTask();
}
}

View file

@ -159,10 +159,8 @@ namespace Elsa.Scripting.JavaScript.Providers
var returnType = method.ReturnType;
if (returnType != typeof(void))
{
if (returnType != typeof(void))
output.AppendFormat(":{0}", GetTypeScriptType(context, returnType, collectedTypes));
}
output.AppendLine(";");
}

View file

@ -1,12 +1,29 @@
using System;
using System.Collections;
using System.Linq;
using Elsa.Scripting.JavaScript.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Scripting.JavaScript.Typings
{
public class EnumerableTypeDefinitionProvider : TypeDefinitionProvider
{
public override bool SupportsType(TypeDefinitionContext context, Type type) => typeof(IEnumerable).IsAssignableFrom(type);
public override string GetTypeDefinition(TypeDefinitionContext context, Type type) => "[]";
private readonly IServiceProvider _serviceProvider;
public EnumerableTypeDefinitionProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override bool SupportsType(TypeDefinitionContext context, Type type) => typeof(string) != type && typeof(IEnumerable).IsAssignableFrom(type);
public override string GetTypeDefinition(TypeDefinitionContext context, Type type)
{
var providers = _serviceProvider.GetServices<ITypeDefinitionProvider>().Where(x => x is not EnumerableTypeDefinitionProvider).ToList();
var elementType = type.IsArray ? type.GetElementType()! : type.GetGenericArguments().FirstOrDefault();
var typeScriptType = elementType != null ? providers.FirstOrDefault(x => x.SupportsType(context, elementType))?.GetTypeDefinition(context, elementType) : null;
return typeScriptType == null ? "[]" : $"Array<{typeScriptType}>";
}
}
}