扩展Javascript语法以添加键入

时间:2017-04-26 05:28:48

标签: javascript sweet.js esprima

我想扩展javascript以添加自定义类型检查。

e.g。

function test(welcome:string, num:integer:non-zero) {
   console.log(welcome + num)
}

将编译成:

function test(welcome, num) {
    if(Object.prototype.toString.call(welcome) !== "[object String]") {
        throw new Error('welcome must be a string')
    }

    if (!Number.isInteger(num)) {
        throw new Error('num must be an integer')
    }

    console.log(welcome + num)
}

最简单的方法是什么?

到目前为止,我已经看过了:

  • sweet.js(在线文档看起来已过时,因为我认为它正在进行某种内部重写)
  • esprima和escodegen(不知道从哪里开始)
  • 使用常规表达式手动解析

1 个答案:

答案 0 :(得分:1)

在评估了所有各种选项后,使用sweet.js似乎是最佳解决方案。工作仍然相当困难(我可能做错了方法)但是为了防止有人想要做类似的事情,这就是我的解决方案。

    'use strict'

    syntax function = function(ctx) {
        let funcName   = ctx.next().value;
        let funcParams = ctx.next().value;
        let funcBody   = ctx.next().value;

        //produce the normal params array
        var normalParams = produceNormalParams(funcParams)

        //produce the checks
        var paramChecks = produceParamChecks(funcParams)

        //produce the original funcBody code

        //put them together as the final result

        var params = ctx.contextify(funcParams)

        var paramsArray = []
        for (let stx of params) {
            paramsArray.push(stx)
        }

        var inner = #``
        var innerStuff = ctx.contextify(funcBody)
        for (let item of innerStuff) {
            inner = inner.concat(#`${item}`)
        }

        var result = #`function ${funcName} ${normalParams} {
            ${paramChecks}
            ${inner}
        }`

        return result

        function extractParamsAndParamChecks(paramsToken) {
            var paramsContext = ctx.contextify(paramsToken)

            //extracts the actual parameters
            var paramsArray = []
            var i = 0;
            var firstItembyComma = true
            for (let paramItem of paramsContext) {
                if (firstItembyComma) {
                    paramsArray.push({
                        param: paramItem,
                        checks: []
                    })
                    firstItembyComma = false
                }

                if (paramItem.value.token.value === ',') {
                    firstItembyComma = true
                    i++
                } else {
                    paramsArray[i].checks.push(paramItem.value.token.value)
                }
            }

            for (var i = 0; i < paramsArray.length; i++) {
                var checks = paramsArray[i].checks.join('').split(':')
                checks.splice(0, 1)
                paramsArray[i].checks = checks
            }

            return paramsArray
        }

        function produceNormalParams(paramsToken) {
            var paramsArray = extractParamsAndParamChecks(paramsToken)

            //Produces the final params #string
            var inner = #``
            var first = true
            for (let item of paramsArray) {
                if (first === true) {
                    inner = inner.concat(#`${item.param}`)
                } else {
                    inner = inner.concat(#`,${item.param}`)
                }
            }
            return #`(${inner})`
        }

        function produceParamChecks(paramsToken) {
            var paramsArray = extractParamsAndParamChecks(paramsToken)

            var result = #``
            for (let paramObject of paramsArray) {
                var tests = produceChecks(paramObject)
                result = result.concat(#`${tests}`)
            }
            return result
        }

        function produceChecks(paramObject) {
            var paramToken = paramObject.param
            var itemType   = paramObject.checks[0]
            var checks     = paramObject.checks

            if (itemType === undefined) return #``

            if (itemType === 'array') {
                return #`if (Object.prototype.toString.call(${paramToken}) !== "[object Array]") throw new Error('Must be array:' + ${paramToken})`
             else {
                throw new Error('item type not recognised: ' + itemType)
            }
        }
    }