mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-21 22:32:41 +01:00
Implement lint check that filename matches only declared interface/class.
Differential Revision: 201593 Reviewed By: epriestley
This commit is contained in:
parent
59f48f7901
commit
026a322dd2
4 changed files with 49 additions and 0 deletions
|
@ -38,6 +38,8 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
const LINT_TODO_COMMENT = 16;
|
const LINT_TODO_COMMENT = 16;
|
||||||
const LINT_EXIT_EXPRESSION = 17;
|
const LINT_EXIT_EXPRESSION = 17;
|
||||||
const LINT_COMMENT_STYLE = 18;
|
const LINT_COMMENT_STYLE = 18;
|
||||||
|
const LINT_CLASS_FILENAME_MISMATCH = 19;
|
||||||
|
|
||||||
|
|
||||||
public function getLintNameMap() {
|
public function getLintNameMap() {
|
||||||
return array(
|
return array(
|
||||||
|
@ -59,6 +61,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
self::LINT_TODO_COMMENT => 'TODO Comment',
|
self::LINT_TODO_COMMENT => 'TODO Comment',
|
||||||
self::LINT_EXIT_EXPRESSION => 'Exit Used as Expression',
|
self::LINT_EXIT_EXPRESSION => 'Exit Used as Expression',
|
||||||
self::LINT_COMMENT_STYLE => 'Comment Style',
|
self::LINT_COMMENT_STYLE => 'Comment Style',
|
||||||
|
self::LINT_CLASS_FILENAME_MISMATCH => 'Class-Filename Mismatch',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,6 +131,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
$this->lintUndeclaredVariables($root);
|
$this->lintUndeclaredVariables($root);
|
||||||
$this->lintArrayIndexWhitespace($root);
|
$this->lintArrayIndexWhitespace($root);
|
||||||
$this->lintHashComments($root);
|
$this->lintHashComments($root);
|
||||||
|
$this->lintPrimaryDeclarationFilenameMatch($root);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function lintHashComments($root) {
|
protected function lintHashComments($root) {
|
||||||
|
@ -880,6 +884,48 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lint that if the file declares exactly one interface or class,
|
||||||
|
* the name of the file matches the name of the class,
|
||||||
|
* unless the classname is funky like an XHP element.
|
||||||
|
*/
|
||||||
|
private function lintPrimaryDeclarationFilenameMatch($root) {
|
||||||
|
$classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION');
|
||||||
|
$interfaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION');
|
||||||
|
|
||||||
|
if (count($classes) + count($interfaces) != 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$declarations = count($classes) ? $classes : $interfaces;
|
||||||
|
$declarations->rewind();
|
||||||
|
$declaration = $declarations->current();
|
||||||
|
|
||||||
|
$decl_name = $declaration->getChildByIndex(1);
|
||||||
|
$decl_string = $decl_name->getConcreteString();
|
||||||
|
|
||||||
|
//Exclude strangely named classes.
|
||||||
|
if (!preg_match('/\w+/', $decl_string)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rename = $decl_string.'.php';
|
||||||
|
|
||||||
|
$path = $this->getActivePath();
|
||||||
|
$filename = basename($path);
|
||||||
|
|
||||||
|
if ($rename == $filename) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->raiseLintAtNode(
|
||||||
|
$decl_name,
|
||||||
|
self::LINT_CLASS_FILENAME_MISMATCH,
|
||||||
|
"The name of this file differs from the name of the class or interface ".
|
||||||
|
"it declares. Rename the file to '{$rename}'."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
protected function raiseLintAtToken(
|
protected function raiseLintAtToken(
|
||||||
XHPASTToken $token,
|
XHPASTToken $token,
|
||||||
$code,
|
$code,
|
||||||
|
|
|
@ -35,6 +35,7 @@ warning:14:10
|
||||||
warning:14:19
|
warning:14:19
|
||||||
warning:15:12
|
warning:15:12
|
||||||
warning:15:15
|
warning:15:15
|
||||||
|
error:16:7 XHP19 Class-Filename Mismatch
|
||||||
warning:17:21
|
warning:17:21
|
||||||
warning:17:24
|
warning:17:24
|
||||||
warning:20:10
|
warning:20:10
|
||||||
|
|
|
@ -5,4 +5,5 @@ class Platypus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~~~~~~~~~~
|
~~~~~~~~~~
|
||||||
|
error:2:7 XHP19 Class-Filename Mismatch
|
||||||
error:3:19
|
error:3:19
|
|
@ -10,4 +10,5 @@ class A {
|
||||||
}
|
}
|
||||||
|
|
||||||
~~~~~~~~~~
|
~~~~~~~~~~
|
||||||
|
error:3:7 XHP19 Class-Filename Mismatch
|
||||||
error:8:5 Use of $this in a static method.
|
error:8:5 Use of $this in a static method.
|
||||||
|
|
Loading…
Reference in a new issue