Skip to content

Commit 68d08e0

Browse files
committed
refactor: get rid of $._raw_js_extension and $._raw_ggl_extension
Close #144
1 parent 2bf1d01 commit 68d08e0

File tree

5 files changed

+96
-95
lines changed

5 files changed

+96
-95
lines changed

grammar.js

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = grammar({
8686
[$._record_field_name, $.record_pattern],
8787
[$.decorator],
8888
[$._statement, $._one_or_more_statements],
89-
[$._simple_extension],
89+
[$.extension_expression],
9090
[$._inline_type, $.function_type_parameters],
9191
[$.primary_expression, $.parameter, $._pattern],
9292
[$.parameter, $._pattern],
@@ -1137,63 +1137,11 @@ module.exports = grammar({
11371137

11381138
extension_expression: $ => prec('call', seq(
11391139
repeat1('%'),
1140-
choice(
1141-
$._raw_js_extension,
1142-
$._raw_gql_extension,
1143-
$._simple_extension,
1144-
),
1145-
)),
1146-
1147-
_simple_extension: $ => seq(
11481140
$.extension_identifier,
1149-
optional($._extension_expression_payload),
1150-
),
1151-
1152-
_raw_js_extension: $ => seq(
1153-
alias(token('raw'), $.extension_identifier),
1154-
'(',
1155-
alias($._raw_js, $.expression_statement),
1156-
')',
1157-
),
1158-
1159-
_raw_js: $ => choice(
1160-
alias($._raw_js_template_string, $.template_string),
1161-
alias($._raw_js_string, $.string),
1162-
),
1163-
1164-
_raw_js_string: $ => alias($.string, $.raw_js),
1165-
1166-
_raw_js_template_string: $ => seq(
1167-
token(seq(
1168-
optional(choice(
1169-
'j',
1170-
'js',
1171-
)),
1172-
'`',
1173-
)),
1174-
alias(repeat($._template_string_content), $.raw_js),
1175-
'`',
1176-
),
1177-
1178-
_raw_gql_extension: $ => seq(
1179-
alias(token('graphql'), $.extension_identifier),
1180-
'(',
1181-
alias($._raw_gql, $.expression_statement),
1182-
')',
1183-
),
1184-
1185-
_raw_gql: $ => choice(
1186-
alias($._raw_gql_template_string, $.template_string),
1187-
alias($._raw_gql_string, $.string),
1188-
),
1189-
1190-
_raw_gql_string: $ => alias($.string, $.raw_gql),
1191-
1192-
_raw_gql_template_string: $ => seq(
1193-
'`',
1194-
alias(repeat($._template_string_content), $.raw_gql),
1195-
'`',
1196-
),
1141+
optional(
1142+
$._extension_expression_payload,
1143+
)
1144+
)),
11971145

11981146
_extension_expression_payload: $ => seq(
11991147
'(',
@@ -1430,18 +1378,21 @@ module.exports = grammar({
14301378
)),
14311379
'`',
14321380
)),
1433-
repeat($._template_string_content),
1381+
$.template_string_content,
14341382
'`'
14351383
),
14361384

1437-
_template_string_content: $ => choice(
1438-
$._template_chars,
1439-
$.template_substitution,
1440-
choice(
1441-
alias('\\`', $.escape_sequence),
1442-
$.escape_sequence,
1385+
template_string_content: $ =>
1386+
repeat1(
1387+
choice(
1388+
$._template_chars,
1389+
$.template_substitution,
1390+
choice(
1391+
alias('\\`', $.escape_sequence),
1392+
$.escape_sequence,
1393+
)
1394+
),
14431395
),
1444-
),
14451396

14461397
template_substitution: $ => choice(
14471398
seq('$', $.value_identifier),

queries/injections.scm

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1-
(raw_js) @javascript
2-
(raw_gql) @graphql
31
(comment) @comment
2+
3+
; %re("")
4+
(extension_expression
5+
(extension_identifier) @_name
6+
(#eq? @_name "re")
7+
(expression_statement (string) @regex))
8+
9+
; %re(``)
10+
(extension_expression
11+
(extension_identifier) @_name
12+
(#eq? @_name "re")
13+
(expression_statement
14+
(template_string
15+
(template_string_content) @regex)))
16+
17+
; %raw("") or %%raw("")
18+
(extension_expression
19+
(extension_identifier) @_name
20+
(#eq? @_name "raw")
21+
(expression_statement
22+
(string
23+
(string_fragment) @javascript)))
24+
25+
; %raw(``) or %%raw(``)
26+
(extension_expression
27+
(extension_identifier) @_name
28+
(#eq? @_name "raw")
29+
(expression_statement
30+
(template_string
31+
(template_string_content) @javascript)))
32+
33+
; %graphql``
34+
(extension_expression
35+
(extension_identifier) @_name
36+
(#eq? @_name "graphql")
37+
(expression_statement
38+
(string
39+
(string_fragment) @graphql)))
40+
41+
; %graphql""
42+
(extension_expression
43+
(extension_identifier) @_name
44+
(#eq? @_name "graphql")
45+
(expression_statement
46+
(string
47+
(string_fragment) @graphql)))

test/corpus/expressions.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,7 @@ module Test = %graphql(`
11381138
(extension_identifier)
11391139
(expression_statement
11401140
(string
1141-
(raw_js
1142-
(string_fragment))))))
1141+
(string_fragment)))))
11431142
(expression_statement
11441143
(extension_expression
11451144
(extension_identifier)
@@ -1168,7 +1167,7 @@ module Test = %graphql(`
11681167
(extension_identifier)
11691168
(expression_statement
11701169
(template_string
1171-
(raw_js
1170+
(template_string_content
11721171
(escape_sequence)
11731172
(escape_sequence))))))
11741173
(let_binding
@@ -1177,7 +1176,7 @@ module Test = %graphql(`
11771176
(extension_identifier)
11781177
(expression_statement
11791178
(template_string
1180-
(raw_js
1179+
(template_string_content
11811180
(escape_sequence)
11821181
(escape_sequence))))))
11831182
(module_declaration
@@ -1186,7 +1185,7 @@ module Test = %graphql(`
11861185
(extension_identifier)
11871186
(expression_statement
11881187
(template_string
1189-
(raw_gql))))))
1188+
(template_string_content))))))
11901189

11911190
===========================================
11921191
Raise expression

test/corpus/literals.txt

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,30 @@ The caller should either handle this error, or expect that exit code.`
129129
----
130130

131131
(source_file
132-
(expression_statement (template_string))
133-
(expression_statement (template_string))
134-
(expression_statement (template_string))
135-
(expression_statement (template_string
136-
(template_substitution
137-
(binary_expression (number) (number)))
138-
(template_substitution
139-
(binary_expression (number) (number)))))
140-
(expression_statement (template_string
141-
(escape_sequence)
142-
(template_substitution (call_expression
143-
(value_identifier)
144-
(arguments (string (string_fragment)))))
145-
(escape_sequence)
146-
(template_substitution (value_identifier))))
147-
(expression_statement (template_string))
148-
(expression_statement (template_string (escape_sequence))))
132+
(expression_statement (template_string (template_string_content)))
133+
(expression_statement (template_string (template_string_content)))
134+
(expression_statement (template_string (template_string_content)))
135+
(expression_statement
136+
(template_string
137+
(template_string_content
138+
(template_substitution
139+
(binary_expression (number) (number)))
140+
(template_substitution
141+
(binary_expression (number) (number))))))
142+
(expression_statement
143+
(template_string
144+
(template_string_content
145+
(escape_sequence)
146+
(template_substitution
147+
(call_expression
148+
(value_identifier)
149+
(arguments
150+
(string
151+
(string_fragment)))))
152+
(escape_sequence)
153+
(template_substitution (value_identifier)))))
154+
(expression_statement (template_string (template_string_content)))
155+
(expression_statement (template_string (template_string_content (escape_sequence)))))
149156

150157
============================================
151158
Tricky template strings
@@ -160,11 +167,11 @@ Tricky template strings
160167
---
161168

162169
(source_file
163-
(expression_statement (template_string))
164-
(expression_statement (template_string))
165-
(expression_statement (template_string))
166-
(expression_statement (template_string))
167-
(expression_statement (template_string)))
170+
(expression_statement (template_string (template_string_content)))
171+
(expression_statement (template_string (template_string_content)))
172+
(expression_statement (template_string (template_string_content)))
173+
(expression_statement (template_string (template_string_content)))
174+
(expression_statement (template_string (template_string_content))))
168175

169176
============================================
170177
Characters

test/corpus/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ external add: (
413413
(parameter
414414
(type_identifier_path (module_identifier_path (module_identifier)) (type_identifier)))
415415
(parameter
416-
(decorator (decorator_identifier) (decorator_arguments (template_string)))
416+
(decorator (decorator_identifier) (decorator_arguments (template_string (template_string_content))))
417417
(type_identifier))
418418
(parameter
419419
(decorator (decorator_identifier) (decorator_arguments (string)))

0 commit comments

Comments
 (0)