komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components

Home Page:http://komponent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Component generator error with single quotes string

nicolas-brousse opened this issue · comments

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.

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.

Thanks for the instructions, it will help!

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

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

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

I added a failing test case here: #101
and pushed this commit 086be84 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?

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 :)

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