~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to arcanist/src/lint/linter/ArcanistCSSLintLinter.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Uses "CSS Lint" to detect checkstyle errors in css code.
 
5
 */
 
6
final class ArcanistCSSLintLinter extends ArcanistExternalLinter {
 
7
 
 
8
  public function getInfoName() {
 
9
    return 'CSSLint';
 
10
  }
 
11
 
 
12
  public function getInfoURI() {
 
13
    return 'http://csslint.net';
 
14
  }
 
15
 
 
16
  public function getInfoDescription() {
 
17
    return pht('Use `csslint` to detect issues with CSS source files.');
 
18
  }
 
19
 
 
20
  public function getLinterName() {
 
21
    return 'CSSLint';
 
22
  }
 
23
 
 
24
  public function getLinterConfigurationName() {
 
25
    return 'csslint';
 
26
  }
 
27
 
 
28
  public function getMandatoryFlags() {
 
29
    return array(
 
30
      '--format=lint-xml',
 
31
      '--quiet',
 
32
    );
 
33
  }
 
34
 
 
35
  public function getDefaultFlags() {
 
36
    return $this->getDeprecatedConfiguration('lint.csslint.options', array());
 
37
  }
 
38
 
 
39
  public function getDefaultBinary() {
 
40
    return $this->getDeprecatedConfiguration('lint.csslint.bin', 'csslint');
 
41
  }
 
42
 
 
43
  public function getVersion() {
 
44
    list($stdout) = execx('%C --version', $this->getExecutableCommand());
 
45
 
 
46
    $matches = array();
 
47
    if (preg_match('/^v(?P<version>\d+\.\d+\.\d+)$/', $stdout, $matches)) {
 
48
      return $matches['version'];
 
49
    } else {
 
50
      return false;
 
51
    }
 
52
  }
 
53
 
 
54
  public function getInstallInstructions() {
 
55
    return pht('Install CSSLint using `npm install -g csslint`.');
 
56
  }
 
57
 
 
58
  public function shouldExpectCommandErrors() {
 
59
    return true;
 
60
  }
 
61
 
 
62
  protected function parseLinterOutput($path, $err, $stdout, $stderr) {
 
63
    $report_dom = new DOMDocument();
 
64
    $ok = @$report_dom->loadXML($stdout);
 
65
 
 
66
    if (!$ok) {
 
67
      return false;
 
68
    }
 
69
 
 
70
    $files = $report_dom->getElementsByTagName('file');
 
71
    $messages = array();
 
72
    foreach ($files as $file) {
 
73
      foreach ($file->childNodes as $child) {
 
74
        if (!($child instanceof DOMElement)) {
 
75
          continue;
 
76
        }
 
77
 
 
78
        $data = $this->getData($path);
 
79
        $lines = explode("\n", $data);
 
80
        $name = $child->getAttribute('reason');
 
81
        $severity = ($child->getAttribute('severity') == 'warning')
 
82
          ? ArcanistLintSeverity::SEVERITY_WARNING
 
83
          : ArcanistLintSeverity::SEVERITY_ERROR;
 
84
 
 
85
        $message = new ArcanistLintMessage();
 
86
        $message->setPath($path);
 
87
        $message->setLine($child->getAttribute('line'));
 
88
        $message->setChar($child->getAttribute('char'));
 
89
        $message->setCode('CSSLint');
 
90
        $message->setDescription($child->getAttribute('reason'));
 
91
        $message->setSeverity($severity);
 
92
 
 
93
        if ($child->hasAttribute('line') && $child->getAttribute('line') > 0) {
 
94
          $line = $lines[$child->getAttribute('line') - 1];
 
95
          $text = substr($line, $child->getAttribute('char') - 1);
 
96
          $message->setOriginalText($text);
 
97
        }
 
98
 
 
99
        $messages[] = $message;
 
100
      }
 
101
    }
 
102
 
 
103
    return $messages;
 
104
  }
 
105
 
 
106
  protected function getLintCodeFromLinterConfigurationKey($code) {
 
107
 
 
108
    // NOTE: We can't figure out which rule generated each message, so we
 
109
    // can not customize severities. I opened a pull request to add this
 
110
    // ability; see:
 
111
    //
 
112
    // https://github.com/stubbornella/csslint/pull/409
 
113
 
 
114
    throw new Exception(
 
115
      pht(
 
116
        "CSSLint does not currently support custom severity levels, because ".
 
117
        "rules can't be identified from messages in output. ".
 
118
        "See Pull Request #409."));
 
119
  }
 
120
 
 
121
}