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

fix package name regex #659 #660

Closed
wants to merge 1 commit into from
Closed

Conversation

pillsilly
Copy link

@pillsilly pillsilly commented Aug 11, 2023

Fixes #659

It's a good idea to open an issue first for discussion.

  • Tests pass
  • Appropriate changes to README are included in PR
  • Types updated

@google-cla
Copy link

google-cla bot commented Aug 11, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@pillsilly pillsilly force-pushed the fix-regex branch 2 times, most recently from eba52e7 to a7144da Compare August 12, 2023 16:29

if (!name) return

if (name.startsWith('@') && name.includes('/')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let the regex do this job.

@@ -41,6 +41,8 @@ test('parseDeps(): import or require', async () => {
assert.equal(parseDeps(`import * as bar from "foo"`), { foo: 'latest' })
assert.equal(parseDeps(`import('foo')`), { foo: 'latest' })
assert.equal(parseDeps(`require('foo')`), { foo: 'latest' })
assert.equal(parseDeps(`require('foo_abc')`), { foo_abc: 'latest' })
Copy link
Contributor

Choose a reason for hiding this comment

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

And let's also check another missing cases:

test('parseDeps(): import or require', async () => {
  ;[
    [`import "foo"`, { foo: 'latest' }],
    [`import "foo"`, { foo: 'latest' }],
    [`import * as bar from "foo"`, { foo: 'latest' }],
    [`import('foo')`, { foo: 'latest' }],
    [`require('foo')`, { foo: 'latest' }],
    [`require('foo/bar')`, { foo: 'latest' }],
    [`require('foo/bar.js')`, { foo: 'latest' }],
    [`require('foo-bar')`, { 'foo-bar': 'latest' }],
    [`require('foo_bar')`, { foo_bar: 'latest' }],
    [`require('@foo/bar')`, { '@foo/bar': 'latest' }],
    [`require('@foo/bar/baz')`, { '@foo/bar': 'latest' }],
    [`require('foo.js')`, { 'foo.js': 'latest' }],

    // ignores local deps
    [`import '.'`, {}],
    [`require('.')`, {}],
    [`require('..')`, {}],
    [`require('../foo.js')`, {}],
    [`require('./foo.js')`, {}],

    // ignores invalid pkg names
    [`require('_foo')`, {}],
    [`require('@')`, {}],
    [`require('@/_foo')`, {}],
    [`require('@foo')`, {}],
  ].forEach(([input, result]) => {
    assert.equal(parseDeps(input), result)
  })
})

@@ -94,7 +94,8 @@ const importRe = [
/\brequire\(['"](?<path>[^'"]+)['"]\)/,
/\bfrom\s+['"](?<path>[^'"]+)['"]/,
]
const nameRe = /^(?<name>(@[a-z0-9-]+\/)?[a-z0-9-.]+)\/?.*$/i
const nameRe =
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still suggesting smth like

// Adapted from https://github.com/dword-design/package-name-regex/blob/5d8bfe9b6e140e3e114833e68dc1fbf500991f2c/src/index.js#L1C22-L1C22
const nameRe = /^(?<name>(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*)\/?.*$/i

as a bit more readable and maintainable alternative.

antongolub added a commit to antongolub/zx that referenced this pull request Aug 13, 2023
antongolub added a commit to antongolub/zx that referenced this pull request Aug 13, 2023
antongolub added a commit to antongolub/zx that referenced this pull request Aug 15, 2023
antongolub added a commit to antongolub/zx that referenced this pull request Aug 17, 2023
@antonmedv antonmedv closed this in 956dcc3 Aug 17, 2023
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.

--install couldn't perform installation process for dependency packages that has name contains '-'
2 participants