diff --git a/ftdetect/javascript.vim b/ftdetect/javascript.vim index 84fb5d6..923218d 100644 --- a/ftdetect/javascript.vim +++ b/ftdetect/javascript.vim @@ -23,10 +23,20 @@ if g:jsx_pragma_required let b:jsx_pragma_found = search(s:jsx_pragma_pattern, 'npw') endif +" if g:jsx_check_react_import == 1 +" parse the first line of js file (skipping comments). When it has a 'react' +" importation, we guess the user writes jsx +" endif +let s:jsx_prevalent_pattern = + \ '\v\C%^\_s*%(%(//.*\_$|/\*\_.{-}\*/)@>\_s*)*%(import\s+\k+\s+from\s+|%(\l+\s+)=\k+\s*\=\s*require\s*\(\s*)[`"'']react>' + " Whether to set the JSX filetype on *.js files. fu! EnableJSX() if g:jsx_pragma_required && !b:jsx_pragma_found | return 0 | endif - if g:jsx_ext_required && !exists('b:jsx_ext_found') | return 0 | endif + if g:jsx_ext_required && !exists('b:jsx_ext_found') && + \ !(get(g:,'jsx_check_react_import') && search(s:jsx_prevalent_pattern, 'nw')) + return 0 + endif return 1 endfu