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

detect on common react patterns #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

bounceme
Copy link
Contributor

@bounceme bounceme commented Mar 4, 2017

maybe it should be an option though

@hawkins
Copy link

hawkins commented Aug 1, 2017

Nice idea! I agree though, I know I personally would appreciate this change even more if it could be disabled via an option. While 99% of users importing from react are writing JSX, some environments define import aliases (think to avoid ../../../../../) of commonly-used folder names within their project, so it might not always be safe to assume this. Maybe default on, but allowing us to turn this off would be swell 👍

@mxw
Copy link
Owner

mxw commented Aug 1, 2017

Right; this should be an option, and should also I think skip comments in addition to whitespace.

@mxw
Copy link
Owner

mxw commented Aug 2, 2017

Could you add an option to enable/disable this check?

return 1
endfu

autocmd BufNewFile,BufRead *.jsx let b:jsx_ext_found = 1
autocmd BufNewFile,BufRead *.jsx set filetype=javascript.jsx
autocmd BufNewFile,BufRead *.js
\ if <SID>EnableJSX() | set filetype=javascript.jsx | endif
\ if <SID>EnableJSX() | set filetype=javascript.jsx | endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo this change?

" Whether to set the JSX filetype on *.js files.
fu! <SID>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_prevalent_declaration') && search(s:jsx_prevalent_pattern, 'nw'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this into a separate conditional? We could smush all three checks into one, but they're unrelated and I think it's clearer to separate them (and also probably avoids a line continuation).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that condition is a pre-req. If it was rearranged, it'd be dead code because of the return

" Whether to set the JSX filetype on *.js files.
fu! <SID>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_prevalent_declaration') && search(s:jsx_prevalent_pattern, 'nw'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a little strange to identify the declaration as "the prevalent declaration" rather than "the react import" or "the require react expression". Rename this variable to something like g:jsx_check_react_import or something more self-documenting.

" importation, we guess the user writes jsx
" endif
let s:jsx_prevalent_pattern =
\ '\v\C%^\_s*%(%(//.*\_$|/\*\_.{-1,}\*/)@>\_s*)*%(import\s+\k+\s+from\s+|%(\l+\s+)=\k+\s*\=\s*require\s*\(\s*)[`"'']react>'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm—this regex skips over comments and looks for something that looks like import ... from ... 'react or require( ... 'react (modulo whitespace, choice of quote, etc.). Is that correct, or have I missed something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's it. i did this with the \v flag to reduce the size. pretty fast, and accurate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the first step in a sed js parser haha

" Whether to set the JSX filetype on *.js files.
fu! <SID>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'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's right—I forgot this function was setting the filetype on .js files only, not files in general.

@mxw
Copy link
Owner

mxw commented Aug 3, 2017

Can you rebase this on master? I'm having trouble upstreaming the change.

@bounceme
Copy link
Contributor Author

bounceme commented Aug 3, 2017

what about github.com ? says it is clean and mergeable

@bounceme
Copy link
Contributor Author

should be fine now

bradford-smith94 added a commit to bradford-smith94/dotfiles that referenced this pull request Sep 22, 2017
This uses the vim-jsx plugin to add JSX filetype support, also by
setting g:jsx_ext_required to zero it enables JSX in all *.js files.
It's not ideal but hopefully the plugin's ftdetect will get better at
recognizing React patterns (i.e. when [1] gets merged).

[1]: mxw/vim-jsx#130
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants