Changeset View
Changeset View
Standalone View
Standalone View
arcanist/linter/PythonMutableDefaultLinter.php
- This file was added.
<?php | |||||
/** | |||||
* Make sure we don't use mutable default arguments | |||||
* https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments | |||||
*/ | |||||
final class PythonMutableDefaultLinter extends ArcanistLinter { | |||||
const MUTABLE_DEFAULT_FOUND = 1; | |||||
public function getInfoName() { | |||||
return 'lint-python-mutable-default'; | |||||
} | |||||
public function getInfoDescription() { | |||||
return pht('Make sure we don\'t use mutable default arguments '. | |||||
'in python.'); | |||||
} | |||||
public function getLinterName() { | |||||
return 'PYTHON_MUTABLE_DEFAULT'; | |||||
} | |||||
public function getLinterConfigurationName() { | |||||
return 'lint-python-mutable-default'; | |||||
} | |||||
public function getLintSeverityMap() { | |||||
return array( | |||||
self::MUTABLE_DEFAULT_FOUND => ArcanistLintSeverity::SEVERITY_WARNING, | |||||
); | |||||
} | |||||
public function getLintNameMap() { | |||||
return array( | |||||
self::MUTABLE_DEFAULT_FOUND => pht('Mutable default arguments should '. | |||||
'generally not be used in python. If you want to use them anyway, '. | |||||
'please add comments about what you are trying to achieve.'), | |||||
); | |||||
} | |||||
public function lintPath($path) { | |||||
$path = Filesystem::resolvePath($path, $this->getProjectRoot()); | |||||
$fileContent = Filesystem::readFile($path); | |||||
// Match all function definitions containing list, dict or set literals | |||||
// used as default arguments. | |||||
$pattern = "/[ ]*def \w+\([\w, \n=\(\)\"']+=\s*(\[|\{)[^:]+:/"; | |||||
if (preg_match_all($pattern, $fileContent, $matches, | |||||
PREG_OFFSET_CAPTURE)) { | |||||
foreach ($matches[0] as $match) { | |||||
list($fullLine, $offset) = $match; | |||||
list($before) = str_split($fileContent, $offset); | |||||
$numLines = strlen($before) - strlen(str_replace("\n", "", $before)); | |||||
$lineNumber = $numLines + 1; | |||||
return $this->raiseLintAtPath( | |||||
self::MUTABLE_DEFAULT_FOUND, | |||||
pht("Found mutable default argument in function, at line ". | |||||
$lineNumber.":\n\n".$fullLine)); | |||||
} | |||||
} | |||||
} | |||||
} |