Skip to content

Commit

Permalink
fixes nim-lang#15848 [backport:1.2] (nim-lang#17959)
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored and PMunch committed Mar 28, 2022
1 parent 04d4f91 commit 94cd570
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion compiler/linter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
1 change: 1 addition & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions doc/advopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/stylecheck/tusages.nim
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 94cd570

Please sign in to comment.