komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components
http://komponent.io
MIT License
427 stars 31 forks source link

Component generator error with single quotes string #100

Closed nicolas-brousse closed 6 years ago

nicolas-brousse commented 6 years ago

On a project we use single quotes on js files. So our frontend/components/index.js has list of components with single quotes.

The problem with when we generate a new component, the import_to_packs method seems to remove all components import and adding the new one.

- import "components/alert/alert";
- import "components/button/button";
- import "components/container/container";
- import "components/footer/footer";
- import "components/navigation/navigation";
- import "components/pagination/pagination";
+ import "components/test/test";

Not sure what could be done. May be using the Komponent::Component.all method a created on #95, could help us to do the import list directly by getting the list of present components.

nicolas-brousse commented 6 years ago

How to reproduce the bug

$ rails new demo-app --webpack
$ cd demo-app
$ echo "gem 'komponent'" >> Gemfile
$ bundle
$ rails generate komponent:install
$ rails generate component container
$ rails generate component button

After what I got this inside frontend/components/index.js:

import "components/button/button";
import "components/container/container";

Ok, now I convert double to single quotes inside this file, then I generate a new component rails generate component form.

And frontend/components/index.js now contain:

import "components/form/form";

My two previous components are absent from index.js now.

Spone commented 6 years ago

Thanks for the instructions, it will help!

nicolas-brousse commented 6 years ago

Maybe the error is due to sort_lines_alphabetically!(path) and not import_to_packs.

Spone commented 6 years ago

I think you're right. Can you try modifying the regex from this line?

We would need /^import ["'](.*)["'];$/ to support simple quotes.

Spone commented 6 years ago

I added a failing test case here: https://github.com/komposable/komponent/pull/101 and pushed this commit https://github.com/komposable/komponent/pull/101/commits/086be84f66e0483b97f63960095e49b26f557b98 to fix the issue.

But I'm not sure what behavior we want... Should we rewrite the whole index file with single or double quotes? How do we check what is the expected syntax for the current project?

nicolas-brousse commented 6 years ago

I think to start it's not really a problem to continue adding new component with double quotes. The main think is to not remove previous added components :)

Spone commented 6 years ago

Okay, let's merge this fix for now, and we may improve this later.