Commit 87346deb authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[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: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55832}
parent 2d7e221d
......@@ -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 =
......
......@@ -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;
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment