Skip to content

Commit d2b87c0

Browse files
authored
fix: standardize x x@ and x@* (#97)
BREAKING CHANGE: `x` and `x@` now return the same spec as `x@*` From #45: Right now, `name@` and `name` are parsed with `{type:'tag', fetchSpec: 'latest'}`, but `name@*` is parsed as `{type: 'range'}`. But since `''` is a valid semver range, it should be parsed the same as `*`. This also saves npm-package-arg from guessing the default tag, which currently poses some semantic hazards. npm (via npm-pick-manifest) will prefer its default tag if given a range that includes it. But `name@latest` should always and only resolve to that specific version in the dist-tags. So npm-pick-manifest and pacote have to detect this and do some extra work to figure out if `latest` was actually specified or just guessed as a default. Closes npm/statusboard#460
1 parent e4409eb commit d2b87c0

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

lib/npa.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ function npa (arg, where) {
3939
spec = arg
4040
} else if (nameEndsAt > 0) {
4141
name = namePart
42-
spec = arg.slice(nameEndsAt + 1)
42+
spec = arg.slice(nameEndsAt + 1) || '*'
4343
} else {
4444
const valid = validatePackageName(arg)
4545
if (valid.validForOldPackages) {
4646
name = arg
47+
spec = '*'
4748
} else {
4849
spec = arg
4950
}
@@ -113,7 +114,7 @@ function Result (opts) {
113114
this.name = undefined
114115
this.escapedName = undefined
115116
this.scope = undefined
116-
this.rawSpec = opts.rawSpec == null ? '' : opts.rawSpec
117+
this.rawSpec = opts.rawSpec || ''
117118
this.saveSpec = opts.saveSpec
118119
this.fetchSpec = opts.fetchSpec
119120
if (opts.name) {
@@ -383,7 +384,7 @@ function fromAlias (res, where) {
383384

384385
function fromRegistry (res) {
385386
res.registry = true
386-
const spec = res.rawSpec === '' ? 'latest' : res.rawSpec.trim()
387+
const spec = res.rawSpec.trim()
387388
// no save spec for registry components as we save based on the fetched
388389
// version, not on the argument so this can't compute that.
389390
res.saveSpec = null

test/basic.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ t.test('basic', function (t) {
4444
name: '@foo/bar',
4545
escapedName: '@foo%2fbar',
4646
scope: '@foo',
47-
rawSpec: '',
47+
rawSpec: '*',
4848
saveSpec: null,
49-
fetchSpec: 'latest',
50-
type: 'tag',
49+
fetchSpec: '*',
50+
type: 'range',
5151
},
5252

5353
'@foo/bar@': {
5454
raw: '@foo/bar@',
5555
name: '@foo/bar',
5656
escapedName: '@foo%2fbar',
5757
scope: '@foo',
58-
rawSpec: '',
58+
rawSpec: '*',
5959
saveSpec: null,
60-
fetchSpec: 'latest',
61-
type: 'tag',
60+
fetchSpec: '*',
61+
type: 'range',
6262
},
6363

6464
'@foo/bar@baz': {
@@ -113,11 +113,11 @@ t.test('basic', function (t) {
113113
registry: true,
114114
name: 'bar',
115115
escapedName: 'bar',
116-
type: 'tag',
116+
type: 'range',
117117
raw: 'bar',
118-
rawSpec: '',
118+
rawSpec: '*',
119119
saveSpec: null,
120-
fetchSpec: 'latest',
120+
fetchSpec: '*',
121121
},
122122
},
123123

@@ -366,10 +366,10 @@ t.test('basic', function (t) {
366366
name: 'git',
367367
type: 'alias',
368368
subSpec: {
369-
type: 'tag',
369+
type: 'range',
370370
registry: true,
371371
name: 'not-git',
372-
fetchSpec: 'latest',
372+
fetchSpec: '*',
373373
},
374374
raw: 'git@npm:not-git',
375375
},
@@ -587,9 +587,9 @@ t.test('basic', function (t) {
587587
foo: {
588588
name: 'foo',
589589
escapedName: 'foo',
590-
type: 'tag',
590+
type: 'range',
591591
saveSpec: null,
592-
fetchSpec: 'latest',
592+
fetchSpec: '*',
593593
raw: 'foo',
594594
},
595595

test/realize-package-specifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test('realize-package-specifier', function (t) {
88
result = npa('a.tar.gz', '/test/a/b')
99
t.equal(result.type, 'file', 'local tarball')
1010
result = npa('d', '/test/a/b')
11-
t.equal(result.type, 'tag', 'remote package')
11+
t.equal(result.type, 'range', 'remote package')
1212
result = npa('file:./a.tar.gz', '/test/a/b')
1313
t.equal(result.type, 'file', 'local tarball')
1414
result = npa('file:./b', '/test/a/b')

0 commit comments

Comments
 (0)