Skip to content

Allow multiline string literals as JSX attribute values. #91

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

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
29 changes: 29 additions & 0 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const PREC = {
ACCESSIBILITY: 1,
DEFINITION: 1,
DECLARATION: 1,
STRING: 2, // must match the value defined in javascript's grammar.js.
INTERSECTION: 2,
UNION: 2,
PLUS: 4,
Expand Down Expand Up @@ -156,6 +157,34 @@ module.exports = function defineGrammar(dialect) {
'>'
)),

// Use more tolerant $.tsx_string instead of $.string.
_jsx_attribute_value: ($, previous) => choice(
...previous.members.map(member =>
member.name == 'string' ? $.tsx_string : member
)),

// Same as $.string defined for javascript, with modified regexp
// that allows unescaped newlines.
// See https://github.com/Microsoft/TypeScript/issues/13328
tsx_string: $ => choice(
seq(
'"',
repeat(choice(
token.immediate(prec(PREC.STRING, /[^"\\\n]+|\\?\r?\n/)),
$.escape_sequence
)),
'"'
),
seq(
"'",
repeat(choice(
token.immediate(prec(PREC.STRING, /[^'\\\n]+|\\?\r?\n/)),
$.escape_sequence
)),
"'"
)
),

_import_export_specifier: ($, previous) => seq(
optional(choice('type', 'typeof')),
previous
Expand Down
22 changes: 22 additions & 0 deletions tsx/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,25 @@ Type arguments in JSX
(jsx_opening_element (identifier) (type_arguments (type_identifier)))
(jsx_text)
(jsx_closing_element (identifier)))))

==========================================================
TSX string literals
==========================================================

<Element attr="hello
world">
</Element>;

---

(program
(expression_statement
(jsx_element
(jsx_opening_element
(identifier)
(jsx_attribute
(property_identifier)
(tsx_string)))
(jsx_text)
(jsx_closing_element
(identifier)))))
79 changes: 78 additions & 1 deletion tsx/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2624,7 +2624,7 @@
"members": [
{
"type": "SYMBOL",
"name": "string"
"name": "tsx_string"
},
{
"type": "SYMBOL",
Expand Down Expand Up @@ -6565,6 +6565,83 @@
}
]
},
"tsx_string": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PREC",
"value": 2,
"content": {
"type": "PATTERN",
"value": "[^\"\\\\\\n]+|\\\\?\\r?\\n"
}
}
},
{
"type": "SYMBOL",
"name": "escape_sequence"
}
]
}
},
{
"type": "STRING",
"value": "\""
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "'"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PREC",
"value": 2,
"content": {
"type": "PATTERN",
"value": "[^'\\\\\\n]+|\\\\?\\r?\\n"
}
}
},
{
"type": "SYMBOL",
"name": "escape_sequence"
}
]
}
},
{
"type": "STRING",
"value": "'"
}
]
}
]
},
"non_null_expression": {
"type": "PREC_LEFT",
"value": 10,
Expand Down
21 changes: 18 additions & 3 deletions tsx/src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3283,7 +3283,7 @@
"named": true
},
{
"type": "string",
"type": "tsx_string",
"named": true
}
]
Expand Down Expand Up @@ -5078,6 +5078,21 @@
}
}
},
{
"type": "tsx_string",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "escape_sequence",
"named": true
}
]
}
},
{
"type": "tuple_type",
"named": true,
Expand Down Expand Up @@ -6314,11 +6329,11 @@
},
{
"type": "number",
"named": false
"named": true
},
{
"type": "number",
"named": true
"named": false
},
{
"type": "of",
Expand Down
Loading