Skip to content

Commit

Permalink
fix: javascript and typescript queries (#34)
Browse files Browse the repository at this point in the history
* Update class.scm

* Update class.scm

* refactor: simplify query

* add: more tests

* fix: missing parser

---------

Co-authored-by: Luckas <[email protected]>
  • Loading branch information
RoboG-11 and luckasRanarison authored Aug 4, 2024
1 parent f96222d commit 8c5766c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions queries/javascript/class.scm
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
; inherits: tsx

(call_expression
function: [
(identifier) @ident
Expand Down
15 changes: 14 additions & 1 deletion queries/typescript/class.scm
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
; inherits: javascript
(call_expression
function: [
(identifier) @ident
(member_expression
object: (identifier) @object-ident)
]
(#any-of? @ident "clsx" "classnames" "tw" "css")
(#eq? @object-ident "tw")
arguments: [
(arguments
(_)+) @tailwind ; the actual class range is extracted in the code
(template_string
(string_fragment) @tailwind)
])
1 change: 1 addition & 0 deletions tests/parsers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local parsers = {
"heex",
"elixir",
"javascript",
"typescript",
"templ",
}

Expand Down
23 changes: 23 additions & 0 deletions tests/queries/javascript/Component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const color = "text-blue-300";

const Component = () => {
return (
<div>
<div
className="flex items-center justify-center container mx-auto p-4
border-2 border-gray-500 rounded-md bg-gray-100"
>
<div className="text-2xl font-bold mb-4">
Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint
cillum sint consectetur cupidatat.
</div>
<div className={`${color} bg-gray-100`}>
Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint
cillum sint consectetur cupidatat.
</div>
</div>
</div>
);
};

export default Component;
15 changes: 14 additions & 1 deletion tests/queries/javascript_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require("tests.queries.runner").test({
local runner = require("tests.queries.runner")

runner.test({
name = "javascript",
provider = "treesitter",
file = "tests/queries/javascript/test.js",
Expand All @@ -9,3 +11,14 @@ require("tests.queries.runner").test({
{ 3, 9, 3, 34 },
},
})

runner.test({
name = "javascriptreact",
provider = "treesitter",
file = "tests/queries/javascript/Component.jsx",
ranges = {
{ 6, 19, 7, 55 },
{ 9, 24, 9, 47 },
{ 13, 24, 13, 46 },
},
})
4 changes: 4 additions & 0 deletions tests/queries/typescript/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clsx("p-4", "text-center");
classnames("bg-red-500", "uppercase");
tw`font-mono text-sm`
tw.input`border hover:border-black`
11 changes: 11 additions & 0 deletions tests/queries/typescript_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require("tests.queries.runner").test({
name = "typescript",
provider = "treesitter",
file = "tests/queries/typescript/test.ts",
ranges = {
{ 0, 5, 0, 25 },
{ 1, 11, 1, 36 },
{ 2, 3, 2, 20 },
{ 3, 9, 3, 34 },
},
})

0 comments on commit 8c5766c

Please sign in to comment.