[torque] Add linter rule for module constant naming convention

R=tebbi@chromium.org

Bug: v8:7793
Change-Id: I0314f1b45433c3023f803d67a056ffd6dbf83ce9
Reviewed-on: https://chromium-review.googlesource.com/1221212
Commit-Queue: Simon Zünd <szuend@google.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55832}
This commit is contained in:
Simon Zünd 2018-09-12 16:31:44 +02:00 committed by Commit Bot
parent 2d7e221d40
commit 87346debf0
3 changed files with 26 additions and 0 deletions

View File

@ -365,6 +365,13 @@ base::Optional<ParseResult> MakeTorqueBuiltinDeclaration(
base::Optional<ParseResult> MakeConstDeclaration(
ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
if (!IsValidModuleConstName(name)) {
std::stringstream sstream;
sstream << "Constant \"" << name << "\" doesn't follow "
<< "\"kUpperCamelCase\" naming convention.";
LintError(sstream.str());
}
auto type = child_results->NextAs<TypeExpression*>();
auto expression = child_results->NextAs<Expression*>();
Declaration* result =

View File

@ -100,6 +100,17 @@ bool ContainsUpperCase(const std::string& s) {
return std::any_of(s.begin(), s.end(), [](char c) { return isupper(c); });
}
// Torque has some module constants that are used like language level
// keywords, e.g.: 'True', 'Undefined', etc.
// These do not need to follow the default naming convention for constants.
bool IsKeywordLikeName(const std::string& s) {
static const std::vector<std::string> keyword_like_constants{
"True", "False", "Hole", "Null", "Undefined"};
return std::find(keyword_like_constants.begin(), keyword_like_constants.end(),
s) != keyword_like_constants.end();
}
} // namespace
bool IsLowerCamelCase(const std::string& s) {
@ -117,6 +128,13 @@ bool IsSnakeCase(const std::string& s) {
return !ContainsUpperCase(s);
}
bool IsValidModuleConstName(const std::string& s) {
if (s.empty()) return false;
if (IsKeywordLikeName(s)) return true;
return s[0] == 'k' && IsUpperCamelCase(s.substr(1));
}
std::string CamelifyString(const std::string& underscore_string) {
std::string result;
bool word_beginning = true;

View File

@ -38,6 +38,7 @@ void LintError(const std::string& error);
bool IsLowerCamelCase(const std::string& s);
bool IsUpperCamelCase(const std::string& s);
bool IsSnakeCase(const std::string& s);
bool IsValidModuleConstName(const std::string& s);
std::string CamelifyString(const std::string& underscore_string);
std::string DashifyString(const std::string& underscore_string);