|
| 1 | +{- |
| 2 | + Copyright 2012-2024 Vidar Holen |
| 3 | +
|
| 4 | + This file is part of ShellCheck. |
| 5 | + https://www.shellcheck.net |
| 6 | +
|
| 7 | + ShellCheck is free software: you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation, either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + ShellCheck is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | +-} |
| 20 | + |
| 21 | +{-# LANGUAGE TemplateHaskell #-} |
| 22 | +-- Minimal support for reading shellcheck directives from EditorConfig |
| 23 | +-- style files (https://editorconfig.org/). Only the `shellcheck.*` keys |
| 24 | +-- of sections whose glob matches the file being checked are extracted, |
| 25 | +-- and turned into the same "key=value" directive syntax that is used in |
| 26 | +-- .shellcheckrc files. |
| 27 | +module ShellCheck.EditorConfig (parseEditorConfig, isEditorConfigRoot, globToRegexString, runTests) where |
| 28 | + |
| 29 | +import Data.Char |
| 30 | +import Data.List |
| 31 | +import Data.Maybe |
| 32 | + |
| 33 | +import ShellCheck.Regex |
| 34 | + |
| 35 | +import Test.QuickCheck |
| 36 | + |
| 37 | +-- Given the contents of an EditorConfig style file and the name of the |
| 38 | +-- file being checked, return the shellcheck directives (as a |
| 39 | +-- "key=value\n" delimited blob, suitable for feeding into the same |
| 40 | +-- parser as .shellcheckrc) found in matching sections. |
| 41 | +-- |
| 42 | +-- As per the EditorConfig spec, files are read top to bottom and |
| 43 | +-- properties from later sections override those from earlier ones |
| 44 | +-- (for the same key), so on conflicts the last matching section wins. |
| 45 | +parseEditorConfig :: String -> FilePath -> String |
| 46 | +parseEditorConfig contents name = |
| 47 | + unlines . map render . lastWins . concatMap sectionDirectives $ sections |
| 48 | + where |
| 49 | + render (key, value) = key ++ "=" ++ value |
| 50 | + |
| 51 | + -- Keep only the last occurrence of each key, preserving the |
| 52 | + -- relative order of the remaining (first-seen) entries. |
| 53 | + lastWins = reverse . nubBy (\a b -> fst a == fst b) . reverse |
| 54 | + |
| 55 | + ls = lines contents |
| 56 | + sections = splitSections ls |
| 57 | + |
| 58 | + splitSections [] = [] |
| 59 | + splitSections (l:rest) = |
| 60 | + case parseHeader l of |
| 61 | + Just pat -> |
| 62 | + let (body, rest') = break (isJust . parseHeader) rest |
| 63 | + in (pat, body) : splitSections rest' |
| 64 | + Nothing -> splitSections rest |
| 65 | + |
| 66 | + parseHeader l = |
| 67 | + let t = trim (stripComment l) |
| 68 | + in case t of |
| 69 | + ('[':cs@(_:_)) | last cs == ']' -> Just (init cs) |
| 70 | + _ -> Nothing |
| 71 | + |
| 72 | + sectionDirectives (pat, body) = |
| 73 | + if matchesGlob pat name |
| 74 | + then mapMaybe toDirective body |
| 75 | + else [] |
| 76 | + |
| 77 | + toDirective l = |
| 78 | + let t = trim (stripComment l) |
| 79 | + in case break (== '=') t of |
| 80 | + (key, '=':value) -> |
| 81 | + let key' = trim key |
| 82 | + value' = trim value |
| 83 | + in if "shellcheck." `isPrefixOf` key' |
| 84 | + then Just (drop (length "shellcheck.") key', value') |
| 85 | + else Nothing |
| 86 | + _ -> Nothing |
| 87 | + |
| 88 | + stripComment = takeWhile (\c -> c /= '#' && c /= ';') |
| 89 | + |
| 90 | +-- Does the top-level (pre-section) part of an EditorConfig file |
| 91 | +-- declare "root = true"? Per the spec, this stops the search for |
| 92 | +-- further EditorConfig files in parent directories. |
| 93 | +isEditorConfigRoot :: String -> Bool |
| 94 | +isEditorConfigRoot contents = |
| 95 | + any isRootTrue . takeWhile (not . isSectionHeader) $ lines contents |
| 96 | + where |
| 97 | + isSectionHeader l = |
| 98 | + case trim (stripComment l) of |
| 99 | + ('[':cs@(_:_)) -> last cs == ']' |
| 100 | + _ -> False |
| 101 | + |
| 102 | + isRootTrue l = |
| 103 | + case break (== '=') (trim (stripComment l)) of |
| 104 | + (key, '=':value) -> |
| 105 | + map toLower (trim key) == "root" && map toLower (trim value) == "true" |
| 106 | + _ -> False |
| 107 | + |
| 108 | + stripComment = takeWhile (\c -> c /= '#' && c /= ';') |
| 109 | + |
| 110 | +trim :: String -> String |
| 111 | +trim = dropWhileEnd isSpace . dropWhile isSpace |
| 112 | + |
| 113 | +-- Does the (relative path of the) file match the given EditorConfig glob? |
| 114 | +matchesGlob :: String -> FilePath -> Bool |
| 115 | +matchesGlob pattern name = |
| 116 | + name `matches` mkRegex (globToRegexString pattern) |
| 117 | + |
| 118 | +-- Translate an EditorConfig glob pattern into an anchored regex string. |
| 119 | +-- Per the spec, patterns without a path separator are matched against |
| 120 | +-- the file at any depth (as if prefixed with "**/"). |
| 121 | +globToRegexString :: String -> String |
| 122 | +globToRegexString pattern = "^" ++ prefix ++ go pattern ++ "$" |
| 123 | + where |
| 124 | + prefix = if '/' `elem` pattern then "" else "(.*/)?" |
| 125 | + |
| 126 | + go [] = "" |
| 127 | + go ('*':'*':rest) = ".*" ++ go rest |
| 128 | + go ('*':rest) = "[^/]*" ++ go rest |
| 129 | + go ('?':rest) = "[^/]" ++ go rest |
| 130 | + go ('[':rest) = |
| 131 | + let (cls, rest') = break (== ']') rest |
| 132 | + in case rest' of |
| 133 | + (']':rest'') -> "[" ++ translateClass cls ++ "]" ++ go rest'' |
| 134 | + _ -> "\\[" ++ go rest |
| 135 | + go ('{':rest) = |
| 136 | + let (body, rest') = break (== '}') rest |
| 137 | + in case rest' of |
| 138 | + ('}':rest'') -> |
| 139 | + "(" ++ intercalate "|" (map go (splitCommas body)) ++ ")" ++ go rest'' |
| 140 | + _ -> "\\{" ++ go rest |
| 141 | + go (c:rest) |
| 142 | + | c `elem` regexSpecials = ['\\', c] ++ go rest |
| 143 | + | otherwise = c : go rest |
| 144 | + |
| 145 | + regexSpecials = ".\\+()^$|" |
| 146 | + |
| 147 | + translateClass ('!':cs) = '^' : escapeClass cs |
| 148 | + translateClass cs = escapeClass cs |
| 149 | + escapeClass = concatMap (\c -> if c == '\\' then "\\\\" else [c]) |
| 150 | + |
| 151 | + splitCommas s = |
| 152 | + case break (== ',') s of |
| 153 | + (before, ',':after) -> before : splitCommas after |
| 154 | + (before, "") -> [before] |
| 155 | + (before, after) -> [before ++ after] |
| 156 | + |
| 157 | +prop_globStar = matchesGlob "*.ebuild" "foo.ebuild" |
| 158 | +prop_globBraceExt = matchesGlob "*.{ebuild,eclass}" "foo.eclass" |
| 159 | +prop_globBraceExt2 = matchesGlob "*.{ebuild,eclass}" "foo.ebuild" |
| 160 | +prop_globBraceName = matchesGlob "{PKGBUILD,APKBUILD}" "PKGBUILD" |
| 161 | +prop_globBraceName2 = matchesGlob "{PKGBUILD,APKBUILD}" "APKBUILD" |
| 162 | +prop_globNoMatch = not $ matchesGlob "*.ebuild" "foo.txt" |
| 163 | +prop_globQuestion = matchesGlob "foo?.sh" "food.sh" |
| 164 | +prop_globClass = matchesGlob "foo[0-9].sh" "foo1.sh" |
| 165 | +prop_globClassNeg = not $ matchesGlob "foo[!0-9].sh" "foo1.sh" |
| 166 | +-- Patterns without a path separator should match at any depth. |
| 167 | +prop_globAnyDepth = matchesGlob "*.sh" "sub/dir/foo.sh" |
| 168 | +prop_globAnyDepthPlain = matchesGlob "foo" "sub/foo" |
| 169 | +-- Patterns with a path separator are only matched against the full |
| 170 | +-- relative path. |
| 171 | +prop_globWithSlashNoMatch = not $ matchesGlob "sub/*.sh" "other/foo.sh" |
| 172 | +prop_globWithSlashMatch = matchesGlob "sub/*.sh" "sub/foo.sh" |
| 173 | + |
| 174 | +prop_parseEditorConfig1 = |
| 175 | + parseEditorConfig "[*.{ebuild,eclass}]\nshellcheck.shell=bash\nshellcheck.disable=SC2034\n" "foo.ebuild" |
| 176 | + == "shell=bash\ndisable=SC2034\n" |
| 177 | +prop_parseEditorConfig2 = |
| 178 | + parseEditorConfig "[*.{ebuild,eclass}]\nshellcheck.shell=bash\n" "foo.txt" == "" |
| 179 | +prop_parseEditorConfig3 = |
| 180 | + parseEditorConfig "[{PKGBUILD,APKBUILD}]\nshellcheck.disable=SC2034\n" "PKGBUILD" == "disable=SC2034\n" |
| 181 | +prop_parseEditorConfig4 = |
| 182 | + parseEditorConfig "root = true\n[*.sh]\nindent_style = space\nshellcheck.shell=bash\n" "foo.sh" |
| 183 | + == "shell=bash\n" |
| 184 | +-- A later, more specific section overrides an earlier, more general |
| 185 | +-- one for the same key. |
| 186 | +prop_parseEditorConfig5 = |
| 187 | + parseEditorConfig "[*]\nshellcheck.shell=sh\n\n[foo]\nshellcheck.shell=bash\n" "foo" |
| 188 | + == "shell=bash\n" |
| 189 | +-- Non-conflicting keys from earlier and later sections are all kept. |
| 190 | +prop_parseEditorConfig6 = |
| 191 | + parseEditorConfig "[*]\nshellcheck.shell=sh\n\n[foo]\nshellcheck.disable=SC2034\n" "foo" |
| 192 | + == "shell=sh\ndisable=SC2034\n" |
| 193 | + |
| 194 | +return [] |
| 195 | +runTests = $quickCheckAll |
0 commit comments