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

importmap.scan incorrect regex #285

Open
vfonic opened this issue Jan 6, 2025 · 0 comments
Open

importmap.scan incorrect regex #285

vfonic opened this issue Jan 6, 2025 · 0 comments

Comments

@vfonic
Copy link

vfonic commented Jan 6, 2025

I added flowbite with support for Importmaps as per their documentation: https://flowbite.com/docs/getting-started/rails/#importmap:

pin "flowbite", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/flowbite.turbo.min.js"

I wanted to add the version number at the end in a comment, just like importmap-rails already added for all the other packages. For example:

pin "@rails/ujs", to: "@rails--ujs.js" # @7.1.400

So I added:

pin "flowbite", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/flowbite.turbo.min.js" # @2.5.2

Unfortunately, this breaks importmap outdated as the regex matches the whole pin "flowbite"... line:

def packages_with_versions
# We cannot use the name after "pin" because some dependencies are loaded from inside packages
# Eg. pin "buffer", to: "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/buffer.js"
importmap.scan(/^pin .*(?<=npm:|npm\/|skypack\.dev\/|unpkg\.com\/)(.*)(?=@\d+\.\d+\.\d+)@(\d+\.\d+\.\d+(?:[^\/\s["']]*)).*$/) |
importmap.scan(/^pin ["']([^["']]*)["'].* #.*@(\d+\.\d+\.\d+(?:[^\s]*)).*$/)
end

Here's the regex tester:
https://rubular.com/r/uzRSGTsdzOLRXW

I've updated the regex here:
https://rubular.com/r/xuUcWPo4r3ePMa

Not sure if this should be the solution. The difference is that, instead of matching any character .* until the end of the line $, I match any non-whitespace character \S and I don't match until the end of the line.

Before:

 > importmap outdated
/Users/viktor/.rvm/gems/ruby-3.4.1/gems/uri-1.0.2/lib/uri/generic.rb:770:in 'URI::Generic#check_path': bad component(expected absolute path component): /[email protected]/dist/flowbite.turbo.min.js" #  (URI::InvalidComponentError)
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/uri-1.0.2/lib/uri/generic.rb:816:in 'URI::Generic#path='
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/importmap-rails-2.1.0/lib/importmap/npm.rb:67:in 'Importmap::Npm#get_package'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/importmap-rails-2.1.0/lib/importmap/npm.rb:21:in 'block in Importmap::Npm#outdated_packages'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/importmap-rails-2.1.0/lib/importmap/npm.rb:17:in 'Array#each'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/importmap-rails-2.1.0/lib/importmap/npm.rb:17:in 'Enumerator#with_object'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/importmap-rails-2.1.0/lib/importmap/npm.rb:17:in 'Importmap::Npm#outdated_packages'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/importmap-rails-2.1.0/lib/importmap/commands.rb:96:in 'Importmap::Commands#outdated'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/thor-1.3.2/lib/thor/command.rb:28:in 'Thor::Command#run'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/thor-1.3.2/lib/thor/invocation.rb:127:in 'Thor::Invocation#invoke_command'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/thor-1.3.2/lib/thor.rb:538:in 'Thor.dispatch'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/thor-1.3.2/lib/thor/base.rb:584:in 'Thor::Base::ClassMethods#start'
        from /Users/viktor/.rvm/gems/ruby-3.4.1/gems/importmap-rails-2.1.0/lib/importmap/commands.rb:159:in '<main>'

After:

> importmap outdated
No outdated packages found

Should I make a PR? Should I add a test? What would be a good location for this test? Should I add a line to https://github.com/rails/importmap-rails/blob/main/test/fixtures/files/outdated_import_map.rb and then add a test to test/npm_test.rb?

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

No branches or pull requests

1 participant