批量修复JSHints在项目中找到的缺少分号

Eva*_*bbs 9 javascript command-line jshint gruntjs

根据JSHint,我有一个100多个javascript文件的项目,每个文件有1-5个缺少的分号(因此有多个人在使用不同的约定来处理项目).

我想批量修复所有内容,因为单独修复每个内容是不切实际的.我发现了这个:https://github.com/jshint/fixmyjs但我没有看到只修复分号而没有'修复'其他东西(例如制表符 - >空格)的方法.

有谁知道这样做的方法?我很满意它可能导致破损/引入错误.我查看错误,看起来很常规.

Law*_*nes 14

我真的希望你喜欢这个解决方案.修复问题后,请务必小心再次使用jshint进行验证.出于好奇,你是如何设法获得这么多破解的javascript文件的?

#!/bin/sh
function fixFile {
  for i in `jshint $1 | grep -i "Missing semicolon" \
                      | sed -e 's/\([^0-9]*\)\([0-9]*\)\(.*$\)/\2/'`;
  do
    sed -i $1 -e $i's/\(\s*\)$/;/'
  done
}

fixFile $1
Run Code Online (Sandbox Code Playgroud)

上面使用jshint生成一些错误行,只为缺少的分号错误greps它们,提取每个错误的行号,然后在该行上就地放置文件以删除任何尾随空格并用分号替换它.

文件...

var a = 5, c = 4

function helloWorld() {
  if (this == doesntmakesense)
    console.log('hello' + 'world');
}

console.log(a+c);
console.log('finished')   
Run Code Online (Sandbox Code Playgroud)

... ...变

var a = 5, c = 4;

function helloWorld() {
  if (this == doesntmakesense)
    console.log('hello' + 'world');
}

console.log(a+c);
console.log('finished');
Run Code Online (Sandbox Code Playgroud)

忽略小的语义错误,只处理分号.

我将bash脚本保存为类似fixFile.sh然后运行的东西find . -name "*.js" -exec ./fixFile.sh {} \;

但请事先提交.所有命令的运行风险由您自行承担;)


Chr*_*s2M 11

添加缺少的分号比使用正则表达式更安全.使用以下步骤将fixmyjs配置为仅修复Evan最初提出的分号:

  1. 安装fixmyjs

    npm install fixmyjs -g

  2. fixmyjs-config使用以下内容创建fixmyjs配置文件

    { "asi": false }

  3. 运行fixmyjs

    fixmyjs --config [fixmyjs-config] --legacy yourfile.js

参考:https://github.com/jshint/fixmyjs/issues/86