|
| 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, 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 | +parseEditorConfig :: String -> FilePath -> String |
| 42 | +parseEditorConfig contents name = |
| 43 | + unlines . concatMap sectionDirectives $ sections |
| 44 | + where |
| 45 | + ls = lines contents |
| 46 | + sections = splitSections ls |
| 47 | + |
| 48 | + splitSections [] = [] |
| 49 | + splitSections (l:rest) = |
| 50 | + case parseHeader l of |
| 51 | + Just pat -> |
| 52 | + let (body, rest') = break (isJust . parseHeader) rest |
| 53 | + in (pat, body) : splitSections rest' |
| 54 | + Nothing -> splitSections rest |
| 55 | + |
| 56 | + parseHeader l = |
| 57 | + let t = trim (stripComment l) |
| 58 | + in case t of |
| 59 | + ('[':cs@(_:_)) | last cs == ']' -> Just (init cs) |
| 60 | + _ -> Nothing |
| 61 | + |
| 62 | + sectionDirectives (pat, body) = |
| 63 | + if matchesGlob pat name |
| 64 | + then mapMaybe toDirective body |
| 65 | + else [] |
| 66 | + |
| 67 | + toDirective l = |
| 68 | + let t = trim (stripComment l) |
| 69 | + in case break (== '=') t of |
| 70 | + (key, '=':value) -> |
| 71 | + let key' = trim key |
| 72 | + value' = trim value |
| 73 | + in if "shellcheck." `isPrefixOf` key' |
| 74 | + then Just (drop (length "shellcheck.") key' ++ "=" ++ value') |
| 75 | + else Nothing |
| 76 | + _ -> Nothing |
| 77 | + |
| 78 | + stripComment = takeWhile (\c -> c /= '#' && c /= ';') |
| 79 | + |
| 80 | +trim :: String -> String |
| 81 | +trim = dropWhileEnd isSpace . dropWhile isSpace |
| 82 | + |
| 83 | +-- Does the (basename of the) file match the given EditorConfig glob? |
| 84 | +matchesGlob :: String -> FilePath -> Bool |
| 85 | +matchesGlob pattern name = |
| 86 | + name `matches` mkRegex (globToRegexString pattern) |
| 87 | + |
| 88 | +-- Translate an EditorConfig glob pattern into an anchored regex string. |
| 89 | +globToRegexString :: String -> String |
| 90 | +globToRegexString pattern = "^" ++ go pattern ++ "$" |
| 91 | + where |
| 92 | + go [] = "" |
| 93 | + go ('*':'*':rest) = ".*" ++ go rest |
| 94 | + go ('*':rest) = "[^/]*" ++ go rest |
| 95 | + go ('?':rest) = "[^/]" ++ go rest |
| 96 | + go ('[':rest) = |
| 97 | + let (cls, rest') = break (== ']') rest |
| 98 | + in case rest' of |
| 99 | + (']':rest'') -> "[" ++ translateClass cls ++ "]" ++ go rest'' |
| 100 | + _ -> "\\[" ++ go rest |
| 101 | + go ('{':rest) = |
| 102 | + let (body, rest') = break (== '}') rest |
| 103 | + in case rest' of |
| 104 | + ('}':rest'') -> |
| 105 | + "(" ++ intercalate "|" (map go (splitCommas body)) ++ ")" ++ go rest'' |
| 106 | + _ -> "\\{" ++ go rest |
| 107 | + go (c:rest) |
| 108 | + | c `elem` regexSpecials = ['\\', c] ++ go rest |
| 109 | + | otherwise = c : go rest |
| 110 | + |
| 111 | + regexSpecials = ".\\+()^$|" |
| 112 | + |
| 113 | + translateClass ('!':cs) = '^' : escapeClass cs |
| 114 | + translateClass cs = escapeClass cs |
| 115 | + escapeClass = concatMap (\c -> if c == '\\' then "\\\\" else [c]) |
| 116 | + |
| 117 | + splitCommas s = |
| 118 | + case break (== ',') s of |
| 119 | + (before, ',':after) -> before : splitCommas after |
| 120 | + (before, "") -> [before] |
| 121 | + (before, after) -> [before ++ after] |
| 122 | + |
| 123 | +prop_globStar = matchesGlob "*.ebuild" "foo.ebuild" |
| 124 | +prop_globBraceExt = matchesGlob "*.{ebuild,eclass}" "foo.eclass" |
| 125 | +prop_globBraceExt2 = matchesGlob "*.{ebuild,eclass}" "foo.ebuild" |
| 126 | +prop_globBraceName = matchesGlob "{PKGBUILD,APKBUILD}" "PKGBUILD" |
| 127 | +prop_globBraceName2 = matchesGlob "{PKGBUILD,APKBUILD}" "APKBUILD" |
| 128 | +prop_globNoMatch = not $ matchesGlob "*.ebuild" "foo.txt" |
| 129 | +prop_globQuestion = matchesGlob "foo?.sh" "food.sh" |
| 130 | +prop_globClass = matchesGlob "foo[0-9].sh" "foo1.sh" |
| 131 | +prop_globClassNeg = not $ matchesGlob "foo[!0-9].sh" "foo1.sh" |
| 132 | + |
| 133 | +prop_parseEditorConfig1 = |
| 134 | + parseEditorConfig "[*.{ebuild,eclass}]\nshellcheck.shell=bash\nshellcheck.disable=SC2034\n" "foo.ebuild" |
| 135 | + == "shell=bash\ndisable=SC2034\n" |
| 136 | +prop_parseEditorConfig2 = |
| 137 | + parseEditorConfig "[*.{ebuild,eclass}]\nshellcheck.shell=bash\n" "foo.txt" == "" |
| 138 | +prop_parseEditorConfig3 = |
| 139 | + parseEditorConfig "[{PKGBUILD,APKBUILD}]\nshellcheck.disable=SC2034\n" "PKGBUILD" == "disable=SC2034\n" |
| 140 | +prop_parseEditorConfig4 = |
| 141 | + parseEditorConfig "root = true\n[*.sh]\nindent_style = space\nshellcheck.shell=bash\n" "foo.sh" |
| 142 | + == "shell=bash\n" |
| 143 | + |
| 144 | +return [] |
| 145 | +runTests = $quickCheckAll |
0 commit comments