php - 在哪里可以找到用 PHP 编写的 JSLint/js 验证解析器?

标签 php javascript jslint

我为构建脚本使用了 PHP 版本的 JSLint。但不是我写的……我的前同事写的。但是现在,我不记得名字是什么了,我在哪里可以找到 PHP 库为我做的 JSLint 事情。有知道的吗?塔克斯。

最佳答案

尝试:

http://www.overflow.biz/blog/lang/en-us/2010/07/07/jslint-php-class/

与:

http://www.javascriptlint.com/download.htm

来自第一个链接的代码(归功于 z3n)

解决方案:

// (c) z3n - R1V1@100707 - www.overflow.biz - rodrigo.orph@gmail.com
// Based on the original by Matthias Miller (http://www.JavaScriptLint.com/)

class JSLEngine {
    private $_binarypath;  // jlint exec
    private $_confpath;    // config path
    private $fn;           // temp filename (not used outside class)
    private $r;            // jlint output
    private $has_errors=0; // error flag

    public function __construct($binarypath="", $confpath="") {
        // default paths
        base_defines(array(
            "jslint_binary_path" => _fn_fix(dirname(dirname(dirname(__FILE__)))."/3rd/jsl-0.3.0/jsl.exe"),
            "jslint_conf_path" => _fn_fix(dirname(dirname(dirname(__FILE__)))."/3rd/jsl-0.3.0/jsl.default.conf")
        ));

        // startup
        $this->_binarypath = $binarypath == "" ? jslint_binary_path : $binarypath;
        $this->_confpath = $confpath == "" ? jslint_conf_path : $confpath;
    }

    public function __destruct() {
        if ($this->fn != null && file_exists($this->fn))
            unlink($this->fn);
    }

    /* returns error on failure; returns true on success */
    public function Lint($code) {
      if (!$this->_launchLintBinary($code, $output))
          die('The JavaScript Lint online service is currently unavailable.');

      // store lint
      $this->r=$output;
      $output=explode("\n",$output); // break lines
      $x=$output[count($output)-2]; // X error(s), X warning(s) (total lines -2)
      $x=trim(substr($x,0,strpos($x," ")));
      if ($x > 0) { // has errors
          $this->has_errors=1;
          return false;
      } else { // clean
          $this->has_errors=0;
          return true;
      }
    }

    /* assumes path and that SERVER_SOFTWARE env is set */
    private function _launchLintBinary($input, &$output) {
    $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
        2 => array("pipe", "w")
    );

    $this->fn=_fn_fix(dirname(__FILE__).'/tmp.js');

    file_put_contents($this->fn,$input);
    /* launch process */
    $path = PHP_OS == "WINNT" ? $this->_binarypath : escapeshellcmd($this->_binarypath);
    $path.= ' --nologo --conf '.escapeshellarg($this->_confpath).' --process '.escapeshellarg($this->fn);

    $process = proc_open($path, $descriptorspec, $pipes);
    if (!is_resource($process))
        return false;

    $output = '';
    while (!feof($pipes[1]))
       $output .= fgets($pipes[1], 1024);
    fclose($pipes[1]);
    fclose($pipes[2]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);
    return true;
    }

    public function output() {
        return $this->r;
    }
}

辅助函数:

function _fn_fix($fn,$force="") { // v1.02
    if (strpos($fn,"://") === false) {
        if ((PHP_OS == "WINNT" && $force == "") || $force == "WINNT")
            $fn=str_replace("/","\\",$fn);
        else
            $fn=str_replace("\\","/",$fn);
    }
    if (strpos($fn,":/") !== false && strpos($fn,"://") === false)
        $fn=substr($fn,2);

    return $fn;
}

function base_defines($x) { // define default
    foreach ($x as $k => $v) {
        if (!defined($k)) {
            define($k,$v);
        }
    }
}

用法:

$lint=new JSLEngine();
if (!$lint->Lint($js)) {
    echo "bad js code! full output:\n";
    echo $lint->output();
}

必需:

来自 http://www.JavaScriptLint.com/ 的 jslint 二进制文件

确保在 __construct 上设置默认路径,这样就不需要在每次调用时都设置它。

关于php - 在哪里可以找到用 PHP 编写的 JSLint/js 验证解析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6473473/

相关文章:

php - MySQL:跨位于不同服务器上的多个数据库加入查询

php - 在 jquery 数据表中加入查询问题

javascript - 5 秒后删除内容表单标签

javascript - 除了在表单元素上使用原始 "action"属性之外,还有其他方法可以解析表单的 POST 请求吗?

php - 可以使用 PHP 短标签吗?

php - 无法在弹出窗口中显示图像

javascript - 如何从浏览器运行桌面应用程序?

visual-studio-2013 - Visual Studio 2013 - JSLint.NET 已安装但未显示在工具中?

javascript - 在 CodeMirror 中禁用 JSHint 警告

javascript - 函数 (..) 抛出 'eval is evil' 消息