如何在我的代码中找到 CoffeeScript 1.9.0 重大更改的案例?

How do I find cases of CoffeeScript 1.9.0 breaking change in my code?

TL;DR:有什么方法可以识别违反 CoffeeScript 新的 1.9.0 行为的 @foo 参数命名吗?现在是非法的,并且不会导致 warning/error,在函数中使用裸 foo 变量。

在1.9.0版本的CoffeeScript中是这样写的:

Changed strategy for the generation of internal compiler variable names. Note that this means that @example function parameters are no longer available as naked example variables within the function body.

这意味着

class Animal
  constructor: (@name) ->
    console.log name

.. 会失败,默默地。即上面不会打印新动物的名字。

新的正确解是:

class Animal
  constructor: (@name) ->
    console.log @name

CoffeeLint 没有捕捉到这一点。是否有任何已知的特技来查找现在非法的裸参数使用?也许在生成的 javascript?

上有一个漂亮的脚本 运行

这里有 2 个链接:

最简单的方法是使用旧的 coffee 版本。

如果这不可能,您可以编译每个源文件并 运行 通过 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 
}

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

我制作了一个 mod 的 CoffeeScript 编译器,console.error 每次你都缺少 @https://github.com/lydell/coffee-script/tree/1.8-at-params-warn

安装:

npm install lydell/coffee-script#1.8-at-params-warn

检查文件是否丢失 @s,例如 运行:

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

以上将记录每个需要 @.

的变量的文件路径、行号、列号和变量名