Changeset View
Changeset View
Standalone View
Standalone View
src/lint/linter/ArcanistTextLinter.php
| <?php | <?php | ||||
| /** | /** | ||||
| * Enforces basic text file rules. | * Enforces basic text file rules. | ||||
| */ | */ | ||||
| final class ArcanistTextLinter extends ArcanistLinter { | final class ArcanistTextLinter extends ArcanistLinter { | ||||
| const LINT_DOS_NEWLINE = 1; | const LINT_DOS_NEWLINE = 1; | ||||
| const LINT_TAB_LITERAL = 2; | const LINT_TAB_LITERAL = 2; | ||||
| const LINT_LINE_WRAP = 3; | const LINT_LINE_WRAP = 3; | ||||
| 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_NO_COMMIT = 7; | |||||
| const LINT_BOF_WHITESPACE = 8; | const LINT_BOF_WHITESPACE = 8; | ||||
| const LINT_EOF_WHITESPACE = 9; | const LINT_EOF_WHITESPACE = 9; | ||||
| private $maxLineLength = 80; | private $maxLineLength = 80; | ||||
| public function getInfoName() { | public function getInfoName() { | ||||
| return pht('Basic Text Linter'); | return pht('Basic Text Linter'); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | final class ArcanistTextLinter extends ArcanistLinter { | ||||
| public function getLintNameMap() { | public function getLintNameMap() { | ||||
| return array( | return array( | ||||
| self::LINT_DOS_NEWLINE => pht('DOS Newlines'), | self::LINT_DOS_NEWLINE => pht('DOS Newlines'), | ||||
| self::LINT_TAB_LITERAL => pht('Tab Literal'), | self::LINT_TAB_LITERAL => pht('Tab Literal'), | ||||
| self::LINT_LINE_WRAP => pht('Line Too Long'), | self::LINT_LINE_WRAP => pht('Line Too Long'), | ||||
| self::LINT_EOF_NEWLINE => pht('File Does Not End in Newline'), | self::LINT_EOF_NEWLINE => pht('File Does Not End in Newline'), | ||||
| self::LINT_BAD_CHARSET => pht('Bad Charset'), | self::LINT_BAD_CHARSET => pht('Bad Charset'), | ||||
| self::LINT_TRAILING_WHITESPACE => pht('Trailing Whitespace'), | self::LINT_TRAILING_WHITESPACE => pht('Trailing Whitespace'), | ||||
| self::LINT_NO_COMMIT => pht('Explicit %s', '@no'.'commit'), | |||||
| self::LINT_BOF_WHITESPACE => pht('Leading Whitespace at BOF'), | self::LINT_BOF_WHITESPACE => pht('Leading Whitespace at BOF'), | ||||
| self::LINT_EOF_WHITESPACE => pht('Trailing Whitespace at EOF'), | self::LINT_EOF_WHITESPACE => pht('Trailing Whitespace at EOF'), | ||||
| ); | ); | ||||
| } | } | ||||
| public function lintPath($path) { | public function lintPath($path) { | ||||
| if (!strlen($this->getData($path))) { | if (!strlen($this->getData($path))) { | ||||
| // If the file is empty, don't bother; particularly, don't require | // If the file is empty, don't bother; particularly, don't require | ||||
| Show All 15 Lines | public function lintPath($path) { | ||||
| } | } | ||||
| $this->lintLineLength($path); | $this->lintLineLength($path); | ||||
| $this->lintEOFNewline($path); | $this->lintEOFNewline($path); | ||||
| $this->lintTrailingWhitespace($path); | $this->lintTrailingWhitespace($path); | ||||
| $this->lintBOFWhitespace($path); | $this->lintBOFWhitespace($path); | ||||
| $this->lintEOFWhitespace($path); | $this->lintEOFWhitespace($path); | ||||
| if ($this->getEngine()->getCommitHookMode()) { | |||||
| $this->lintNoCommit($path); | |||||
| } | |||||
| } | } | ||||
| protected function lintNewlines($path) { | protected function lintNewlines($path) { | ||||
| $data = $this->getData($path); | $data = $this->getData($path); | ||||
| $pos = strpos($this->getData($path), "\r"); | $pos = strpos($this->getData($path), "\r"); | ||||
| if ($pos !== false) { | if ($pos !== false) { | ||||
| $this->raiseLintAtOffset( | $this->raiseLintAtOffset( | ||||
| ▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | $this->raiseLintAtOffset( | ||||
| self::LINT_EOF_WHITESPACE, | self::LINT_EOF_WHITESPACE, | ||||
| pht( | pht( | ||||
| 'This file contains trailing whitespace at the end of the file. '. | 'This file contains trailing whitespace at the end of the file. '. | ||||
| 'This is unnecessary and should be avoided when possible.'), | 'This is unnecessary and should be avoided when possible.'), | ||||
| $string, | $string, | ||||
| ''); | ''); | ||||
| } | } | ||||
| private function lintNoCommit($path) { | |||||
| $data = $this->getData($path); | |||||
| $deadly = '@no'.'commit'; | |||||
| $offset = strpos($data, $deadly); | |||||
| if ($offset !== false) { | |||||
| $this->raiseLintAtOffset( | |||||
| $offset, | |||||
| self::LINT_NO_COMMIT, | |||||
| pht( | |||||
| 'This file is explicitly marked as "%s", which blocks commits.', | |||||
| $deadly), | |||||
| $deadly); | |||||
| } | |||||
| } | |||||
| } | } | ||||