npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

as2ts-smart

v1.0.13

Published

An useful tool to translate actionscript code to typescript.

Downloads

23

Readme

AS2TS

本工具可以帮助你将Actionscript3.0项目完美地转换为TypeScript项目,特别适合于早期使用Laya1.0+AS3开发项目的你。

特色

  • 批量将ActionScript3.0代码完美翻译成TypeScript代码,解决普通的正则表达式替换无法解决的各类问题
  • 智能添加this指针
  • 智能导入import
  • 支持自定义类型转换

比较

  • nshen/as2ts - 只是简单的正则表达式替换,翻译后会有各种语法问题,无法解决for each/for in/new Vector.<int>()之类的翻译问题,也无法自动添加this指针和import。

将Laya 1.0 as项目转换为Laya 2.0 ts项目

使用本工具可以一键转换Laya项目,这篇文章提供了操作步骤,请参考:全网最好用的AS3转TS代码工具:as2ts-smart

使用环境

Node.js

安装

npm i as2ts-smart -g

用法

简单模式

as2ts-smart -s E:\\asproj\\src\\ --dist E:\\tsproj\\src\\

高级模式

as2ts-smart -s E:\\asproj\\src\\ --dist E:\\tsproj\\src\\ -r E:\\rule.json

参数

-h, --help

输出帮助信息。

-s, --src

必须。待翻译的ActionScript3.0代码目录。

--dist

必须。生成的TypeScript代码输出目录。

-m, --module

可选。将ActionScript3.0的package翻译成module

-M, --no-module

可选(默认)。与-m相反。

-r, --rule

可选。翻译规则描述文件,as2ts-smart将根据该描述指定的规则进行翻译。

使用配置文件自定义翻译规则

as2ts-smart支持你通过配置文件自定义翻译规则,配置文件需要为一个JSON文件。 配置文件的格式定义如下,请参见As2TsOption

export interface As2TsOption {
    /**忽略规则 */
    skipRule?: As2TsSkipRule, 
    /**标识符替换规则,不支持正则表达式匹配 */
    idReplacement?: {[raw: string]: string}, 
    /**字面量替换规则,不支持正则表达式匹配 */
    literalReplacement?: {[raw: string]: string}, 
    /**类型映射规则,不支持正则表达式匹配 */
    typeMapper?: {[key: string]: string}, 
    /**方法替换映射规则,不支持正则表达式匹配 */
    methordMapper?: {[key: string]: string}, 
    /**是否生成模块,默认为false */
    module?: boolean, 
    /**模块导入规则 */
    importRule?: As2TsImportRule, 
    /**额外的typescript代码库,用于补充类库信息 */
    tsLibs?: string[], 
    /**是否打印详细的错误信息 */
    errorDetail?: boolean, 
    /**是否在发生错误时立即终止 */
    terminateWhenError?: boolean, 
    /**是否从上次运行处继续翻译 */
    continueLast?: boolean,
    /**临时文件缓存目录 */
    tmpRoot?: string
}

配置例子

完整的配置例子,请参见rule.json 将Laya 1.0+as升级到Laya 2.0+ts的配置例子,请参见laya1upto2.json

skipRule

此项用于在翻译时忽略部分文件,其格式定义如下,请参见As2TsSkipRule

export interface As2TsSkipRule {
    /**需要忽略的文件夹,支持正则表达式匹配 */
    "dirs"?: string[], 
    /**需要忽略的文件,支持正则表达式匹配 */
    "files"?: string[]
}

比如,假设输入的as3项目目录为asproj/src,则下述规则表示不要翻译asproj/src/ui/*asproj/src/automatic/*目录下的文件,以及MsgPool.as等文件。 注意,如果使用正则表达式匹配,也要用字符串形式配置,如例子中的"^\\bui\\b",相当于正则表达式^\bui\b。 小提示:\b用于匹配单词的边界,关于其详细用法请查找正则表达式资料,以下不详细解释。

"skipRule": {
    "dirs": ["^\\bui\\b", "^automatic"], 
    "files": ["FyMsg.as", "DecodeUtil.as", "EncodeUtil.as", "SendMsgUtil.as"]
}

idReplacement

此项用于在翻译时将某些标识符(包括变量名、类名、函数名等)进行替换。不支持正则表达式匹配。比如下述规则表示将KW替换为KeyWord等。

"idReplacement": {
    "KW": "KeyWord", 
    "Laya.Component": "Laya.UIComponent", 
    "Laya.RaycastHit": "Laya.HitResult", 
    "Laya.StandardMaterial": "Laya.BlinnPhongMaterial"
}

literalReplacement

此项用于在翻译时将某些字面量(包括数值、字符串等)进行替换。不支持正则表达式匹配比如下述规则表示将字符串automatic/constants/KW替换为automatic/constants/KeyWord

"literalReplacement": {
    "\"automatic/constants/KW\"": "\"automatic/constants/KeyWord\""
}

typeMapper

此项用于对某些类型进行自定义替换。下述是as2ts-smart对此项的默认配置,用于对AS3的基本类型进行翻译。 你只需要配置自己新增的替换项,而不需要重复配置默认替换项。as2ts-smart会将你的自定义配置和默认配置进行合并。

{
    "int": "number", 
    "Number": "number", 
    "uint": "number", 
    "Boolean": "boolean", 
    "String": "string", 
    "Object": "any"
}

methordMapper

此项用于对某些函数进行自定义替换。下述是as2ts-smart对此项的默认配置,用于对AS3的trace方法翻译为TS的console.log方法。 你只需要配置自己新增的替换项,而不需要重复配置默认替换项。as2ts-smart会将你的自定义配置和默认配置进行合并。

{
    "trace": "console.log"
}

module

此项用于指定是否将AS3中的package xxx翻译为module xxx,相应的,所有的import a.b.C;将会翻译为import C = a.b.C;。默认关闭。也可用参数-m/--module开启。比如如下两个AS3类:

// file a/B.as
package a {
    public class B
    {
        //...
    }
}
// file a/b/C.as
package a.b {
    import a.B;
    public class C extends B
    {
        //...
    }
}

默认情况下,将分别翻译为

// file a/B.ts
module a {
    expport class B
    {
        //...
    }
}
// file a/b/C.ts
module a.b {
    import B = a.B;
    export class C extends B
    {
        //...
    }
}

noModule选项设置为true时,则翻译为

// file a/B.ts
expport class B
{
    //...
}
// file a/b/C.ts
import {B} from '../B';
export class C extends B
{
    //...
}

importRule

此项用于将AS的包按照指定的规则以TS模块的形式进行导入,并去除原先的import语句。其格式定义如下,请参见As2TsImportRule

export interface FromModule {
    /**导入的模块名 */
    "module": string, 
    /**匹配规则,支持正则表达式匹配 */
    "regular": string, 
    /**模块名需要另外import的路径*/
    "import"?: string
}

export interface As2TsImportRule {
    /**需要以模块形式进行import导入的规则 */
    "fromModule"?: FromModule[]
}

下述例子的第一个配置即是对AS中的laya.*等进行翻译的应用。它将会把laya.utils.Handlerlaya.ui.Image之类的翻译成Laya.HandlerLaya.Image。 对于某些需要import的模块,比如Laya2.0中ui类的使用,可以配置import项进行import。

"importRule": {
    "fromModule": [
        {"module": "Protocol", "regular": "^automatic/protocol/(?!Macros|ErrorId)" }, 
        {"module": "GameConfig", "regular": "^automatic/cfgs/(?!ConfigDecoder)" }, 
        {"module": "Laya", "regular": "^laya/" }, 
        {"module": "ui", "regular": "^ui/", "import": "ui/layaMaxUI" }
    ]
}

tsLibs

as2ts-smart在翻译时会通过分析AS代码得到类与类之间的继承关系,进而实现对AS中常见的this指针缺省现象进行智能添加。但有时候输入的AS项目中并不能提供最完整的信息,会导致as2ts-smart无法判定某些标识符是否需要添加this指针。比如,有些类型的是由第三方SDK定义的,这时你可以将第三方SDK的.d.ts文件配置于此项。比如,下述例子可以让as2ts-smart获得Laya SDK的相关信息。

"tsLibs": ["E:/qhgame/tsproj/libs/LayaAir.d.ts", "E:/qhgame/tsproj/libs/layaAir.minigame.d.ts", "E:/qhgame/tsproj/src/ui/layaMaxUI.ts"]

errorDetail

开启此项,当发生错误时as2ts-smart将输出更详尽的信息方便定位问题。

terminateWhenError

开启此项,当发生错误时as2ts-smart将停止继续翻译。

continueLast

开启此项,as2ts-smart将从上一次翻译中断的文件继续翻译。

tmpRoot

设置as2ts-smart的工作临时目录,默认为{CURRENTDIRECTORY}/tmp

已知问题

内联函数

暂不支持内联函数的翻译。如果AS代码中存在内联函数,可能导致转换时发生错误(当然也有可能可以成功转换)。请尽量避免使用类似下述的写法:

private function doSomething(): void {
    // 以下调用了一个内联函数
    doSomethingInner();
    // 不要使用此类内联函数
    function doSomethingInner(): void {
        // ...
    }
}

某些不常规的写法

比如不常规的换行、拆行,也可能导致转换时失败,请根据失败提示修改一下写法。比如:

private function maybeFailed(sth: *): void {
    // 以下写法把包括is关键的一对括号放在上下两行,可能会导致转换失败
    if(sth is 
    Image) {
        // ...
    }
}

使用许可

ISC

作者

Github | npm | QQ

鼓励作者继续改善

1元助力