Commit 84d3fe1e authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Add linter rules for module names and generic variables

R=tebbi@chromium.org

Bug: v8:7793
Change-Id: I8db34b56e3dd3ac24ca7e368054829befb13397f
Reviewed-on: https://chromium-review.googlesource.com/1221187Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Simon Zünd <szuend@google.com>
Cr-Commit-Position: refs/heads/master@{#55824}
parent 93cbdab3
......@@ -183,6 +183,17 @@ T* MakeNode(Args... args) {
new T(CurrentSourcePosition::Get(), std::move(args)...)));
}
void LintGenericParameters(const GenericParameters& parameters) {
for (const std::string& parameter : parameters) {
if (!IsUpperCamelCase(parameter)) {
std::stringstream sstream;
sstream << "Generic parameter \"" << parameter << "\" doesn't follow "
<< "\"UpperCamelCase\" naming convention.";
LintError(sstream.str());
}
}
}
base::Optional<ParseResult> MakeCall(ParseResultIterator* child_results) {
auto callee = child_results->NextAs<std::string>();
auto generic_args = child_results->NextAs<TypeList>();
......@@ -276,6 +287,8 @@ base::Optional<ParseResult> MakeExternalMacro(
auto operator_name = child_results->NextAs<base::Optional<std::string>>();
auto name = child_results->NextAs<std::string>();
auto generic_parameters = child_results->NextAs<GenericParameters>();
LintGenericParameters(generic_parameters);
auto args = child_results->NextAs<ParameterList>();
auto return_type = child_results->NextAs<TypeExpression*>();
auto labels = child_results->NextAs<LabelAndTypesVector>();
......@@ -302,6 +315,8 @@ base::Optional<ParseResult> MakeTorqueMacroDeclaration(
}
auto generic_parameters = child_results->NextAs<GenericParameters>();
LintGenericParameters(generic_parameters);
auto args = child_results->NextAs<ParameterList>();
auto return_type = child_results->NextAs<TypeExpression*>();
auto labels = child_results->NextAs<LabelAndTypesVector>();
......@@ -330,6 +345,8 @@ base::Optional<ParseResult> MakeTorqueBuiltinDeclaration(
}
auto generic_parameters = child_results->NextAs<GenericParameters>();
LintGenericParameters(generic_parameters);
auto args = child_results->NextAs<ParameterList>();
auto return_type = child_results->NextAs<TypeExpression*>();
auto body = child_results->NextAs<base::Optional<Statement*>>();
......@@ -389,6 +406,12 @@ base::Optional<ParseResult> MakeTypeDeclaration(
base::Optional<ParseResult> MakeExplicitModuleDeclaration(
ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
if (!IsSnakeCase(name)) {
std::stringstream sstream;
sstream << "Module \"" << name << "\" doesn't follow "
<< "\"snake_case\" naming convention.";
LintError(sstream.str());
}
auto declarations = child_results->NextAs<std::vector<Declaration*>>();
Declaration* result = MakeNode<ExplicitModuleDeclaration>(
std::move(name), std::move(declarations));
......@@ -424,6 +447,8 @@ base::Optional<ParseResult> MakeExternalBuiltin(
auto js_linkage = child_results->NextAs<bool>();
auto name = child_results->NextAs<std::string>();
auto generic_parameters = child_results->NextAs<GenericParameters>();
LintGenericParameters(generic_parameters);
auto args = child_results->NextAs<ParameterList>();
auto return_type = child_results->NextAs<TypeExpression*>();
BuiltinDeclaration* builtin =
......
......@@ -95,6 +95,11 @@ bool ContainsUnderscore(const std::string& s) {
return s.find("_") != std::string::npos;
}
bool ContainsUpperCase(const std::string& s) {
if (s.empty()) return false;
return std::any_of(s.begin(), s.end(), [](char c) { return isupper(c); });
}
} // namespace
bool IsLowerCamelCase(const std::string& s) {
......@@ -107,6 +112,11 @@ bool IsUpperCamelCase(const std::string& s) {
return isupper(s[0]) && !ContainsUnderscore(s);
}
bool IsSnakeCase(const std::string& s) {
if (s.empty()) return false;
return !ContainsUpperCase(s);
}
std::string CamelifyString(const std::string& underscore_string) {
std::string result;
bool word_beginning = true;
......
......@@ -37,6 +37,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);
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