Skip to content
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

Add diagnostic for inconsistent string quoting styles #2510

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/en-us/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ Array<string>
* ``"luadoc-miss-vararg-type"``
* ``"luadoc-miss-version"``
* ``"malformed-number"``
* ``"mismatched-quote-types"``
* ``"miss-end"``
* ``"miss-esc-x"``
* ``"miss-exp"``
Expand Down
1 change: 1 addition & 0 deletions doc/pt-br/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ Array<string>
* ``"luadoc-miss-vararg-type"``
* ``"luadoc-miss-version"``
* ``"malformed-number"``
* ``"mismatched-quote-types"``
* ``"miss-end"``
* ``"miss-esc-x"``
* ``"miss-exp"``
Expand Down
1 change: 1 addition & 0 deletions doc/zh-cn/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ Array<string>
* ``"luadoc-miss-vararg-type"``
* ``"luadoc-miss-version"``
* ``"malformed-number"``
* ``"mismatched-quote-types"``
* ``"miss-end"``
* ``"miss-esc-x"``
* ``"miss-exp"``
Expand Down
1 change: 1 addition & 0 deletions doc/zh-tw/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ Array<string>
* ``"luadoc-miss-vararg-type"``
* ``"luadoc-miss-version"``
* ``"malformed-number"``
* ``"mismatched-quote-types"``
* ``"miss-end"``
* ``"miss-esc-x"``
* ``"miss-exp"``
Expand Down
2 changes: 2 additions & 0 deletions locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ DIAG_INJECT_FIELD_FIX_CLASS =
'To do so, use `---@class` for `{node}`.'
DIAG_INJECT_FIELD_FIX_TABLE =
'To allow injection, add `{fix}` to the definition.'
DIAG_MISMATCHED_QUOTE_TYPES =
'This string uses different quote types than other strings in this file.'

MWS_NOT_SUPPORT =
'{} does not support multi workspace for now, I may need to restart to support the new workspace ...'
Expand Down
2 changes: 2 additions & 0 deletions locale/en-us/setting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ config.diagnostics['unreachable-code'] =
'Enable diagnostics for unreachable code.'
config.diagnostics['global-element'] =
'Enable diagnostics to warn about global elements.'
config.diagnostics['mismatched-quote-types']=
'Enable diagnostics for mismatched quote types on string literals within a file.'
config.typeFormat.config =
'Configures the formatting behavior while typing Lua code.'
config.typeFormat.config.auto_complete_end =
Expand Down
39 changes: 39 additions & 0 deletions script/core/diagnostics/mismatched-quote-types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local files = require 'files'
local guide = require 'parser.guide'
local lang = require 'language'

---@async
return function (uri, callback)
local ast = files.getState(uri)
if not ast then
return
end

local singleQuotedStrings = {}
local doubleQuotedStrings = {}
guide.eachSourceType(ast.ast, 'string', function (source)
local str = source[1]
local quoteType = source[2]
if quoteType == "'" and not str:find('"') then
singleQuotedStrings[#singleQuotedStrings+1] = source
elseif quoteType == '"' and not str:find("'") then
doubleQuotedStrings[#doubleQuotedStrings+1] = source
end
end)
if #singleQuotedStrings == 0 or #doubleQuotedStrings == 0 then
return
end
local minorityQuoteTypes
if #singleQuotedStrings > #doubleQuotedStrings then
minorityQuoteTypes = doubleQuotedStrings
else
minorityQuoteTypes = singleQuotedStrings
end
for _, source in ipairs(minorityQuoteTypes) do
callback {
start = source.start,
finish = source.finish,
message = lang.script.DIAG_MISMATCHED_QUOTE_TYPES,
}
end
end
3 changes: 2 additions & 1 deletion script/proto/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ m.register {
}

m.register {
'codestyle-check'
'codestyle-check',
'mismatched-quote-types'
} {
group = 'codestyle',
severity = 'Warning',
Expand Down
1 change: 1 addition & 0 deletions test/diagnostics/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ check 'incomplete-signature-doc'
check 'inject-field'
check 'invisible'
check 'lowercase-global'
check 'mismatched-quote-types'
check 'missing-fields'
check 'missing-global-doc'
check 'missing-local-export-doc'
Expand Down
29 changes: 29 additions & 0 deletions test/diagnostics/mismatched-quote-types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
TEST [[
local x = 'foo'
local y = 'bar'
local z = <!"baz"!>
]]

TEST [[
local x = <!'foo'!>
local y = "bar"
local z = "baz"
]]

TEST [=[
local x = [[foo]]
local y = [[bar]]
local z = "baz"
]=]

TEST [[
local x = "foo"
local y = "bar"
local z = 'b"a"z'
]]

TEST [[
local x = 'foo'
local y = 'bar'
local z = "b'a'z"
]]
Loading