summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2021-05-07 16:50:11 +0200
committerGitHub <noreply@github.com>2021-05-07 16:50:11 +0200
commit51f3ef6cb8ac9fbbc9533c1de2af219dc4ecabe7 (patch)
treed631917998fd8725b98cc7e2331061dff7db2c74
parent56068101f6aee4635c22ca10b3f3062f27bcc946 (diff)
downloadNim-51f3ef6cb8ac9fbbc9533c1de2af219dc4ecabe7.tar.gz
fixes #15848 [backport:1.2] (#17959)
-rw-r--r--changelog.md5
-rw-r--r--compiler/commands.nim3
-rw-r--r--compiler/linter.nim2
-rw-r--r--compiler/options.nim1
-rw-r--r--doc/advopt.txt2
-rw-r--r--tests/stylecheck/tusages.nim23
6 files changed, 34 insertions, 2 deletions
diff --git a/changelog.md b/changelog.md
index d1e04177d..29b3237d0 100644
--- a/changelog.md
+++ b/changelog.md
@@ -397,6 +397,11 @@
   nim r main # uses cached binary
   nim r main arg1 arg2 # ditto (runtime arguments are irrelevant)
 
+- The style checking of the compiler now supports a `--styleCheck:usages` switch. This switch
+  enforces that every symbol is written as it was declared, not enforcing
+  the official Nim style guide. To be enabled, this has to be combined either
+  with `--styleCheck:error` or `--styleCheck:hint`.
+
 ## Tool changes
 
 - The rst parser now supports markdown table syntax.
diff --git a/compiler/commands.nim b/compiler/commands.nim
index 32f56ad0e..16b85275c 100644
--- a/compiler/commands.nim
+++ b/compiler/commands.nim
@@ -150,7 +150,7 @@ template switchOn(arg: string): bool =
 proc processOnOffSwitch(conf: ConfigRef; op: TOptions, arg: string, pass: TCmdLinePass,
                         info: TLineInfo) =
   case arg.normalize
-  of "","on": conf.options.incl op
+  of "", "on": conf.options.incl op
   of "off": conf.options.excl op
   else: localError(conf, info, errOnOrOffExpectedButXFound % arg)
 
@@ -971,6 +971,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
     of "off": conf.globalOptions = conf.globalOptions - {optStyleHint, optStyleError}
     of "hint": conf.globalOptions = conf.globalOptions + {optStyleHint} - {optStyleError}
     of "error": conf.globalOptions = conf.globalOptions + {optStyleError}
+    of "usages": conf.globalOptions.incl optStyleUsages
     else: localError(conf, info, errOffHintsError % arg)
   of "showallmismatches":
     processOnOffSwitchG(conf, {optShowAllMismatches}, arg, pass, info)
diff --git a/compiler/linter.nim b/compiler/linter.nim
index 83dd84699..7059689cf 100644
--- a/compiler/linter.nim
+++ b/compiler/linter.nim
@@ -95,7 +95,7 @@ proc nep1CheckDefImpl(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
     lintReport(conf, info, beau, s.name.s)
 
 template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
-  if {optStyleHint, optStyleError} * conf.globalOptions != {}:
+  if {optStyleHint, optStyleError} * conf.globalOptions != {} and optStyleUsages notin conf.globalOptions:
     nep1CheckDefImpl(conf, info, s, k)
 
 template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym) =
diff --git a/compiler/options.nim b/compiler/options.nim
index 717001b2b..931458cb0 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -60,6 +60,7 @@ type                          # please make sure we have under 32 options
     optUseNimcache,           # save artifacts (including binary) in $nimcache
     optStyleHint,             # check that the names adhere to NEP-1
     optStyleError,            # enforce that the names adhere to NEP-1
+    optStyleUsages,           # only enforce consistent **usages** of the symbol
     optSkipSystemConfigFile,  # skip the system's cfg/nims config file
     optSkipProjConfigFile,    # skip the project's cfg/nims config file
     optSkipUserConfigFile,    # skip the users's cfg/nims config file
diff --git a/doc/advopt.txt b/doc/advopt.txt
index a783ebec5..e50fb243b 100644
--- a/doc/advopt.txt
+++ b/doc/advopt.txt
@@ -57,6 +57,8 @@ Advanced options:
                             produce hints or errors for Nim identifiers that
                             do not adhere to Nim's official style guide
                             https://nim-lang.org/docs/nep1.html
+  --styleCheck:usages       only enforce consistent spellings of identifiers,
+                            do not enforce the style on declarations
   --showAllMismatches:on|off
                             show all mismatching candidates in overloading
                             resolution
diff --git a/tests/stylecheck/tusages.nim b/tests/stylecheck/tusages.nim
new file mode 100644
index 000000000..f3b0d27cc
--- /dev/null
+++ b/tests/stylecheck/tusages.nim
@@ -0,0 +1,23 @@
+discard """
+  cmd: "nim c --styleCheck:error --styleCheck:usages $file"
+  errormsg: "'BAD_STYLE' should be: 'BADSTYLE'"
+  line: 20
+"""
+
+import strutils
+
+proc BADSTYLE(c: char) = discard
+
+proc toSnakeCase(s: string): string =
+  result = newStringOfCap(s.len + 3)
+  for i in 0..<s.len:
+    if s[i] in {'A'..'Z'}:
+      if i > 0 and s[i-1] in {'a'..'z'}:
+        result.add '_'
+      result.add toLowerAscii(s[i])
+    else:
+      result.add s[i]
+    BAD_STYLE(s[i])
+
+echo toSnakeCase("fooBarBaz Yes")
+