-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Support editorconfig #3513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Freed-Wu
wants to merge
2
commits into
koalaman:master
Choose a base branch
from
Freed-Wu:copilot/support-editorconfig-another-one
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Support editorconfig #3513
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| {- | ||
| Copyright 2012-2024 Vidar Holen | ||
|
|
||
| This file is part of ShellCheck. | ||
| https://www.shellcheck.net | ||
|
|
||
| ShellCheck is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| ShellCheck is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| -} | ||
|
|
||
| {-# LANGUAGE TemplateHaskell #-} | ||
| -- Minimal support for reading shellcheck directives from EditorConfig | ||
| -- style files (https://editorconfig.org/). Only the `shellcheck.*` keys | ||
| -- of sections whose glob matches the file being checked are extracted, | ||
| -- and turned into the same "key=value" directive syntax that is used in | ||
| -- .shellcheckrc files. | ||
| module ShellCheck.EditorConfig (parseEditorConfig, isEditorConfigRoot, globToRegexString, runTests) where | ||
|
|
||
| import Data.Char | ||
| import Data.List | ||
| import Data.Maybe | ||
|
|
||
| import ShellCheck.Regex | ||
|
|
||
| import Test.QuickCheck | ||
|
|
||
| -- Given the contents of an EditorConfig style file and the name of the | ||
| -- file being checked, return the shellcheck directives (as a | ||
| -- "key=value\n" delimited blob, suitable for feeding into the same | ||
| -- parser as .shellcheckrc) found in matching sections. | ||
| -- | ||
| -- As per the EditorConfig spec, files are read top to bottom and | ||
| -- properties from later sections override those from earlier ones | ||
| -- (for the same key), so on conflicts the last matching section wins. | ||
| parseEditorConfig :: String -> FilePath -> String | ||
| parseEditorConfig contents name = | ||
| unlines . map render . lastWins . concatMap sectionDirectives $ sections | ||
| where | ||
| render (key, value) = key ++ "=" ++ value | ||
|
|
||
| -- Keep only the last occurrence of each key, preserving the | ||
| -- relative order of the remaining (first-seen) entries. | ||
| lastWins = reverse . nubBy (\a b -> fst a == fst b) . reverse | ||
|
|
||
| ls = lines contents | ||
| sections = splitSections ls | ||
|
|
||
| splitSections [] = [] | ||
| splitSections (l:rest) = | ||
| case parseHeader l of | ||
| Just pat -> | ||
| let (body, rest') = break (isJust . parseHeader) rest | ||
| in (pat, body) : splitSections rest' | ||
| Nothing -> splitSections rest | ||
|
|
||
| parseHeader l = | ||
| let t = trim (stripComment l) | ||
| in case t of | ||
| ('[':cs@(_:_)) | last cs == ']' -> Just (init cs) | ||
| _ -> Nothing | ||
|
|
||
| sectionDirectives (pat, body) = | ||
| if matchesGlob pat name | ||
| then mapMaybe toDirective body | ||
| else [] | ||
|
|
||
| toDirective l = | ||
| let t = trim (stripComment l) | ||
| in case break (== '=') t of | ||
| (key, '=':value) -> | ||
| let key' = trim key | ||
| value' = trim value | ||
| in if "shellcheck." `isPrefixOf` key' | ||
| then Just (drop (length "shellcheck.") key', value') | ||
| else Nothing | ||
| _ -> Nothing | ||
|
|
||
| stripComment = takeWhile (\c -> c /= '#' && c /= ';') | ||
|
|
||
| -- Does the top-level (pre-section) part of an EditorConfig file | ||
| -- declare "root = true"? Per the spec, this stops the search for | ||
| -- further EditorConfig files in parent directories. | ||
| isEditorConfigRoot :: String -> Bool | ||
| isEditorConfigRoot contents = | ||
| any isRootTrue . takeWhile (not . isSectionHeader) $ lines contents | ||
| where | ||
| isSectionHeader l = | ||
| case trim (stripComment l) of | ||
| ('[':cs@(_:_)) -> last cs == ']' | ||
| _ -> False | ||
|
|
||
| isRootTrue l = | ||
| case break (== '=') (trim (stripComment l)) of | ||
| (key, '=':value) -> | ||
| map toLower (trim key) == "root" && map toLower (trim value) == "true" | ||
| _ -> False | ||
|
|
||
| stripComment = takeWhile (\c -> c /= '#' && c /= ';') | ||
|
|
||
| trim :: String -> String | ||
| trim = dropWhileEnd isSpace . dropWhile isSpace | ||
|
|
||
| -- Does the (relative path of the) file match the given EditorConfig glob? | ||
| matchesGlob :: String -> FilePath -> Bool | ||
| matchesGlob pattern name = | ||
| name `matches` mkRegex (globToRegexString pattern) | ||
|
|
||
| -- Translate an EditorConfig glob pattern into an anchored regex string. | ||
| -- Per the spec, patterns without a path separator are matched against | ||
| -- the file at any depth (as if prefixed with "**/"). | ||
| globToRegexString :: String -> String | ||
| globToRegexString pattern = "^" ++ prefix ++ go pattern ++ "$" | ||
| where | ||
| prefix = if '/' `elem` pattern then "" else "(.*/)?" | ||
|
|
||
| go [] = "" | ||
| go ('*':'*':rest) = ".*" ++ go rest | ||
| go ('*':rest) = "[^/]*" ++ go rest | ||
| go ('?':rest) = "[^/]" ++ go rest | ||
| go ('[':rest) = | ||
| let (cls, rest') = break (== ']') rest | ||
| in case rest' of | ||
| (']':rest'') -> "[" ++ translateClass cls ++ "]" ++ go rest'' | ||
| _ -> "\\[" ++ go rest | ||
| go ('{':rest) = | ||
| let (body, rest') = break (== '}') rest | ||
| in case rest' of | ||
| ('}':rest'') -> | ||
| "(" ++ intercalate "|" (map go (splitCommas body)) ++ ")" ++ go rest'' | ||
| _ -> "\\{" ++ go rest | ||
| go (c:rest) | ||
| | c `elem` regexSpecials = ['\\', c] ++ go rest | ||
| | otherwise = c : go rest | ||
|
|
||
| regexSpecials = ".\\+()^$|" | ||
|
|
||
| translateClass ('!':cs) = '^' : escapeClass cs | ||
| translateClass cs = escapeClass cs | ||
| escapeClass = concatMap (\c -> if c == '\\' then "\\\\" else [c]) | ||
|
|
||
| splitCommas s = | ||
| case break (== ',') s of | ||
| (before, ',':after) -> before : splitCommas after | ||
| (before, "") -> [before] | ||
| (before, after) -> [before ++ after] | ||
|
|
||
| prop_globStar = matchesGlob "*.ebuild" "foo.ebuild" | ||
| prop_globBraceExt = matchesGlob "*.{ebuild,eclass}" "foo.eclass" | ||
| prop_globBraceExt2 = matchesGlob "*.{ebuild,eclass}" "foo.ebuild" | ||
| prop_globBraceName = matchesGlob "{PKGBUILD,APKBUILD}" "PKGBUILD" | ||
| prop_globBraceName2 = matchesGlob "{PKGBUILD,APKBUILD}" "APKBUILD" | ||
| prop_globNoMatch = not $ matchesGlob "*.ebuild" "foo.txt" | ||
| prop_globQuestion = matchesGlob "foo?.sh" "food.sh" | ||
| prop_globClass = matchesGlob "foo[0-9].sh" "foo1.sh" | ||
| prop_globClassNeg = not $ matchesGlob "foo[!0-9].sh" "foo1.sh" | ||
| -- Patterns without a path separator should match at any depth. | ||
| prop_globAnyDepth = matchesGlob "*.sh" "sub/dir/foo.sh" | ||
| prop_globAnyDepthPlain = matchesGlob "foo" "sub/foo" | ||
| -- Patterns with a path separator are only matched against the full | ||
| -- relative path. | ||
| prop_globWithSlashNoMatch = not $ matchesGlob "sub/*.sh" "other/foo.sh" | ||
| prop_globWithSlashMatch = matchesGlob "sub/*.sh" "sub/foo.sh" | ||
|
|
||
| prop_parseEditorConfig1 = | ||
| parseEditorConfig "[*.{ebuild,eclass}]\nshellcheck.shell=bash\nshellcheck.disable=SC2034\n" "foo.ebuild" | ||
| == "shell=bash\ndisable=SC2034\n" | ||
| prop_parseEditorConfig2 = | ||
| parseEditorConfig "[*.{ebuild,eclass}]\nshellcheck.shell=bash\n" "foo.txt" == "" | ||
| prop_parseEditorConfig3 = | ||
| parseEditorConfig "[{PKGBUILD,APKBUILD}]\nshellcheck.disable=SC2034\n" "PKGBUILD" == "disable=SC2034\n" | ||
| prop_parseEditorConfig4 = | ||
| parseEditorConfig "root = true\n[*.sh]\nindent_style = space\nshellcheck.shell=bash\n" "foo.sh" | ||
| == "shell=bash\n" | ||
| -- A later, more specific section overrides an earlier, more general | ||
| -- one for the same key. | ||
| prop_parseEditorConfig5 = | ||
| parseEditorConfig "[*]\nshellcheck.shell=sh\n\n[foo]\nshellcheck.shell=bash\n" "foo" | ||
| == "shell=bash\n" | ||
| -- Non-conflicting keys from earlier and later sections are all kept. | ||
| prop_parseEditorConfig6 = | ||
| parseEditorConfig "[*]\nshellcheck.shell=sh\n\n[foo]\nshellcheck.disable=SC2034\n" "foo" | ||
| == "shell=sh\ndisable=SC2034\n" | ||
|
|
||
| return [] | ||
| runTests = $quickCheckAll | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not support
{num1..num2}pattern.