mirror of
				https://github.com/koalaman/shellcheck.git
				synced 2025-10-31 22:52:50 +08:00 
			
		
		
		
	Parse heredocs correctly with carriage returns (fixes #2103)
This commit is contained in:
		| @@ -14,6 +14,7 @@ | |||||||
|   is still purely cosmetic and does not allow ShellCheck to continue. |   is still purely cosmetic and does not allow ShellCheck to continue. | ||||||
| - Improved error reporting for trailing tokens after ]/]] and compound commands | - Improved error reporting for trailing tokens after ]/]] and compound commands | ||||||
| - `#!/usr/bin/env -S shell` is now handled correctly | - `#!/usr/bin/env -S shell` is now handled correctly | ||||||
|  | - Here docs with \r are now parsed correctly and give better warnings | ||||||
|  |  | ||||||
| ### Changed | ### Changed | ||||||
| - Assignments are now parsed to spec, without leniency for leading $ or spaces | - Assignments are now parsed to spec, without leniency for leading $ or spaces | ||||||
|   | |||||||
| @@ -123,8 +123,10 @@ readUnicodeQuote = do | |||||||
|     return $ T_Literal id [c] |     return $ T_Literal id [c] | ||||||
|  |  | ||||||
| carriageReturn = do | carriageReturn = do | ||||||
|     parseNote ErrorC 1017 "Literal carriage return. Run script through tr -d '\\r' ." |     pos <- getPosition | ||||||
|     char '\r' |     char '\r' | ||||||
|  |     parseProblemAt pos ErrorC 1017 "Literal carriage return. Run script through tr -d '\\r' ." | ||||||
|  |     return '\r' | ||||||
|  |  | ||||||
| almostSpace = | almostSpace = | ||||||
|     choice [ |     choice [ | ||||||
| @@ -1761,6 +1763,8 @@ prop_readHereDoc17= isWarning readScript "cat <<- ' foo'\nbar\n  foo\n foo\n" | |||||||
| prop_readHereDoc18= isOk readScript "cat <<'\"foo'\nbar\n\"foo\n" | prop_readHereDoc18= isOk readScript "cat <<'\"foo'\nbar\n\"foo\n" | ||||||
| prop_readHereDoc20= isWarning readScript "cat << foo\n  foo\n()\nfoo\n" | prop_readHereDoc20= isWarning readScript "cat << foo\n  foo\n()\nfoo\n" | ||||||
| prop_readHereDoc21= isOk readScript "# shellcheck disable=SC1039\ncat << foo\n  foo\n()\nfoo\n" | prop_readHereDoc21= isOk readScript "# shellcheck disable=SC1039\ncat << foo\n  foo\n()\nfoo\n" | ||||||
|  | prop_readHereDoc22 = isWarning readScript "cat << foo\r\ncow\r\nfoo\r\n" | ||||||
|  | prop_readHereDoc23 = isNotOk readScript "cat << foo \r\ncow\r\nfoo\r\n" | ||||||
| readHereDoc = called "here document" $ do | readHereDoc = called "here document" $ do | ||||||
|     pos <- getPosition |     pos <- getPosition | ||||||
|     try $ string "<<" |     try $ string "<<" | ||||||
| @@ -1789,7 +1793,9 @@ readHereDoc = called "here document" $ do | |||||||
|     -- Fun fact: bash considers << foo"" quoted, but not << <("foo"). |     -- Fun fact: bash considers << foo"" quoted, but not << <("foo"). | ||||||
|     readToken = do |     readToken = do | ||||||
|         str <- readStringForParser readNormalWord |         str <- readStringForParser readNormalWord | ||||||
|         return $ unquote str |         -- A here doc actually works with \r\n because the \r becomes part of the token | ||||||
|  |         crstr <- (carriageReturn >> (return $ str ++ "\r")) <|> return str | ||||||
|  |         return $ unquote crstr | ||||||
|  |  | ||||||
| readPendingHereDocs = do | readPendingHereDocs = do | ||||||
|     docs <- popPendingHereDocs |     docs <- popPendingHereDocs | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user