mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-01 02:42:05 +00:00
linter: refactorings
This commit is contained in:
@@ -13,7 +13,7 @@ import
|
||||
strutils, os, intsets, strtabs
|
||||
|
||||
import options, ast, astalgo, msgs, semdata, ropes, idents,
|
||||
lineinfos, pathutils
|
||||
lineinfos, pathutils, wordrecg
|
||||
|
||||
const
|
||||
Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
|
||||
@@ -22,28 +22,6 @@ proc identLen*(line: string, start: int): int =
|
||||
while start+result < line.len and line[start+result] in Letters:
|
||||
inc result
|
||||
|
||||
type
|
||||
StyleCheck* {.pure.} = enum None, Warn, Auto
|
||||
|
||||
proc overwriteFiles*(conf: ConfigRef) =
|
||||
let doStrip = options.getConfigVar(conf, "pretty.strip").normalize == "on"
|
||||
for i in 0 .. high(conf.m.fileInfos):
|
||||
if conf.m.fileInfos[i].dirty and
|
||||
(FileIndex(i) == conf.projectMainIdx):
|
||||
let newFile = if false: conf.m.fileInfos[i].fullpath
|
||||
else: conf.m.fileInfos[i].fullpath.changeFileExt(".pretty.nim")
|
||||
try:
|
||||
var f = open(newFile.string, fmWrite)
|
||||
for line in conf.m.fileInfos[i].lines:
|
||||
if doStrip:
|
||||
f.write line.strip(leading = false, trailing = true)
|
||||
else:
|
||||
f.write line
|
||||
f.write(conf.m.fileInfos[i], "\L")
|
||||
f.close
|
||||
except IOError:
|
||||
rawMessage(conf, errGenerated, "cannot open file: " & newFile.string)
|
||||
|
||||
proc `=~`(s: string, a: openArray[string]): bool =
|
||||
for x in a:
|
||||
if s.startsWith(x): return true
|
||||
@@ -95,7 +73,7 @@ proc beautifyName(s: string, k: TSymKind): string =
|
||||
|
||||
proc differ*(line: string, a, b: int, x: string): string =
|
||||
let y = line[a..b]
|
||||
if cmpIgnoreStyle(y, x) == 0 and y != x:
|
||||
if y != x and cmpIgnoreStyle(y, x) == 0:
|
||||
result = y
|
||||
else:
|
||||
result = ""
|
||||
@@ -125,8 +103,6 @@ proc nep1CheckDefImpl(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
|
||||
template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
|
||||
if {optStyleHint, optStyleError} * conf.globalOptions != {}:
|
||||
nep1CheckDefImpl(conf, info, s, k)
|
||||
when defined(nimfix):
|
||||
if gStyleCheck != StyleCheck.None: styleCheckDefImpl(conf, cache, info, s, k)
|
||||
|
||||
template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym) =
|
||||
styleCheckDef(conf, info, s, s.kind)
|
||||
@@ -159,32 +135,7 @@ proc styleCheckUse*(conf: ConfigRef; info: TLineInfo; s: PSym) =
|
||||
if oldName.len > 0:
|
||||
lintReport(conf, info, newName, oldName)
|
||||
|
||||
proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; pragmaName: string) =
|
||||
const inMixedCase = [
|
||||
"noSideEffect", "importCompilerProc", "incompleteStruct", "requiresInit",
|
||||
"sideEffect", "compilerProc", "lineDir", "stackTrace", "lineTrace",
|
||||
"rangeChecks", "boundChecks",
|
||||
"overflowChecks", "nilChecks",
|
||||
"floatChecks", "nanChecks", "infChecks", "moveChecks",
|
||||
"nonReloadable", "executeOnReload",
|
||||
"deadCodeElim",
|
||||
"compileTime", "noInit", "fieldChecks",
|
||||
"linearScanEnd",
|
||||
"computedGoto", "injectStmt",
|
||||
"asmNoStackframe", "implicitStatic", "codegenDecl", "liftLocals"
|
||||
]
|
||||
let name = pragmaName.normalize
|
||||
for x in inMixedCase:
|
||||
if x.normalize == name:
|
||||
if pragmaName != x:
|
||||
lintReport(conf, info, x, pragmaName)
|
||||
return
|
||||
|
||||
let wanted = pragmaName.toLowerAscii.replace("_", "")
|
||||
proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; w: TSpecialWord; pragmaName: string) =
|
||||
let wanted = canonPragmaSpelling(w)
|
||||
if pragmaName != wanted:
|
||||
lintReport(conf, info, wanted, pragmaName)
|
||||
|
||||
|
||||
template styleCheckUse*(info: TLineInfo; s: PSym) =
|
||||
when defined(nimfix):
|
||||
if gStyleCheck != StyleCheck.None: styleCheckUseImpl(conf, info, s)
|
||||
|
||||
@@ -778,7 +778,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
|
||||
let k = whichKeyword(ident)
|
||||
if k in validPragmas:
|
||||
if {optStyleHint, optStyleError} * c.config.globalOptions != {}:
|
||||
checkPragmaUse(c.config, key.info, ident.s)
|
||||
checkPragmaUse(c.config, key.info, k, ident.s)
|
||||
case k
|
||||
of wExportc:
|
||||
makeExternExport(c, sym, getOptionalStr(c, it, "$1"), it.info)
|
||||
|
||||
@@ -44,7 +44,7 @@ type
|
||||
wImportCompilerProc,
|
||||
wImportc, wExportc, wExportNims, wIncompleteStruct, wRequiresInit,
|
||||
wAlign, wNodecl, wPure, wSideeffect, wHeader,
|
||||
wNosideeffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib,
|
||||
wNoSideEffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib,
|
||||
wCompilerproc, wCore, wProcVar, wBase, wUsed,
|
||||
wFatal, wError, wWarning, wHint, wLine, wPush, wPop, wDefine, wUndef,
|
||||
wLinedir, wStacktrace, wLinetrace, wLink, wCompile,
|
||||
@@ -184,3 +184,37 @@ proc findStr*(a: openArray[string], s: string): int =
|
||||
if cmpIgnoreStyle(a[i], s) == 0:
|
||||
return i
|
||||
result = - 1
|
||||
|
||||
proc canonPragmaSpelling*(w: TSpecialWord): string =
|
||||
case w
|
||||
of wNoSideEffect: "noSideEffect"
|
||||
of wImportCompilerProc: "importCompilerProc"
|
||||
of wIncompleteStruct: "incompleteStruct"
|
||||
of wRequiresInit: "requiresInit"
|
||||
of wSideEffect: "sideEffect"
|
||||
of wCompilerProc: "compilerProc"
|
||||
of wLineDir: "lineDir"
|
||||
of wStackTrace: "stackTrace"
|
||||
of wLineTrace: "lineTrace"
|
||||
of wRangeChecks: "rangeChecks"
|
||||
of wBoundChecks: "boundChecks"
|
||||
of wOverflowChecks: "overflowChecks"
|
||||
of wNilChecks: "nilChecks"
|
||||
of wFloatChecks: "floatChecks"
|
||||
of wNanChecks: "nanChecks"
|
||||
of wInfChecks: "infChecks"
|
||||
of wMoveChecks: "moveChecks"
|
||||
of wNonReloadable: "nonReloadable"
|
||||
of wExecuteOnReload: "executeOnReload"
|
||||
of wDeadCodeElimUnused: "deadCodeElim"
|
||||
of wCompileTime: "compileTime"
|
||||
of wNoInit: "noInit"
|
||||
of wFieldChecks: "fieldChecks"
|
||||
of wLinearScanEnd: "linearScanEnd"
|
||||
of wComputedGoto: "computedGoto"
|
||||
of wInjectStmt: "injectStmt"
|
||||
of wAsmNoStackFrame: "asmNoStackframe"
|
||||
of wImplicitStatic: "implicitStatic"
|
||||
of wCodegenDecl: "codegenDecl"
|
||||
of wLiftLocals: "liftLocals"
|
||||
else: specialWords[w]
|
||||
|
||||
Reference in New Issue
Block a user