Skip to content

Commit

Permalink
fix: relax pkg name pattern
Browse files Browse the repository at this point in the history
closes google#659
closes google#660
  • Loading branch information
antongolub committed Aug 13, 2023
1 parent 483cc88 commit f126d2d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ const importRe = [
/\brequire\(['"](?<path>[^'"]+)['"]\)/,
/\bfrom\s+['"](?<path>[^'"]+)['"]/,
]
const nameRe = /^(?<name>(@[a-z0-9-]+\/)?[a-z0-9-.]+)\/?.*$/i
// 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
const versionRe = /(\/\/|\/\*)\s*@(?<version>[~^]?([\dvx*]+([-.][\dx*]+)*))/i

export function parseDeps(content: Buffer): Record<string, string> {
Expand Down
4 changes: 2 additions & 2 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ test('argv works with zx and node', async () => {
`global {"_":["bar"]}\nimported {"_":["bar"]}\n`
)
assert.is(
(await $`node build/cli.js --eval 'console.log(argv._)' baz`).toString(),
`[ 'baz' ]\n`
(await $`node build/cli.js --eval 'console.log(argv._.join(''))' baz`).toString(),
`baz\n`
)
})

Expand Down
34 changes: 29 additions & 5 deletions test/deps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,35 @@ test('installDeps() loader works via CLI', async () => {
})

test('parseDeps(): import or require', async () => {
assert.equal(parseDeps(`import "foo"`), { foo: 'latest' })
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.js')`), { 'foo.js': 'latest' })
;[
[`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)
})
})

test('parseDeps(): import with org and filename', async () => {
Expand Down

0 comments on commit f126d2d

Please sign in to comment.