-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
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. |
eba52e7
to
a7144da
Compare
|
||
if (!name) return | ||
|
||
if (name.startsWith('@') && name.includes('/')) { |
There was a problem hiding this comment.
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' }) |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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.
closes google#659 closes google#660
closes google#659 closes google#660
closes google#659 closes google#660
closes google#659 closes google#660
Fixes #659