Commit 5c244ca5 authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Add Lint errors for naming convention violations

This CL adds naming convention errors for:
  - Variable names
  - Torque Macro/Builtin names
  - Parameter names

R=tebbi@chromium.org

Bug: v8:7793
Change-Id: Icfe20e53524234f0b4534f0102aff6b14121dfbc
Reviewed-on: https://chromium-review.googlesource.com/1213184
Commit-Queue: Simon Zünd <szuend@google.com>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55758}
parent 5a9f0556
......@@ -233,6 +233,13 @@ base::Optional<ParseResult> MakeParameterListFromNameAndTypeList(
}
ParameterList result;
for (NameAndTypeExpression& pair : params) {
if (!IsLowerCamelCase(pair.name)) {
std::stringstream sstream;
sstream << "Parameter \"" << pair.name << "\" doesn't follow "
<< "\"lowerCamelCase\" naming convention.";
LintError(sstream.str());
}
result.names.push_back(std::move(pair.name));
result.types.push_back(pair.type);
}
......@@ -287,6 +294,13 @@ base::Optional<ParseResult> MakeTorqueMacroDeclaration(
ParseResultIterator* child_results) {
auto operator_name = child_results->NextAs<base::Optional<std::string>>();
auto name = child_results->NextAs<std::string>();
if (!IsUpperCamelCase(name)) {
std::stringstream sstream;
sstream << "Macro \"" << name << "\" doesn't follow "
<< "\"UpperCamelCase\" naming convention.";
LintError(sstream.str());
}
auto generic_parameters = child_results->NextAs<GenericParameters>();
auto args = child_results->NextAs<ParameterList>();
auto return_type = child_results->NextAs<TypeExpression*>();
......@@ -308,6 +322,13 @@ base::Optional<ParseResult> MakeTorqueBuiltinDeclaration(
ParseResultIterator* child_results) {
auto javascript_linkage = child_results->NextAs<bool>();
auto name = child_results->NextAs<std::string>();
if (!IsUpperCamelCase(name)) {
std::stringstream sstream;
sstream << "Builtin \"" << name << "\" doesn't follow "
<< "\"UpperCamelCase\" naming convention.";
LintError(sstream.str());
}
auto generic_parameters = child_results->NextAs<GenericParameters>();
auto args = child_results->NextAs<ParameterList>();
auto return_type = child_results->NextAs<TypeExpression*>();
......@@ -599,6 +620,13 @@ base::Optional<ParseResult> MakeVarDeclarationStatement(
bool const_qualified = kind == "const";
if (!const_qualified) DCHECK_EQ("let", kind);
auto name = child_results->NextAs<std::string>();
if (!IsLowerCamelCase(name)) {
std::stringstream sstream;
sstream << "Variable \"" << name << "\" doesn't follow "
<< "\"lowerCamelCase\" naming convention.";
LintError(sstream.str());
}
auto type = child_results->NextAs<TypeExpression*>();
base::Optional<Expression*> initializer;
if (child_results->HasNext())
......
......@@ -81,6 +81,29 @@ std::string CurrentPositionAsString() {
std::abort();
}
void LintError(const std::string& error) {
std::cerr << CurrentPositionAsString() << ": Lint error: " << error << "\n";
}
namespace {
bool ContainsUnderscore(const std::string& s) {
if (s.empty()) return false;
return s.find("_") != std::string::npos;
}
} // namespace
bool IsLowerCamelCase(const std::string& s) {
if (s.empty()) return false;
return islower(s[0]) && !ContainsUnderscore(s);
}
bool IsUpperCamelCase(const std::string& s) {
if (s.empty()) return false;
return isupper(s[0]) && !ContainsUnderscore(s);
}
std::string CamelifyString(const std::string& underscore_string) {
std::string result;
bool word_beginning = true;
......
......@@ -21,6 +21,10 @@ std::string StringLiteralUnquote(const std::string& s);
std::string StringLiteralQuote(const std::string& s);
[[noreturn]] void ReportError(const std::string& error);
void LintError(const std::string& error);
bool IsLowerCamelCase(const std::string& s);
bool IsUpperCamelCase(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