// TypeScript React

<beginfold id='1'>/** </beginfold id='1'>@author Willy <willy@wmail.com>
  * @url https://reactjs.org/ *<endfold id='1'>*/</endfold id='1'>

import React from 'react';
import <beginfold id='2'>{</beginfold id='2'> PhotoStory, VideoStory <endfold id='2'>}</endfold id='2'> from './stories';

function Story(props) <beginfold id='2'>{</beginfold id='2'>
  const SpecificStory = components<beginfold id='3'>[</beginfold id='3'>props.storyType<endfold id='3'>]</endfold id='3'>;
  return <beginfold id='4'><SpecificStory</beginfold id='4'> story=<beginfold id='5'>{</beginfold id='5'> props.story <endfold id='5'>}</endfold id='5'> attr2="&ref;" attr3="Hello\n" <endfold id='4'>/></endfold id='4'>;
<endfold id='2'>}</endfold id='2'>

function
<beginfold id='4'><Tag</beginfold id='4'> attr1=<beginfold id='5'>{</beginfold id='5'> <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'> function <noTag/> return class var 0x123 <beginfold id='2'>{</beginfold id='2'> <endfold id='2'>}</endfold id='2'> &noRef; hello() React.Component() <endfold id='5'>}</endfold id='5'> attr2="&ref;">
	/* no comment*/ function <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'> return class var 0x123 &ref; hello() React.Component()
	.<beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'> anyWord <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'>
	<beginfold id='5'>{</beginfold id='5'> function <tag> return class var 0x123 hello() React.Component() <endfold id='5'>}</endfold id='5'>
</Tag<endfold id='4'>></endfold id='4'>

<beginfold id='6'><tag1</beginfold id='6'>> </tag1<endfold id='6'>></endfold id='6'>
<beginfold id='6'><tag1</beginfold id='6'>> </Tag$<endfold id='6'>></endfold id='6'>
<beginfold id='4'><Tag$</beginfold id='4'>> </tag<endfold id='4'>></endfold id='4'>

<beginfold id='6'><tag</beginfold id='6'><beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'>attr1<beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'>= <beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'>"value"<beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'>attr2 <beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'>attr3='a' key<beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'>key2 <endfold id='6'>/></endfold id='6'>

// Detect Valid tags

<beginfold id='7'>/*</beginfold id='7'> comment <endfold id='7'>*/</endfold id='7'> <beginfold id='6'><tag</beginfold id='6'>></tag<endfold id='6'>></endfold id='6'>
<beginfold id='2'>{ </beginfold id='2'><beginfold id='7'>/*</beginfold id='7'> comment
	<endfold id='7'>*/</endfold id='7'> <beginfold id='4'><Tag</beginfold id='4'> <endfold id='4'>/></endfold id='4'>
	word <noTag/> . <noTag/> <endfold id='2'>}</endfold id='2'> <noTag/>
return <beginfold id='7'>/*</beginfold id='7'> comment
	multiline <endfold id='7'>*/</endfold id='7'> <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'> <beginfold id='7'>/*</beginfold id='7'> comment <endfold id='7'>*/</endfold id='7'> <beginfold id='4'><Tag</beginfold id='4'><endfold id='4'>/></endfold id='4'>
&& <beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'> <beginfold id='4'><Tag</beginfold id='4'><endfold id='4'>/></endfold id='4'>
& <beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'> <noTag/>

  <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'>
<beginfold id='2'>{ </beginfold id='2'><beginfold id='6'><hello</beginfold id='6'>>Hello</hello<endfold id='6'>></endfold id='6'> <endfold id='2'>}</endfold id='2'>
?<beginfold id='4'><Tag</beginfold id='4'> <endfold id='4'>/></endfold id='4'>;
<beginfold id='3'>[ </beginfold id='3'><beginfold id='6'><tag</beginfold id='6'> <endfold id='6'>/></endfold id='6'>  ( <beginfold id='6'><tag</beginfold id='6'> <endfold id='6'>/></endfold id='6'>
,<beginfold id='4'><Tag</beginfold id='4'><endfold id='4'>/></endfold id='4'>    =<beginfold id='4'><Tag</beginfold id='4'><endfold id='4'>/></endfold id='4'>
&&<beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'>   ||<beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'>
return <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'> ;
default<beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'> ;
<beginfold id='4'><Tag</beginfold id='4'>> <beginfold id='6'><tag</beginfold id='6'>> <beginfold id='4'><tag$</beginfold id='4'><endfold id='4'>/></endfold id='4'> </tag<endfold id='6'>></endfold id='6'> return </Tag<endfold id='4'>></endfold id='4'>

anyWord<noTag>
anyWord<beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'> <noTag/>
.<noTag>
&<notag> | <noTag/>
% <beginfold id='7'>/*</beginfold id='7'> comment<endfold id='7'>*/</endfold id='7'> <noTag/>

// TODO: Fix this (comment before the tag name):
var x = <<beginfold id='7'>/*</beginfold id='7'><endfold id='7'>*/</endfold id='7'>div></div>;

// Tag after ":"
annotation: <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'>
annotation: text <beginfold id='3'>[</beginfold id='3'> <beginfold id='6'><tag</beginfold id='6'><endfold id='6'>/></endfold id='6'> <endfold id='3'>]</endfold id='3'>
console.log("hello")

// Type assertion in tag
<beginfold id='4'><C</beginfold id='4'><number><endfold id='4'>/></endfold id='4'>
<beginfold id='4'><C</beginfold id='4'><number>> </C<endfold id='4'>></endfold id='4'>
<beginfold id='4'><C</beginfold id='4'>
<error <endfold id='4'>/></endfold id='4'>

// Non-ASCII tag name & attribute
<beginfold id='4'><日本語</beginfold id='4'>></日本語<endfold id='4'>></endfold id='4'>;
<beginfold id='4'><Component</beginfold id='4'> 本本:本-本 aa本:本 aa:aa <endfold id='4'>/></endfold id='4'>
<beginfold id='4'><aaaa:ñ</beginfold id='4'> <endfold id='4'>/></endfold id='4'>

<beginfold id='4'><Namespace.DeepNamespace.Component</beginfold id='4'> <endfold id='4'>/></endfold id='4'>;
<beginfold id='4'><Component</beginfold id='4'> <beginfold id='5'>{</beginfold id='5'> ... x <endfold id='5'>}</endfold id='5'> y
=<beginfold id='5'>{</beginfold id='5'>2 <endfold id='5'>}</endfold id='5'> z <endfold id='4'>/></endfold id='4'>;

let k1 =
    <beginfold id='4'><Comp</beginfold id='4'> a=<beginfold id='5'>{</beginfold id='5'>10<endfold id='5'>}</endfold id='5'> b="hi" <beginfold id='5'>{</beginfold id='5'>...o<endfold id='5'>}</endfold id='5'> >
        hi hi hi!
    </Comp<endfold id='4'>></endfold id='4'>;

let k2 =
    <beginfold id='4'><Comp</beginfold id='4'> a=<beginfold id='5'>{</beginfold id='5'>10<endfold id='5'>}</endfold id='5'> b="hi">
        <beginfold id='6'><div</beginfold id='6'>> My Div </div<endfold id='6'>></endfold id='6'>
        <beginfold id='5'>{</beginfold id='5'>(name: string) => <beginfold id='6'><div</beginfold id='6'>> My name <beginfold id='5'>{</beginfold id='5'>name<endfold id='5'>}</endfold id='5'> </div<endfold id='6'>></endfold id='6'><endfold id='5'>}</endfold id='5'>
    </Comp<endfold id='4'>></endfold id='4'>;

let k3 = <beginfold id='4'><GenericComponent</beginfold id='4'> initialValues=<beginfold id='5'>{</beginfold id='5'><beginfold id='2'>{</beginfold id='2'> x: "y" <endfold id='2'>}</endfold id='2'><endfold id='5'>}</endfold id='5'> nextValues=<beginfold id='5'>{</beginfold id='5'>a => (<beginfold id='2'>{</beginfold id='2'> x: a.x <endfold id='2'>}</endfold id='2'>)<endfold id='5'>}</endfold id='5'> <endfold id='4'>/></endfold id='4'>; // No Error

// OK
let k1 = <beginfold id='4'><Comp</beginfold id='4'> a=<beginfold id='5'>{</beginfold id='5'>10<endfold id='5'>}</endfold id='5'> b="hi"><beginfold id='6'><></beginfold id='6'><endfold id='6'></></endfold id='6'><beginfold id='4'><Button</beginfold id='4'> <endfold id='4'>/></endfold id='4'><beginfold id='4'><AnotherButton</beginfold id='4'> <endfold id='4'>/></endfold id='4'></Comp<endfold id='4'>></endfold id='4'>;
let k2 = <beginfold id='4'><Comp</beginfold id='4'> a=<beginfold id='5'>{</beginfold id='5'>10<endfold id='5'>}</endfold id='5'> b="hi"><beginfold id='6'><></beginfold id='6'><beginfold id='4'><Button</beginfold id='4'> <endfold id='4'>/></endfold id='4'><endfold id='6'></></endfold id='6'><beginfold id='4'><AnotherButton</beginfold id='4'> <endfold id='4'>/></endfold id='4'></Comp<endfold id='4'>></endfold id='4'>;
let k3 = <beginfold id='4'><Comp</beginfold id='4'> a=<beginfold id='5'>{</beginfold id='5'>10<endfold id='5'>}</endfold id='5'> b="hi"><beginfold id='6'><></beginfold id='6'><beginfold id='4'><Button</beginfold id='4'> <endfold id='4'>/></endfold id='4'><beginfold id='4'><AnotherButton</beginfold id='4'> <endfold id='4'>/></endfold id='4'><endfold id='6'></></endfold id='6'></Comp<endfold id='4'>></endfold id='4'>;
let k4 = <beginfold id='4'><SingleChildComp</beginfold id='4'> a=<beginfold id='5'>{</beginfold id='5'>10<endfold id='5'>}</endfold id='5'> b="hi"><beginfold id='6'><></beginfold id='6'><beginfold id='4'><Button</beginfold id='4'> <endfold id='4'>/></endfold id='4'><beginfold id='4'><AnotherButton</beginfold id='4'> <endfold id='4'>/></endfold id='4'><endfold id='6'></></endfold id='6'></SingleChildComp<endfold id='4'>></endfold id='4'>;
// OK
let k1 = <beginfold id='6'><div</beginfold id='6'>> <beginfold id='6'><h2</beginfold id='6'>> Hello </h2<endfold id='6'>></endfold id='6'> <beginfold id='6'><h1</beginfold id='6'>> world </h1<endfold id='6'>></endfold id='6'></div<endfold id='6'>></endfold id='6'>;
let k2 = <beginfold id='6'><div</beginfold id='6'>> <beginfold id='6'><h2</beginfold id='6'>> Hello </h2<endfold id='6'>></endfold id='6'> <beginfold id='5'>{</beginfold id='5'>(user: any) => <beginfold id='6'><h2</beginfold id='6'>><beginfold id='5'>{</beginfold id='5'>user.name<endfold id='5'>}</endfold id='5'></h2<endfold id='6'>></endfold id='6'><endfold id='5'>}</endfold id='5'></div<endfold id='6'>></endfold id='6'>;
let k3 = <beginfold id='6'><div</beginfold id='6'>> <beginfold id='5'>{</beginfold id='5'>1<endfold id='5'>}</endfold id='5'> <beginfold id='5'>{</beginfold id='5'>"That is a number"<endfold id='5'>}</endfold id='5'> </div<endfold id='6'>></endfold id='6'>;
let k4 = <beginfold id='4'><Button</beginfold id='4'>> <beginfold id='6'><h2</beginfold id='6'>> Hello </h2<endfold id='6'>></endfold id='6'> </Button<endfold id='4'>></endfold id='4'>;

// Empty tags
hello<>
hello<string>

<beginfold id='6'><></beginfold id='6'><endfold id='6'></></endfold id='6'>; // no whitespace
<beginfold id='6'><    ></beginfold id='6'><endfold id='6'></   ></endfold id='6'>; // lots of whitespace
< <beginfold id='7'>/*</beginfold id='7'>starting wrap<endfold id='7'>*/</endfold id='7'> ></ <beginfold id='7'>/*</beginfold id='7'>ending wrap<endfold id='7'>*/</endfold id='7'>>; // comments in the tags
<beginfold id='6'><></beginfold id='6'>hi<endfold id='6'></></endfold id='6'>; // text inside
<beginfold id='6'><></beginfold id='6'><beginfold id='6'><span</beginfold id='6'>>hi</span<endfold id='6'>></endfold id='6'><beginfold id='6'><div</beginfold id='6'>>bye</div<endfold id='6'>></endfold id='6'><endfold id='6'></></endfold id='6'>; // children
<beginfold id='6'><></beginfold id='6'><beginfold id='6'><span</beginfold id='6'>>1</span<endfold id='6'>></endfold id='6'><beginfold id='6'><></beginfold id='6'><beginfold id='6'><span</beginfold id='6'>>2.1</span<endfold id='6'>></endfold id='6'><beginfold id='6'><span</beginfold id='6'>>2.2</span<endfold id='6'>></endfold id='6'><endfold id='6'></></endfold id='6'><beginfold id='6'><span</beginfold id='6'>>3</span<endfold id='6'>></endfold id='6'><endfold id='6'></></endfold id='6'>; // nested fragments
<beginfold id='6'><></beginfold id='6'>#<endfold id='6'></></endfold id='6'>; // # would cause scanning error if not in jsxtext

// Tags after substitutions in templates
<beginfold id='8'>`</beginfold id='8'>aaa${<beginfold id='6'><tag</beginfold id='6'>></tag<endfold id='6'>></endfold id='6'>//comment
    <beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'><beginfold id='4'><A</beginfold id='4'><endfold id='4'>/></endfold id='4'>}<endfold id='8'>`</endfold id='8'>

// Don't highlight tags within type declaration
type T12 = ReturnType<(<T>() => T)>;
type T13 = ReturnType<(<T extends U, U extends number<beginfold id='9'>[</beginfold id='9'><endfold id='9'>]</endfold id='9'>>() => T)>;
type T14 = ReturnType<typeof f1>;
type T15 = ReturnType<(s: string) => void>;

// Don't highlight tags within variable declaration
let myIdentity: <T>(arg: T) => T <noTag/> = <beginfold id='4'><Tag</beginfold id='4'> <endfold id='4'>/></endfold id='4'>;
var myIdentity: <U>(arg: U) => U = identity;
const myIdentity: <beginfold id='10'>{</beginfold id='10'><T>(arg: T): T<endfold id='10'>}</endfold id='10'> = identity;

// Don't highlight tags within interfaces and classes
interface GenericIdentityFn <beginfold id='2'>{</beginfold id='2'>
    <T>(arg: T): T;
    <noTag />
<endfold id='2'>}</endfold id='2'>
class Handler <beginfold id='2'>{</beginfold id='2'>
    info: <T>(arg: T): T <noTag />;
    <beginfold id='6'><tag</beginfold id='6'>> </tag<endfold id='6'>></endfold id='6'>
<endfold id='2'>}</endfold id='2'>

// Highlight "<T extends" as a type assertion, not as a tag
// (the "cast" expression works and isn't supported in the TSX file)
const goodHighlighting = <T extends I>(
  arg: T
) => <beginfold id='2'>{</beginfold id='2'>
  const a = arg
  return a
<endfold id='2'>}</endfold id='2'>
<beginfold id='6'><tag</beginfold id='6'>> text <beginfold id='4'><T</beginfold id='4'> extends I<endfold id='4'>/></endfold id='4'> </tag<endfold id='6'>></endfold id='6'> // Here "<T extends I/>" is a tag

// Check character after tag name, do not highlight invalid tags
<noTag  ?
<noTag  ,
<noTag <beginfold id='7'>/*</beginfold id='7'> comment <endfold id='7'>*/</endfold id='7'> ?
<noTag#
<noTag<beginfold id='7'>/*</beginfold id='7'>comment<endfold id='7'>*/</endfold id='7'>#

// Conditionals expressions
let y = foo == null ? null : <beginfold id='4'><Tag</beginfold id='4'><endfold id='4'>/></endfold id='4'>;
let x = (foo === null || foo === undefined) ?
    undefined :<beginfold id='6'><tag</beginfold id='6'>>hello</tag<endfold id='6'>></endfold id='6'>;

// Tagged template literals
tagFunc<beginfold id='8'>`</beginfold id='8'>
    Hello world!
    ${ <beginfold id='4'><Tag</beginfold id='4'> <endfold id='4'>/></endfold id='4'>; 22 + "11" }<endfold id='8'>`</endfold id='8'>;
obj.something.tagFunc<beginfold id='8'>`</beginfold id='8'>Setting ${setting} is ${value}!<endfold id='8'>`</endfold id='8'>;
