Skip to content

Commit

Permalink
[JavaScript] Fix const generic arrow functions in TSX
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe committed Dec 15, 2024
1 parent 3050a55 commit 28dbfd9
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 16 deletions.
5 changes: 1 addition & 4 deletions JavaScript/JSX.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ variables:
contexts:
expression-begin:
- meta_prepend: true
- include: jsx-tag-hack
- include: jsx-tag

jsx-interpolation:
- match: (?={/\*)
Expand Down Expand Up @@ -67,9 +67,6 @@ contexts:
- meta_scope: meta.jsx.js
- include: immediately-pop

jsx-tag-hack: # Ugly hack so that TSX can un-include this in expression-begin
- include: jsx-tag

jsx-tag:
- match: <(?=\s*[/>{{jsx_tag_name_start}}])
scope: punctuation.definition.tag.begin.js
Expand Down
16 changes: 12 additions & 4 deletions JavaScript/TSX.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ first_line_match: |-
)
contexts:
branch-possible-arrow-function:
- meta_prepend: true
- include: jsx-tag

jsx-tag-hack: []
ts-old-type-assertion:
# old type assertions are replaced by JSX tags
- match: <
scope: punctuation.definition.tag.begin.js
set:
- jsx-meta
- jsx-tag-begin

jsx-tag: [] # replaced by `ts-old-type-assertion`

jsx-tag-begin:
- meta_include_prototype: false
- meta_scope: meta.tag.js
# note: type parameter modifiers indicate generic lambda
- match: (?=(?:in|out|const){{identifier_break}})
fail: arrow-function
- match: /
scope: punctuation.definition.tag.begin.js
set:
Expand Down
78 changes: 78 additions & 0 deletions JavaScript/tests/syntax_test_tsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,84 @@ if (a < b || c <= d) {}
// ^ meta.tag.name entity.name.tag
// ^ punctuation.terminator.statement

<const T>() => {}; // </T>;
// ^^^^^^^^^ meta.function meta.generic - meta.function meta.function
// ^^ meta.function.parameters - meta.function meta.function
// ^^^^^^ meta.function - meta.function meta.function
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^ punctuation.definition.generic.end
// ^ punctuation.section.group.begin
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function.arrow
// ^^ meta.block
// ^ punctuation.section.block.begin
// ^ punctuation.section.block.end
// ^ punctuation.terminator.statement
// ^^^^^^^^ comment.line.double-slash
// ^^ punctuation.definition.comment

<const T extends>() => {}; // </T>;
// ^^^^^^^^^^^^^^^^^ meta.function meta.generic - meta.function meta.function
// ^^ meta.function.parameters - meta.function meta.function
// ^^^^^^ meta.function - meta.function meta.function
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^^^^^^^ storage.modifier.extends
// ^ punctuation.definition.generic.end
// ^ punctuation.section.group.begin
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function.arrow
// ^^ meta.block
// ^ punctuation.section.block.begin
// ^ punctuation.section.block.end
// ^ punctuation.terminator.statement
// ^^^^^^^^ comment.line.double-slash
// ^^ punctuation.definition.comment

<const T extends "s">() => {x}; // </T>;
// ^^^^^^^^^^^^^^^^^^^^^ meta.function meta.generic - meta.function meta.function
// ^^ meta.function.parameters - meta.function meta.function
// ^^^^^^^ meta.function - meta.function meta.function
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^^^^^^^ storage.modifier.extends
// ^^^ meta.string string.quoted.double
// ^ punctuation.definition.generic.end
// ^ punctuation.section.group.begin
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function.arrow
// ^^^ meta.block
// ^ punctuation.section.block.begin
// ^ punctuation.section.block.end
// ^ punctuation.terminator.statement
// ^^^^^^^^ comment.line.double-slash
// ^^ punctuation.definition.comment

<const T extends="s">() => {x}; // </T>;
// ^^^^^^^^^^^^^^^^^^^^^ meta.function meta.generic - meta.function meta.function
// ^^ meta.function.parameters - meta.function meta.function
// ^^^^^^ meta.function - meta.function meta.function
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^^^^^^^ storage.modifier.extends
// ^ keyword.operator.assignment.js
// ^^^ meta.string string.quoted.double
// ^ punctuation.definition.generic.end
// ^ punctuation.section.group.begin
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function.arrow
// ^^^ meta.block
// ^ punctuation.section.block.begin
// ^ punctuation.section.block.end
// ^ punctuation.terminator.statement
// ^^^^^^^^ comment.line.double-slash
// ^^ punctuation.definition.comment

<T {...}>() => {};</T>;
// ^^^^^^^^^^^^^^^^^^^^^^ meta.jsx
// ^^^^^^^^^ meta.tag
Expand Down
137 changes: 129 additions & 8 deletions JavaScript/tests/syntax_test_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,12 +1247,66 @@ let x: (this: any) => any;

let x: < T > ( ... foo : any ) => any;
// ^^^^^ meta.generic
// ^ punctuation.definition.generic.begin
// ^ variable.parameter.generic
// ^ punctuation.definition.generic.end
// ^^^^^^^^^^^^^^^^^ meta.group
// ^ punctuation.section.group.begin
// ^^^ keyword.operator.spread
// ^^^ variable.parameter
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function
// ^^^ support.type.any

let x: < const T > ( ... foo : any ) => any;
// ^^^^^^^^^^^ meta.generic
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^ punctuation.definition.generic.end
// ^^^^^^^^^^^^^^^^^ meta.group
// ^ punctuation.section.group.begin
// ^^^ keyword.operator.spread
// ^^^ variable.parameter
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function
// ^^^ support.type.any

let x: < in T > ( ... foo : any ) => any;
// ^^^^^^^^ meta.generic
// ^ punctuation.definition.generic.begin
// ^^ storage.modifier.variance
// ^ variable.parameter.generic
// ^ punctuation.definition.generic.end
// ^^^^^^^^^^^^^^^^^ meta.group
// ^ punctuation.section.group.begin
// ^^^ keyword.operator.spread
// ^^^ variable.parameter
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function
// ^^^ support.type.any

let x: < out T > ( ... foo : any ) => any;
// ^^^^^^^^^ meta.generic
// ^ punctuation.definition.generic.begin
// ^^^ storage.modifier.variance
// ^ variable.parameter.generic
// ^ punctuation.definition.generic.end
// ^^^^^^^^^^^^^^^^^ meta.group
// ^ punctuation.section.group.begin
// ^^^ keyword.operator.spread
// ^^^ variable.parameter
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function
// ^^^ support.type.any

let x: () => T
U
Expand Down Expand Up @@ -1360,6 +1414,16 @@ let x: T.U
[];
//<- meta.sequence - meta.type

foo != bar
// ^^^ variable.other.readwrite
// ^^ keyword.operator.comparison
// ^^^ variable.other.readwrite

foo < bar
// ^^^ variable.other.readwrite
// ^ keyword.operator.comparison
// ^^^ variable.other.readwrite

foo < bar > ();
// ^^^ variable.function
// ^^^^^^^ meta.generic
Expand All @@ -1368,12 +1432,6 @@ let x: T.U
// ^ punctuation.definition.generic.end
// ^^ meta.group

foo < bar
// ^^^ variable.other.readwrite
// ^ keyword.operator.comparison
// ^^^ variable.other.readwrite
;

new Foo<bar>;
// ^^^ keyword.operator.word.new
// ^^^ variable.other.constant
Expand Down Expand Up @@ -1420,8 +1478,71 @@ const f = <T, U = V<any>>() => {};
// ^^ keyword.declaration.function.arrow
// ^^ meta.block

a != b;
// ^^ keyword.operator.comparison
const f = <const T>() => foo;
// ^^^^^^^^^ meta.function meta.generic - meta.function meta.function
// ^^ meta.function.parameters - meta.function meta.function
// ^^^^^^^ meta.function - meta.function meta.function
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^ punctuation.definition.generic.end
// ^ punctuation.section.group.begin
// ^ punctuation.section.group.end
// ^^ keyword.declaration.function.arrow
// ^^^ meta.block variable.other.readwrite
// ^ punctuation.terminator.statement

const f = <const T>(a: T,): T => foo;
// ^^^^^^^^^ meta.function meta.generic - meta.function meta.function
// ^^^^^^^ meta.function.parameters - meta.function meta.function
// ^ meta.function - meta.type - meta.function meta.function
// ^^^ meta.function meta.type - meta.function meta.function
// ^^^^^^ meta.function - meta.type - meta.function meta.function
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^ punctuation.definition.generic.end
// ^ punctuation.section.group.begin
// ^ variable.parameter.function
// ^ punctuation.separator.type
// ^^ meta.type
// ^ support.class
// ^ punctuation.separator.parameter.function
// ^ punctuation.section.group.end
// ^ punctuation.separator.type
// ^ support.class
// ^^ keyword.declaration.function.arrow
// ^^^ meta.block variable.other.readwrite
// ^ punctuation.terminator.statement

const f = <const T extends Foo, X extends Bar>(a: T,): X => foo;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.js meta.generic.js
// ^^^^^^^ meta.function.parameters - meta.function meta.function
// ^ meta.function - meta.type - meta.function meta.function
// ^^^ meta.function meta.type - meta.function meta.function
// ^^^^^^ meta.function - meta.type - meta.function meta.function
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier.const
// ^ variable.parameter.generic
// ^^^^^^^ storage.modifier.extends
// ^^^ support.class
// ^ punctuation.separator.comma
// ^ variable.parameter.generic
// ^^^^^^^ storage.modifier.extends
// ^^^ support.class
// ^ punctuation.definition.generic.end
// ^ punctuation.section.group.begin
// ^ meta.binding.name variable.parameter.function
// ^ punctuation.separator.type
// ^^ meta.type
// ^ support.class
// ^ punctuation.separator.parameter.function
// ^ punctuation.section.group.end
// ^ punctuation.separator.type
// ^ support.class
// ^^ keyword.declaration.function.arrow
// ^^^ meta.block variable.other.readwrite
// ^ punctuation.terminator.statement

const x = {
readonly: true,
Expand Down

0 comments on commit 28dbfd9

Please sign in to comment.