Skip to content

detect on common react patterns#130

Open
bounceme wants to merge 1 commit into
mxw:masterfrom
bounceme:patch-2
Open

detect on common react patterns#130
bounceme wants to merge 1 commit into
mxw:masterfrom
bounceme:patch-2

Conversation

@bounceme

@bounceme bounceme commented Mar 4, 2017

Copy link
Copy Markdown
Contributor

maybe it should be an option though

@hawkins

hawkins commented Aug 1, 2017

Copy link
Copy Markdown

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

mxw commented Aug 1, 2017

Copy link
Copy Markdown
Owner

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

@mxw

mxw commented Aug 2, 2017

Copy link
Copy Markdown
Owner

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

Comment thread ftdetect/javascript.vim Outdated
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
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Undo this change?

Comment thread ftdetect/javascript.vim Outdated
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
Copy Markdown
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
Copy Markdown
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

Comment thread ftdetect/javascript.vim Outdated
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
Copy Markdown
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.

Comment thread ftdetect/javascript.vim Outdated
" 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Comment thread ftdetect/javascript.vim
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
Copy Markdown
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

mxw commented Aug 3, 2017

Copy link
Copy Markdown
Owner

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

@bounceme

bounceme commented Aug 3, 2017

Copy link
Copy Markdown
Contributor Author

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

@bounceme

Copy link
Copy Markdown
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.

3 participants