Skip to content

Commit

Permalink
Add diagnostic for inconsistent string quoting styles
Browse files Browse the repository at this point in the history
  • Loading branch information
emmericp committed Feb 2, 2024
1 parent a89b343 commit e8cb22c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 1 deletion.
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 files.'

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
38 changes: 38 additions & 0 deletions script/core/diagnostics/mismatched-quote-types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 quoteType = source[2]
if quoteType == '\'' then
singleQuotedStrings[#singleQuotedStrings+1] = source
elseif quoteType == '"' 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
17 changes: 17 additions & 0 deletions test/diagnostics/mismatched-quote-types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
TEST [[
local x = 'foo'
local y = 'bar'
local y = <!"baz"!>
]]

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

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

0 comments on commit e8cb22c

Please sign in to comment.