Changeset View
Standalone View
src/lint/linter/ArcanistTextLinter.php
| Show All 10 Lines | final class ArcanistTextLinter extends ArcanistLinter { | ||||
| const LINT_EOF_NEWLINE = 4; | const LINT_EOF_NEWLINE = 4; | ||||
| const LINT_BAD_CHARSET = 5; | const LINT_BAD_CHARSET = 5; | ||||
| const LINT_TRAILING_WHITESPACE = 6; | const LINT_TRAILING_WHITESPACE = 6; | ||||
| const LINT_BOF_WHITESPACE = 8; | const LINT_BOF_WHITESPACE = 8; | ||||
| const LINT_EOF_WHITESPACE = 9; | const LINT_EOF_WHITESPACE = 9; | ||||
| const LINT_EMPTY_FILE = 10; | const LINT_EMPTY_FILE = 10; | ||||
| private $maxLineLength = 80; | private $maxLineLength = 80; | ||||
| private $allowUTF8 = false; | |||||
| public function getInfoName() { | public function getInfoName() { | ||||
| return pht('Basic Text Linter'); | return pht('Basic Text Linter'); | ||||
| } | } | ||||
| public function getInfoDescription() { | public function getInfoDescription() { | ||||
| return pht( | return pht( | ||||
| 'Enforces basic text rules like line length, character encoding, '. | 'Enforces basic text rules like line length, character encoding, '. | ||||
| 'and trailing whitespace.'); | 'and trailing whitespace.'); | ||||
| } | } | ||||
| public function getLinterPriority() { | public function getLinterPriority() { | ||||
| return 0.5; | return 0.5; | ||||
| } | } | ||||
| public function getLinterConfigurationOptions() { | public function getLinterConfigurationOptions() { | ||||
| $options = array( | $options = array( | ||||
| 'text.max-line-length' => array( | 'text.max-line-length' => array( | ||||
| 'type' => 'optional int', | 'type' => 'optional int', | ||||
| 'help' => pht( | 'help' => pht( | ||||
| 'Adjust the maximum line length before a warning is raised. By '. | 'Adjust the maximum line length before a warning is raised. By '. | ||||
| 'default, a warning is raised on lines exceeding 80 characters.'), | 'default, a warning is raised on lines exceeding 80 characters.'), | ||||
| ), | ), | ||||
| 'text.allow-utf8' => array( | |||||
| 'type' => 'optional bool', | |||||
| 'help' => pht( | |||||
| 'Allow any valid UTF-8 character. By default, only ASCII bytes '. | |||||
| 'with ordinal decimal values between 32 and 126 inclusive, plus '. | |||||
| 'linefeed are allowed.'), | |||||
| ), | |||||
| ); | ); | ||||
| return $options + parent::getLinterConfigurationOptions(); | return $options + parent::getLinterConfigurationOptions(); | ||||
| } | } | ||||
| public function setMaxLineLength($new_length) { | public function setMaxLineLength($new_length) { | ||||
| $this->maxLineLength = $new_length; | $this->maxLineLength = $new_length; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function setLinterConfigurationValue($key, $value) { | public function setLinterConfigurationValue($key, $value) { | ||||
| switch ($key) { | switch ($key) { | ||||
| case 'text.max-line-length': | case 'text.max-line-length': | ||||
| $this->setMaxLineLength($value); | $this->setMaxLineLength($value); | ||||
| return; | return; | ||||
| case 'text.allow-utf8': | |||||
| $this->allowUTF8 = $value; | |||||
| return; | |||||
| } | } | ||||
| return parent::setLinterConfigurationValue($key, $value); | return parent::setLinterConfigurationValue($key, $value); | ||||
| } | } | ||||
| public function getLinterName() { | public function getLinterName() { | ||||
| return 'TXT'; | return 'TXT'; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | if (preg_match('/^\s*$/', $data)) { | ||||
| self::LINT_EMPTY_FILE, | self::LINT_EMPTY_FILE, | ||||
| pht("Empty files usually don't serve any useful purpose.")); | pht("Empty files usually don't serve any useful purpose.")); | ||||
| $this->stopAllLinters(); | $this->stopAllLinters(); | ||||
| } | } | ||||
| } | } | ||||
| protected function lintNewlines($path) { | protected function lintNewlines($path) { | ||||
| $data = $this->getData($path); | $data = $this->getData($path); | ||||
| $allowUTF8 = $this->allowUTF8; | |||||
Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $pos = strpos($this->getData($path), "\r"); | $pos = strpos($this->getData($path), "\r"); | ||||
| if ($pos !== false) { | if ($pos !== false) { | ||||
| $this->raiseLintAtOffset( | $this->raiseLintAtOffset( | ||||
| 0, | 0, | ||||
| self::LINT_DOS_NEWLINE, | self::LINT_DOS_NEWLINE, | ||||
| pht('You must use ONLY Unix linebreaks ("%s") in source code.', '\n'), | pht('You must use ONLY Unix linebreaks ("%s") in source code.', '\n'), | ||||
| $data, | $data, | ||||
| ▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | if (!strlen($data) || $data[strlen($data) - 1] != "\n") { | ||||
| pht('Files must end in a newline.'), | pht('Files must end in a newline.'), | ||||
| '', | '', | ||||
| "\n"); | "\n"); | ||||
| } | } | ||||
| } | } | ||||
| protected function lintCharset($path) { | protected function lintCharset($path) { | ||||
| $data = $this->getData($path); | $data = $this->getData($path); | ||||
| $allowUTF8 = $this->allowUTF8; | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $matches = null; | $matches = null; | ||||
| $bad = '[^\x09\x0A\x20-\x7E]'; | // Allow newline, tab, 32 to 126, Letters, Marks, Numbers, | ||||
| // Punctuation and Symbols | |||||
| $badUTF8 = '[^\x09\x0A\x20-\x7E\p{L}\p{M}\p{N}\p{P}\p{S}]'; | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $badUTF8Regex = "/{$badUTF8}(.*{$badUTF8})?/u"; | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $badAscii = '[^\x09\x0A\x20-\x7E]'; | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $badAsciiRegex = "/{$badAscii}(.*{$badAscii})?/"; | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $badRegex = ($allowUTF8 ? $badUTF8Regex : $badAsciiRegex); | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $preg = preg_match_all( | $preg = preg_match_all( | ||||
| "/{$bad}(.*{$bad})?/", | $badRegex, | ||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| $data, | $data, | ||||
| $matches, | $matches, | ||||
| PREG_OFFSET_CAPTURE); | PREG_OFFSET_CAPTURE); | ||||
| if (!$preg) { | if (!$preg) { | ||||
| return; | return; | ||||
| } | } | ||||
| $badAsciiHelpText = pht('Source code should contain only ASCII bytes with ordinal '. | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Line Too Long This line is 88 characters long, but the convention is 80 characters. Lint: Line Too Long: This line is 88 characters long, but the convention is 80 characters. | |||||
| 'decimal values between 32 and 126 inclusive, plus linefeed. '. | |||||
| 'Do not use UTF-8 or other multibyte charsets.'); | |||||
| $badUTF8HelpText = pht('Source code should contain only printable UTF-8 characters.'); | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Line Too Long This line is 90 characters long, but the convention is 80 characters. Lint: Line Too Long: This line is 90 characters long, but the convention is 80 characters. | |||||
| $helpText = ($allowUTF8 ? $badUTF8HelpText : $badAsciiHelpText); | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| foreach ($matches[0] as $match) { | foreach ($matches[0] as $match) { | ||||
| list($string, $offset) = $match; | list($string, $offset) = $match; | ||||
| $this->raiseLintAtOffset( | $this->raiseLintAtOffset( | ||||
| $offset, | $offset, | ||||
| self::LINT_BAD_CHARSET, | self::LINT_BAD_CHARSET, | ||||
| pht( | $helpText, | ||||
Lint: Naming Conventions Follow naming conventions: variables should be named using lowercase_with_underscores. Lint: Naming Conventions: Follow naming conventions: variables should be named using `lowercase_with_underscores`. | |||||
| 'Source code should contain only ASCII bytes with ordinal '. | |||||
| 'decimal values between 32 and 126 inclusive, plus linefeed. '. | |||||
| 'Do not use UTF-8 or other multibyte charsets.'), | |||||
| $string); | $string); | ||||
| } | } | ||||
| if ($this->isMessageEnabled(self::LINT_BAD_CHARSET)) { | if ($this->isMessageEnabled(self::LINT_BAD_CHARSET)) { | ||||
| $this->stopAllLinters(); | $this->stopAllLinters(); | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines | |||||
Follow naming conventions: variables should be named using lowercase_with_underscores.