Skip to content

Latest commit

 

History

History
199 lines (165 loc) · 9.94 KB

README.org

File metadata and controls

199 lines (165 loc) · 9.94 KB

React layer

img/react.png

Table of Contents

Description

ES6 and JSX ready configuration layer for React It will automatically recognize .jsx and .react.js files

Features

  • on-the-fly syntax checking
  • proper syntax highlight and indentation with jsx
  • ternjs turbocharged autocompletion as in js2-mode
  • jsfmt automatic formatting
  • js2-refactor
  • js-doc

Install

To use this configuration layer, add it to your ~/.spacemacs. You will need to add react to the existing dotspacemacs-configuration-layers list in this file.

You will also need to install tern to use the auto-completion and documentation features:

$ npm install -g tern

To use the on-the-fly syntax checking, install eslint with babel and react support:

$ npm install -g eslint babel-eslint eslint-plugin-react

If your project do not use a custom .eslintrc file I strongly advice you to try out this one by Airbnb: .eslintrc

In order to use automatic code formatting you need to install js-beautify with:

$ npm install -g js-beautify

Be sure to have the e4x option set to true on your .jsbeautifyrc here it is my configuration as an example:

{
  "indent_size": 2,
  "indent_char": " ",
  "eol": "\n",
  "indent_level": 0,
  "indent_with_tabs": false,
  "preserve_newlines": true,
  "max_preserve_newlines": 2,
  "jslint_happy": false,
  "space_after_anon_function": false,
  "brace_style": "collapse",
  "keep_array_indentation": false,
  "keep_function_indentation": false,
  "space_before_conditional": true,
  "break_chained_methods": true,
  "eval_code": false,
  "unescape_strings": false,
  "wrap_line_length": 80,
  "wrap_attributes": "auto",
  "wrap_attributes_indent_size": 2,
  "e4x": true,
  "end_with_newline": true
}

Optional Configuration

You may refer to the web-mode configuration for fine tuning the indenting behaviour.

For example to have a consistent 2 spaces indenting both on js and jsx you may use these settings:

(setq-default
 ;; js2-mode
 js2-basic-offset 2
 ;; web-mode
 css-indent-offset 2
 web-mode-markup-indent-offset 2
 web-mode-css-indent-offset 2
 web-mode-code-indent-offset 2
 web-mode-attr-indent-offset 2)

And if you want to have 2 space indent also for element’s attributes, concatenations and contiguous function calls:

(with-eval-after-load 'web-mode
  (add-to-list 'web-mode-indentation-params '("lineup-args" . nil))
  (add-to-list 'web-mode-indentation-params '("lineup-concats" . nil))
  (add-to-list 'web-mode-indentation-params '("lineup-calls" . nil)))

Key Bindings

js2-mode

Key BindingDescription
SPC m wtoggle js2-mode warnings and errors

Folding (js2-mode)

Key BindingDescription
SPC m z chide element
SPC m z oshow element
SPC m z rshow all element
SPC m z etoggle hide/show element
SPC m z Ftoggle hide functions
SPC m z Ctoggle hide comments

Refactoring (js2-refactor)

Bindings should match the plain emacs assignments.

Key BindingDescription
SPC x m jmove line down, while keeping commas correctly placed
SPC x m kmove line up, while keeping commas correctly placed
SPC m kdeletes to the end of the line, but does not cross semantic boundaries
SPC m r 3 iconverts ternary operator to if-statement
SPC m r a gcreates a /* global */ annotation if it is missing, and adds var to point to it
SPC m r a oreplaces arguments to a function call with an object literal of named arguments
SPC m r b amoves the last child out of current function, if-statement, for-loop or while-loop
SPC m r c aconverts a multiline array to one line
SPC m r c oconverts a multiline object literal to one line
SPC m r c uconverts a multiline function to one line (expecting semicolons as statement delimiters)
SPC m r e aconverts a one line array to multiline
SPC m r e fextracts the marked expressions into a new named function
SPC m r e mextracts the marked expressions out into a new method in an object literal
SPC m r e oconverts a one line object literal to multiline
SPC m r e uconverts a one line function to multiline (expecting semicolons as statement delimiters)
SPC m r e vtakes a marked expression and replaces it with a var
SPC m r i gcreates a shortcut for a marked global by injecting it in the wrapping immediately invoked function expression
SPC m r i pchanges the marked expression to a parameter in a local function
SPC m r i vreplaces all instances of a variable with its initial value
SPC m r l pchanges a parameter to a local var in a local function
SPC m r l tadds a console.log statement for what is at point (or region)
SPC m r r vrenames the variable on point and all occurrences in its lexical scope
SPC m r s lmoves the next statement into current function, if-statement, for-loop, while-loop
SPC m r s ssplits a String
SPC m r s vsplits a var with multiple vars declared into several var statements
SPC m r t ftoggle between function declaration and function expression
SPC m r u wreplaces the parent statement with the selected region
SPC m r v tchanges local var a to be this.a instead
SPC m r w iwraps the entire buffer in an immediately invoked function expression
SPC m r w lwraps the region in a for-loop

Formatting (web-beautify)

Key BindingDescription
SPC m =beautify code in js2-mode, json-mode, web-mode, and css-mode

Documentation (js-doc)

You can check more here

Key BindingDescription
SPC m r d binsert JSDoc comment for current file
SPC m r d finsert JSDoc comment for function
SPC m r d tinsert tag to comment
SPC m r d hshow list of available jsdoc tags

Auto-complete and documentation (tern)

Key BindingDescription
SPC m C-gbrings you back to last place you were when you pressed M-..
SPC m g gjump to the definition of the thing under the cursor
SPC m g Gjump to definition for the given name
SPC m h dfind docs of the thing under the cursor. Press again to open the associated URL (if any)
SPC m h tfind the type of the thing under the cursor
SPC m r r Vrename variable under the cursor using tern

JSON

Key BindingDescription
SPC m h pGet the path of the value at point