Parameters 如何在代码中找到CoffeeScript 1.9.0破坏性更改的案例?

Parameters 如何在代码中找到CoffeeScript 1.9.0破坏性更改的案例?,parameters,error-handling,coffeescript,Parameters,Error Handling,Coffeescript,TL;DR:CoffeeScript在@foo参数命名方面的新1.9.0行为是否存在违规行为?现在,在函数中使用裸foo变量是非法的,并且不会导致警告/错误 CoffeeScript的1.9.0版本中规定: 更改了生成内部编译器变量的策略 名字。请注意,这意味着@example功能参数不可用 不再作为函数体中的裸示例变量提供 这意味着 class Animal constructor: (@name) -> console.log name 。。会默默地失败。也就是说,上面不

TL;DR:CoffeeScript在
@foo
参数命名方面的新1.9.0行为是否存在违规行为?现在,在函数中使用裸
foo
变量是非法的,并且不会导致警告/错误

CoffeeScript的1.9.0版本中规定:

更改了生成内部编译器变量的策略 名字。请注意,这意味着
@example
功能参数不可用 不再作为函数体中的裸
示例
变量提供

这意味着

class Animal
  constructor: (@name) ->
    console.log name
。。会默默地失败。也就是说,上面不会打印新动物的名字

新的正确解决方案是:

class Animal
  constructor: (@name) ->
    console.log @name
咖啡豆不能捕捉到这一点。是否有任何已知的特技来发现现在非法使用的裸参数?可能是在生成的javascript上运行的漂亮脚本

这里有两个关于此的链接:


    • 最简单的方法是使用较旧的
      咖啡
      版本

      如果不可能,您可以编译每个源文件,并通过
      eslint
      检查
      no undef
      规则运行该源文件。可以使用以下bash脚本执行此操作:

      #!/bin/bash
      
      FILES=$@
      
      # https://nodejs.org/api/globals.html
      # Object.keys(global).concat(['__dirname', '__filename']).join(',')
      DEFINED_GLOBALS='ArrayBuffer,Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,DataView,global,process,GLOBAL,root,Buffer,setTimeout,setInterval,clearTimeout,clearInterval,setImmediate,clearImmediate,console,module,require,__dirname,__filename'
      
      function compile() {
        ./node_modules/.bin/coffee --print $1
      }
      
      function lint() {
        # checks only for undefined variables except DEFINED_GLOBALS
        ./node_modules/.bin/eslint\
          --stdin --no-color\
          --global $DEFINED_GLOBALS\
          --reset --rule 'no-undef: [true]'
      }
      
      function main() {
        for file in $FILES; do
          local problems=`compile $file | lint`
          if [[ $problems ]]; then
            echo -e "=== $file\n$problems\n\n"
          fi
        done
      }
      
      main
      
      将其另存为
      check.sh
      ,并沿以下行运行某些操作:

      chmod u+x ./check.sh
      npm install coffee-script eslint
      find . -name "*.coffee" | xargs ./check.sh
      

      我对CoffeeScript编译器做了一个修改,它是
      控制台。错误
      s每次发生时,您都会丢失一个
      @

      要安装它,请执行以下操作:

      npm install lydell/coffee-script#1.8-at-params-warn
      
      要检查文件是否缺少
      @
      s,请运行例如:

      ./node_modules/.bin/coffee -p file-to-check.coffee >/dev/null
      

      上面将记录每个需要
      @

      的变量的文件路径、行号、列号和变量名。这是一个很好的主意,基本上可以捕获错误发生的位置。我没有使用它,而是编写了一个bash脚本来查找方法使用@param样式的所有位置,然后找到我使用“param”的4个位置。但是如果我发现更多的问题,我会试试你的想法。谢谢请注意,我已经从CoffeeScript转到es6/jspm。有点喜欢。