Пример #1
0
        ParameterSyntax[] TransformParameters(Method method)
        {
            var list = new List <ParameterSyntax>();

            if (method.Params.Count > 0)
            {
                var arg0 = method.Params[0];

                if (arg0.Name == null)
                {
                    list.Add(Parameter(_names.AnonymousParameter.Identifier)
                             .WithType(_names.MakeTypeSyntax(arg0.Type, method.DeclaringInterface, TypeUsage.DomainClass)));
                }
                else
                {
                    foreach (var arg in method.Params)
                    {
                        list.Add(Parameter(Identifier(IdentifierRenamer.ToNonKeyword(arg.Name)))
                                 .WithType(_names.MakeTypeSyntax(arg.Type, method.DeclaringInterface, TypeUsage.DomainClass)));
                    }
                }
            }

            list.Add(Parameter(_names.CancellationTokenParameter.Identifier)
                     .WithType(IdentifierName(nameof(CancellationToken)))
                     .WithDefault(EqualsValueClause(LiteralExpression(
                                                        SyntaxKind.DefaultLiteralExpression,
                                                        Token(SyntaxKind.DefaultKeyword)))));

            return(list.ToArray());
        }
Пример #2
0
        public static JsStatement Process(JsStatement statement)
        {
            var locals  = LocalVariableGatherer.Analyze(statement);
            var globals = ImplicitGlobalsGatherer.Analyze(statement, locals, reportGlobalsAsUsedInAllParentScopes: true);
            var renames = IdentifierRenameMapBuilder.Analyze(statement, locals, globals, GenerateName);

            return(IdentifierRenamer.Process(statement, renames));
        }
Пример #3
0
        public string GetCodeIdentifier(TypeDefinition def)
        {
            string id = def.CsName ?? def.Name;

            if (def.Tag == TypeTag.Group) // special treatment for groups: Need to disambiguate between
            {                             // the field name (use original name) and its type (make it start with a lower-case letter)
                id = IdentifierRenamer.ToNonKeyword(SyntaxHelpers.MakeLowerCamel(id));
            }
            return(id);
        }
 IEnumerable <ExpressionSyntax> MakeSkeletonMethodResultStructInitializer(Method method)
 {
     foreach (var arg in method.Results)
     {
         yield return(AssignmentExpression(
                          SyntaxKind.SimpleAssignmentExpression,
                          _names.GetCodeIdentifier(arg).IdentifierName,
                          IdentifierName(IdentifierRenamer.ToNonKeyword(arg.Name))));
     }
 }
Пример #5
0
 IEnumerable <ExpressionSyntax> MakeProxyCallInitializerAssignments(Method method)
 {
     foreach (var methodParam in method.Params)
     {
         yield return(AssignmentExpression(
                          SyntaxKind.SimpleAssignmentExpression,
                          _names.GetCodeIdentifier(methodParam).IdentifierName,
                          IdentifierName(IdentifierRenamer.ToNonKeyword(methodParam.Name))));
     }
 }
Пример #6
0
 protected SyntaxNode HoistVariable(CSharpSyntaxNode node, SyntaxToken identifier, TypeSyntax type)
 {
     if (hoistedVariables.ContainsKey(identifier.ToString()))
     {
         var newName       = GenerateNewName(identifier);
         var newIdentifier = SyntaxFactory.Identifier(newName);
         node       = IdentifierRenamer.RenameIdentifier(node, identifier, newIdentifier);
         identifier = newIdentifier;
     }
     hoistedVariables[identifier.ToString()] = type;
     return(node);
 }
Пример #7
0
 string GetCodeIdentifierLowerCamel(Field field) => field.CsName ?? IdentifierRenamer.ToNonKeyword(SyntaxHelpers.MakeLowerCamel(field.Name));
Пример #8
0
 public Name GetCodeIdentifier(Method method)
 {
     return(new Name(method.CsName ?? IdentifierRenamer.ToNonKeyword(SyntaxHelpers.MakeUpperCamel(method.Name))));
 }
Пример #9
0
            public static IList <JsStatement> Process(IMetadataImporter metadataImporter, INamer namer, IAttributeStore attributeStore, ICompilation compilation, IList <JsStatement> statements)
            {
                var locals          = LocalVariableGatherer.Analyze(statements);
                var globals         = ImplicitGlobalsGatherer.Analyze(statements, locals, reportGlobalsAsUsedInAllParentScopes: false);
                var introducedNames = IntroducedNamesGatherer.Analyze(statements, metadataImporter, attributeStore, compilation.MainAssembly);
                var renameMap       = RenameMapBuilder.BuildMap(statements, locals, globals, introducedNames, namer);

                var usedSymbols = new HashSet <string>();

                foreach (var sym in         locals.Values.SelectMany(v => v)                            // Declared locals.
                         .Concat(globals.Values.SelectMany(v => v))                                     // Implicitly declared globals.
                         .Concat(renameMap.Values.SelectMany(v => v.Values))                            // Locals created during preparing rename.
                         .Concat(introducedNames.Values.SelectMany(v => v))                             // All global types used.
                         )
                {
                    usedSymbols.Add(sym);
                }

                statements = IdentifierRenamer.Process(statements, renameMap).ToList();

                bool isModule = MetadataUtils.GetModuleName(compilation.MainAssembly, attributeStore) != null || MetadataUtils.IsAsyncModule(compilation.MainAssembly, attributeStore);
                var  importer = new ImportVisitor(metadataImporter, namer, attributeStore, compilation.MainAssembly, usedSymbols, JsExpression.Identifier(isModule ? "exports" : "$asm"));

                var body = (!isModule ? new[] { JsStatement.Var("$asm", JsExpression.ObjectLiteral()) } : new JsStatement[0]).Concat(statements.Select(s => importer.VisitStatement(s, null))).ToList();
                var moduleDependencies = importer._moduleAliases.Concat(MetadataUtils.GetAdditionalDependencies(compilation.MainAssembly, attributeStore));

                if (MetadataUtils.IsAsyncModule(compilation.MainAssembly, attributeStore))
                {
                    body.InsertRange(0, new[] { JsStatement.UseStrict, JsStatement.Var("exports", JsExpression.ObjectLiteral()) });
                    body.Add(JsStatement.Return(JsExpression.Identifier("exports")));

                    var pairs = new[] { new KeyValuePair <string, string>("mscorlib", namer.GetVariableName("_", usedSymbols)) }
                    .Concat(moduleDependencies.OrderBy(x => x.Key))
                    .ToList();

                    body = new List <JsStatement> {
                        JsExpression.Invocation(
                            JsExpression.Identifier("define"),
                            JsExpression.ArrayLiteral(pairs.Select(p => JsExpression.String(p.Key))),
                            JsExpression.FunctionDefinition(
                                pairs.Select(p => p.Value),
                                JsStatement.Block(body)
                                )
                            )
                    };
                }
                else if (moduleDependencies.Any())
                {
                    // If we require any module, we require mscorlib. This should work even if we are a leaf module that doesn't include any other module because our parent script will do the mscorlib require for us.
                    body.InsertRange(0, new[] { JsStatement.UseStrict, JsExpression.Invocation(JsExpression.Identifier("require"), JsExpression.String("mscorlib")) }
                                     .Concat(moduleDependencies
                                             .OrderBy(x => x.Key).OrderBy(x => x.Key)
                                             .Select(x => JsStatement.Var(
                                                         x.Value,
                                                         JsExpression.Invocation(
                                                             JsExpression.Identifier("require"),
                                                             JsExpression.String(x.Key))))
                                             .ToList()));
                }
                else
                {
                    body.Insert(0, JsStatement.UseStrict);
                    body = new List <JsStatement> {
                        JsExpression.Invocation(JsExpression.FunctionDefinition(new string[0], JsStatement.Block(body)))
                    };
                }

                return(body);
            }