Commit 448a1d56 authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Add linter rule for type naming convention

Drive-by change: Add helper method for reporting naming
convention errors and refactor existing call sites.

R=tebbi@chromium.org

Bug: v8:7793
Change-Id: I7315b935229ffb7557fa55ebcb222fc91124aab7
Reviewed-on: https://chromium-review.googlesource.com/1223831Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Simon Zünd <szuend@google.com>
Cr-Commit-Position: refs/heads/master@{#55847}
parent 4a751684
......@@ -186,10 +186,7 @@ T* MakeNode(Args... 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());
NamingConventionError("Generic parameter", parameter, "UpperCamelCase");
}
}
}
......@@ -245,10 +242,7 @@ 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());
NamingConventionError("Parameter", pair.name, "lowerCamelCase");
}
result.names.push_back(std::move(pair.name));
......@@ -308,10 +302,7 @@ base::Optional<ParseResult> MakeTorqueMacroDeclaration(
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());
NamingConventionError("Macro", name, "UpperCamelCase");
}
auto generic_parameters = child_results->NextAs<GenericParameters>();
......@@ -338,10 +329,7 @@ base::Optional<ParseResult> MakeTorqueBuiltinDeclaration(
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());
NamingConventionError("Builtin", name, "UpperCamelCase");
}
auto generic_parameters = child_results->NextAs<GenericParameters>();
......@@ -366,10 +354,7 @@ 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());
NamingConventionError("Constant", name, "kUpperCamelCase");
}
auto type = child_results->NextAs<TypeExpression*>();
......@@ -400,6 +385,9 @@ base::Optional<ParseResult> MakeTypeAliasDeclaration(
base::Optional<ParseResult> MakeTypeDeclaration(
ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>();
if (!IsValidTypeName(name)) {
NamingConventionError("Type", name, "UpperCamelCase");
}
auto extends = child_results->NextAs<base::Optional<std::string>>();
auto generates = child_results->NextAs<base::Optional<std::string>>();
auto constexpr_generates =
......@@ -414,10 +402,7 @@ 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());
NamingConventionError("Module", name, "snake_case");
}
auto declarations = child_results->NextAs<std::vector<Declaration*>>();
Declaration* result = MakeNode<ExplicitModuleDeclaration>(
......@@ -653,10 +638,7 @@ base::Optional<ParseResult> MakeVarDeclarationStatement(
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());
NamingConventionError("Variable", name, "lowerCamelCase");
}
auto type = child_results->NextAs<TypeExpression*>();
......
......@@ -88,6 +88,14 @@ void LintError(const std::string& error) {
std::cerr << CurrentPositionAsString() << ": Lint error: " << error << "\n";
}
void NamingConventionError(const std::string& type, const std::string& name,
const std::string& convention) {
std::stringstream sstream;
sstream << type << " \"" << name << "\" doesn't follow \"" << convention
<< "\" naming convention.";
LintError(sstream.str());
}
namespace {
bool ContainsUnderscore(const std::string& s) {
......@@ -111,6 +119,17 @@ bool IsKeywordLikeName(const std::string& s) {
s) != keyword_like_constants.end();
}
// Untagged/MachineTypes like 'int32', 'intptr' etc. follow a 'all-lowercase'
// naming convention and are those exempt from the normal type convention.
bool IsMachineType(const std::string& s) {
static const std::vector<std::string> machine_types{
"void", "never", "int32", "uint32", "int64", "intptr",
"uintptr", "float32", "float64", "bool", "string", "int31"};
return std::find(machine_types.begin(), machine_types.end(), s) !=
machine_types.end();
}
} // namespace
bool IsLowerCamelCase(const std::string& s) {
......@@ -135,6 +154,13 @@ bool IsValidModuleConstName(const std::string& s) {
return s[0] == 'k' && IsUpperCamelCase(s.substr(1));
}
bool IsValidTypeName(const std::string& s) {
if (s.empty()) return false;
if (IsMachineType(s)) return true;
return IsUpperCamelCase(s);
}
std::string CamelifyString(const std::string& underscore_string) {
std::string result;
bool word_beginning = true;
......
......@@ -35,10 +35,16 @@ class LintErrorStatus : public ContextualClass<LintErrorStatus> {
[[noreturn]] void ReportError(const std::string& error);
void LintError(const std::string& error);
// Prints a LintError with the format "{type} '{name}' doesn't follow
// '{convention}' naming convention".
void NamingConventionError(const std::string& type, const std::string& name,
const std::string& convention);
bool IsLowerCamelCase(const std::string& s);
bool IsUpperCamelCase(const std::string& s);
bool IsSnakeCase(const std::string& s);
bool IsValidModuleConstName(const std::string& s);
bool IsValidTypeName(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