Skip to content

AssignName Code Mappings condition

ldfallas edited this page Aug 31, 2015 · 4 revisions

Description

This condition is used to assign a name to the current element being tested.

Name are specified using a dollar sign ($) followed by a sequence of letters. For example $expr .

Assigning a name inside a condition is useful when parts of an input expression need to be reused in a target expression. This condition is useful when used in conjunction with the [ReplaceWithTemplate Code Action](ReplaceWithTemplate Code Action) or the [AddPreStatementFromTemplate Code Action](AddPreStatementFromTemplate Code Action) .

This condition always succeeds if the name of the variable is valid.

Properties

Property ​Usage ​Description
​Name Content property /​Required The name of the variable that refers to the current expression.

Example

Say that we want to convert the Microsoft.Phone.Controls.WebBrowser.InvokeScript method to the WebView.InvokeScriptAsync method. In order to do this we need to take parts of the original expression and assign names to it.

Here's an implementation of this mapping:

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers"
         xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=Mobilize.ExtensibleMappers">
  <MapUnit.Elements>
    <map:CodeMapPackage Type="Microsoft.Phone.Controls.WebBrowser">
      <map:CodeMapPackage.Maps>
        <map:CodeMap Kind="Type">
          <map:CodeMap.Action>
            <map:ReplaceClassUsage NewNamespace="Windows.UI.Xaml.Controls"
                                   NewClassName="WebView" />
          </map:CodeMap.Action>
        </map:CodeMap>
         ...
        <map:CodeMap Kind="Call" MemberName="InvokeScript">
          <map:Conditional>
            <map:Case>
              <map:Case.Condition>
                <map:WithMethodCall>
                  <map:WithCalledMemberOwner>
                    <map:AssignName>$webView</map:AssignName>
                  </map:WithCalledMemberOwner>
                  <map:WithArgument Position="0">
                    <map:AssignName>$function</map:AssignName>
                  </map:WithArgument>
                  <map:AssignNameToArgumentRange From="1" Name="$argsFromtwo"/>
                </map:WithMethodCall>
              </map:Case.Condition>
              <map:Case.Action>
                <map:ReplaceWithTemplate>
                  await $webView.InvokeScriptAsync($function, new[] {$argsFromtwo})
                </map:ReplaceWithTemplate>
              </map:Case.Action>
            </map:Case>
            <map:Default>
              <map:Keep/>
            </map:Default>
          </map:Conditional>
        </map:CodeMap>
       ...
      </map:CodeMapPackage.Maps>
    </map:CodeMapPackage>
  </MapUnit.Elements>
</MapUnit>

The condition of this mapping expression extracts parts of the original expression. The following picture shows how the different parts of the expression are captured:

assign name example

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally