Skip to content
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

TSX Files, Typescript, and React #7344

Closed
jupl opened this issue Oct 10, 2016 · 14 comments
Closed

TSX Files, Typescript, and React #7344

jupl opened this issue Oct 10, 2016 · 14 comments
Labels
Beginner friendly Documentation ✏ Ready for work stale marked as a stale issue/pr (usually by a bot)

Comments

@jupl
Copy link
Contributor

jupl commented Oct 10, 2016

Description :octocat:

When a tsx file is opened, it is treated as a JS file with react-mode and without TypeScript's helpers. What's being used to autoselect react-mode should not be applicable to TypeScript files. A more long term solution would be to support tsx by incorporating pieces of the react layer into the typescript layer (or vice versa) but at least for now stopping the mode change would be helpful. EDIT: I see that tsx uses web-mode.

Reproduction guide 🪲

  • Ensure typescript and react layers are installed.
  • Start Emacs
  • Open an existing file like the following named template.tsx:
import React from 'react' // Alowed with allowSyntheticDefaultImports
import {PAGE_STYLE} from './styles'

/**
 * Render root component representing the entire application
 * @return Root component
 */
export default function Root() {
  return <div style={PAGE_STYLE}>Welcome</div>
}

Observed behaviour: 👀 💔
File is opened in react-mode with JS packages.

Expected behaviour: ❤️ 😄
File is opened in typescript-mode (or maybe react-mode) web-mode with TS packages.

System Info 💻

  • OS: darwin
  • Emacs: 25.1.1
  • Spacemacs: 0.200.1
  • Spacemacs branch: master (rev. 3706a42)
  • Graphic display: t
  • Distribution: spacemacs
  • Editing style: vim
  • Completion: helm
  • Layers:
(typescript colors docker editorconfig emacs-lisp html javascript markdown org-plus osx react restclient shell shell-scripts syntax-checking vimscript yaml
            (auto-completion :variables auto-completion-return-key-behavior nil auto-completion-tab-key-behavior 'complete auto-completion-enable-help-tooltip t)
            (clojure :variables clojure-enable-fancify-symbols t)
            (git :variables git-magit-status-fullscreen t)
            (version-control :variables version-control-diff-tool 'diff-hl))
@TheBB
Copy link
Contributor

TheBB commented Oct 10, 2016

This is a consequence of magic-mode-alist having a higher priority than auto-mode-alist. Should be easy to fix with custom match functions though.

@BrianMcKeever
Copy link

I fresh installed emacs and spacemacs on windows 10. I enabled typescript, react, html, and javascript layers, and .tsx files open in foundation mode for me.

@BrianMcKeever
Copy link

Of course, immediately after posting that, it loads correctly in web-mode. I don't think I did anything to fix it. The last thing I changed was the start directory on my emacs shortcut.

@jupl
Copy link
Contributor Author

jupl commented Oct 14, 2016

Updated after seeing that there is already proper support for TSX files. :)

@RafaelCosman
Copy link

With a .tsx file in Web mode, how does one format the buffer? SPC m = uses web mode's formatting which is terrible for js.

@mattnedrich
Copy link

Does anyone know if this is being worked on?

@vegansk
Copy link

vegansk commented Apr 14, 2017

+1

@pmiddend
Copy link

This is highly annoying. Can we at least hack it so that SPC m = uses typescript/format instead of web-html-beautify?

@NotBrianZach
Copy link

NotBrianZach commented May 15, 2017

@pmiddend it shouldn't be too hard to do that. it's somewhat of a upstream issue (somewhat).
I had similar issues switching to es-beautifier to get nice formating of react components based on eslint rules.
Basically what you can do is (and this worked for me in my situation)

  1. fork web-beautify (https://github.com/yasuyk/web-beautify/pull/28/files changes should be similar to this, except with tslint or whatever)
  2. make a private spacemacs layer (copy typescript layer I suppose)
  3. clone your web beautify fork into your private layer's local directory (see spacemacs docs)
  4. modify your layer's package.el file to use the local version of web-beautify, make sure to include commands from local package in the use-package command for web-beautify in the private layers package.el file.

Also I agree it's extremely annoying. This took me a couple days to figure out.

@coatlicue89
Copy link

Quick workaround:

(add-to-list 'auto-mode-alist '("\\.tsx\\'" . typescript-mode))

in dotspacemacs/user-config

@jonhermansen
Copy link
Contributor

Quick workaround:

(add-to-list 'auto-mode-alist '("\\.tsx\\'" . typescript-mode))

in dotspacemacs/user-config

If html layer is not added to .spacemacs, typescript-mode does not load by default for .tsx files (on develop branch). If the workaround above is used, there is no HTML syntax highlighting. Took me a while to figure this out.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!

@github-actions github-actions bot added the stale marked as a stale issue/pr (usually by a bot) label Apr 12, 2020
@Arelav
Copy link

Arelav commented May 12, 2020

Quick workaround:

(add-to-list 'auto-mode-alist '("\\.tsx\\'" . typescript-mode))

in dotspacemacs/user-config

If html layer is not added to .spacemacs, typescript-mode does not load by default for .tsx files (on develop branch). If the workaround above is used, there is no HTML syntax highlighting. Took me a while to figure this out.

Thanks. Finally, TSX works for me. I think it should be documented to support TSX both HTML and TS layers should be enabled.
All times I tried it was adding the typescript layer automatically. And I have only one index.html in the project and open it very rarely.

@JAremko JAremko added updated This issue has been updated since becoming stale Documentation ✏ Ready for work and removed stale marked as a stale issue/pr (usually by a bot) updated This issue has been updated since becoming stale Bug :-( - Bug tracker - labels May 26, 2020
SiriusDely added a commit to SiriusDely/.files that referenced this issue Nov 24, 2021
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!

@github-actions github-actions bot added the stale marked as a stale issue/pr (usually by a bot) label Mar 14, 2022
@github-actions github-actions bot closed this as completed May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Beginner friendly Documentation ✏ Ready for work stale marked as a stale issue/pr (usually by a bot)
Projects
None yet
Development

No branches or pull requests