     @patchEditorLangModeSuggestedIndentForBufferRow(editor)
     @patchEditorLangModeAutoDecreaseIndentForBufferRow(editor)
 
+  isJSX: (text) ->
+    docblock = require 'jstransform/src/docblock'
+    doc = docblock.parse text;
+    for b in doc
+      return true if b[0] == 'jsx'
+    false
+
   autoSetGrammar: (editor) ->
+    return if editor.getGrammar().scopeName == "source.js.jsx"
+
+    path = require 'path'
+
     # Check if file extension is .jsx or the file has the old JSX notation
-    if path.extname(editor.getPath()) is ".jsx" or isJSX(editor.getText())
+    extName = path.extname(editor.getPath())
+    if extName is ".jsx" or (extName is ".js" and @isJSX(editor.getText()))
       jsxGrammar = atom.syntax.grammarsByScopeName["source.js.jsx"]
       editor.setGrammar jsxGrammar if jsxGrammar
 
   onHTMLToJSX: ->
     jsxformat = require 'jsxformat'
     HTMLtoJSX = require './lib/htmltojsx'
-    converter = new HTMLtoJSX({
-      createClass: false
-    })
+    converter = new HTMLtoJSX(createClass: false)
 
     editor = atom.workspace.getActiveEditor()
+
+    return if not editor?
+
     selections = editor.getSelections()
 
     editor.transact =>

path   = require 'path'
{Subscriber} = require 'emissary'

# use same docblock parser as jsxtransformer does
isJSX = (text) ->
  docblock = require 'jstransform/src/docblock'
  doc = docblock.parse text;
  for b in doc
    return true if b[0] == 'jsx'
  false

class AtomReact
  Subscriber.includeInto(this)

    atom.config.set("react.decreaseIndentForNextLinePattern", decreaseIndentForNextLinePattern)

    # Bind events
    atom.commands.add 'atom-workspace', 'react:reformat-JSX', @onReformat
    atom.commands.add 'atom-workspace', 'react:HTML-to-JSX', @onHTMLToJSX

    atom.workspace.observeTextEditors @processEditor.bind(this)

module.exports = new AtomReact
