unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
To: jostein@kjonigsen.net
Cc: emacs-devel@gnu.org
Subject: Re: Comprehensive JSX support in Emacs
Date: Sun, 17 Feb 2019 23:17:10 -0800	[thread overview]
Message-ID: <0a0f1acb-8d44-2a4f-e648-f596fa8dcfb5@jacksonrayhamilton.com> (raw)
In-Reply-To: <4f6a7f78-caaf-4d1d-bf7a-d8415141efe6@www.fastmail.com>

[-- Attachment #1: Type: text/plain, Size: 2851 bytes --]

Hi Jostein,

Thank you for bringing this use case to my attention.

I haven’t worked with TypeScript before, but I took a look at the docs, 
and read through the whole JSX page.  It looks like the syntax for JSX 
is exactly the same in TS as in JS, so my work may have a chance of 
integrating with typescript-mode.

However, skimming through the rest of the docs, there could be 
challenges dealing with code that uses angle brackets that look like 
“tags,” e.g.:

|let list: Array<number> = [1, 2, 3];|

|let square = <Square>{};|

|let counter = <Counter>function (start: number) { };|

|let output = identity<string>("myString");|

|function loggingIdentity<T>(arg: Array<T>): Array<T> { 
console.log(arg.length);return arg; }|

|function loggingIdentity<T extends Lengthwise>(arg: T): T { 
console.log(arg.length);return arg; }|
||

|class GenericNumber<T> { zeroValue: T; add: (x: T, y: T) => T; }|

I understand that “type assertions” will be written with an |as| keyword 
in TSX, but it looks like angle brackets are still used in other places 
in TSX:

|class Component extends React.Component<PropsType, {}> { render() { 
return ( <h2> {this.props.children} </h2> ) } }|

The parser I’ve written to disambiguate JSX from JS in js-mode makes the 
assumption that matching angle brackets around identifiers indicate the 
presence of JSX with certainty.  In order to adapt that code for TS, one 
solution could be to parse more, looking at the surrounding context of 
the angle brackets to determine whether the thing is a TS type or if 
it’s JSX.  If you’re up for that challenge, hooks could be added to the 
parser to perform additional context checks defined by your mode.

Disclaimer: Maybe “parsing more” won’t work, in which case a different 
strategy might be necessary.  Maybe it’s impossible to know the 
difference between a TS type and JSX without an AST… I dunno.  In any 
case, as long as you can figure out what is JSX in TS, and the JSX is 
marked with the same text properties that js-mode will use, then 
integration with the rest of the JSX code (which indents and font-locks) 
should be possible.

Since typescript-mode doesn’t derive from js-mode, I expect you’ll 
manually pull in the JSX functions and variables from js-mode, and call 
the functions and use the variables in similar ways, mirroring js-mode’s 
ways.  I expect you will do these things:

  * Merge JSX font-lock keywords into TypeScript’s
  * Use JSX syntax propertizing
  * Delegate to Emacs’ JSX indentation inside JSX, and go back to
    typescript-mode’s TS indenting inside JSX expressions
  * Enable JSX features for “.tsx” files

I will try to keep the code modular to make it easy for you to pull in 
the parts you need.

Jackson


[-- Attachment #2: Type: text/html, Size: 5624 bytes --]

  reply	other threads:[~2019-02-18  7:17 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-14  5:06 Comprehensive JSX support in Emacs Jackson Ray Hamilton
2019-02-14  8:03 ` Marcin Borkowski
2019-02-14 14:10 ` Stefan Monnier
2019-02-14 15:04   ` Clément Pit-Claudel
2019-02-15  8:21   ` Jackson Ray Hamilton
2019-02-15 13:25     ` Stefan Monnier
2019-02-15 13:54     ` Dmitry Gutov
2019-02-15 14:39 ` Dmitry Gutov
2019-02-16 20:50 ` Jostein Kjønigsen
2019-02-18  7:17   ` Jackson Ray Hamilton [this message]
2019-03-27  8:03 ` Jackson Ray Hamilton
2019-03-27  9:36   ` Marcin Borkowski
2019-03-30  2:18     ` Jackson Ray Hamilton
2019-04-02  1:12       ` Dmitry Gutov
2019-04-06 16:09         ` Jackson Ray Hamilton
2019-03-30  2:08   ` Jackson Ray Hamilton
2019-04-02  1:07   ` Dmitry Gutov
2019-04-02 11:23     ` Stefan Monnier
2019-04-06 16:02       ` Jackson Ray Hamilton
2019-04-07 23:19         ` Jackson Ray Hamilton
2019-04-09  6:06       ` Jackson Ray Hamilton
2019-04-09  8:12         ` Eli Zaretskii
2019-04-10  1:56           ` Jackson Ray Hamilton
2019-04-10 15:43             ` Eli Zaretskii
2019-04-06 15:55     ` Jackson Ray Hamilton
2019-04-07  0:31       ` Dmitry Gutov
2019-04-09  6:16         ` Jackson Ray Hamilton
2019-04-09  7:10           ` Marcin Borkowski
2019-04-09  8:00             ` Jackson Ray Hamilton
2019-04-11 19:51               ` Marcin Borkowski
2019-10-20 16:57                 ` Steinar Bang
  -- strict thread matches above, loose matches on Subject: below --
2019-02-15  6:38 Jackson Ray Hamilton
2019-02-17  6:10 ` Marcin Borkowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0a0f1acb-8d44-2a4f-e648-f596fa8dcfb5@jacksonrayhamilton.com \
    --to=jackson@jacksonrayhamilton.com \
    --cc=emacs-devel@gnu.org \
    --cc=jostein@kjonigsen.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).