Sitelet https://github.com/import-js/eslint-plugin-import/pull/744
Skip to content

Support scoped modules containing hyphens - #744

Merged
ljharb merged 1 commit into
import-js:masterfrom
rosswarren:master
Jul 25, 2017
Merged

ljharb merged 1 commit into
import-js:masterfrom
rosswarren:master

Conversation

@rosswarren

Copy link
Copy Markdown
Contributor

I was having problems with the order rule and scoped modules. It was returning NaN for the rank. I discovered the regex was not matching our scope name which contained hyphens.

This regex should match the full range of scoped module names I think. Please check it though.

@coveralls

coveralls commented Feb 13, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling fc2569c on rosswarren:master into 0800951 on benmosher:master.

Comment thread src/core/importType.js
return externalModuleRegExp.test(name) && isExternalPath(path, name, settings)
}

const scopedRegExp = /^@\w+\/\w+/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

would this be simpler as /^@[^\/]+\/[^\/]+?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, just tested this on our codebase and that works 👍 I have updated the pull request.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling 2fb246d on rosswarren:master into 0800951 on benmosher:master.

2 similar comments
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling 2fb246d on rosswarren:master into 0800951 on benmosher:master.

@coveralls

coveralls commented Feb 14, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling 2fb246d on rosswarren:master into 0800951 on benmosher:master.

@ljharb

ljharb commented Feb 14, 2017

Copy link
Copy Markdown
Member

Instead of regexes, should we use the official https://github.com/npm/validate-npm-package-name ? It'll be a scoped package if it's an invalid old one but a valid new one, and we won't have to maintain our own logic.

@rosswarren

Copy link
Copy Markdown
Contributor Author

@ljharb I am not sure if that will help us with detection of scoped modules 🤔 . It will validate but not tell us what type it is.

@ljharb

ljharb commented Feb 14, 2017

Copy link
Copy Markdown
Member

It returns two booleans, "old" and "new" - new meaning, including scoped. If they're both true it's scoped, if just "old" is true it's unscoped, if both are false, it's invalid.

@rosswarren

Copy link
Copy Markdown
Contributor Author

@ljharb unfortunately this isn't how it works

capture

@ljharb

ljharb commented Feb 14, 2017

Copy link
Copy Markdown
Member

@rosswarren what about checking "new", and if it starts with an "@"?

That's an official npm package, and it'd really be nice to use that instead of messing around with regexps.

@rosswarren

Copy link
Copy Markdown
Contributor Author

@ljharb OK, good idea. I will get it done!

@rosswarren

Copy link
Copy Markdown
Contributor Author

@ljharb hmm, I tried to implement this but it seems that the isScoped function is passed the whole import for example @scope/donthaveit/lib/foo rather than just the package which would be @scope/donthaveit so you would still need to try and parse out the package name with some code or a regex.

@rosswarren

Copy link
Copy Markdown
Contributor Author

@ljharb would it be a problem to only check if the first character is an "@"?. From NPM docs "If a package's name begins with @, then it is a scoped package". Not sure we really need to check anything else.

@ljharb

ljharb commented Feb 15, 2017

Copy link
Copy Markdown
Member

@rosswarren Probably - but @foo isn't a scoped package, and could be a webpack alias - same with @.

@rosswarren

Copy link
Copy Markdown
Contributor Author

@ljharb do you have any further suggestion given my previous comment? Not sure how to proceed here.

@jfmengels jfmengels left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd be fine with sticking to a regex in this case. If someone can make it work using validate-npm-package-name, even better. But as @rosswarren said, we pass the whole path (which can be like @foo/bar/folder/file.js), which the package doesn't validate, and you'd pretty much need a regex anyway to extract the data.

it("should return 'external' for scopes packages", function() {
expect(importType('@cycle/core', context)).to.equal('external')
expect(importType('@cycle/dom', context)).to.equal('external')
expect(importType('@some-thing/something', context)).to.equal('external')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you add additional tests for the following cases? (and more if you think of some, better safe than sorry)

  • @something/some-thing
  • @something/something/something

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

have added 👍

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling 1fe3b4a on rosswarren:master into b5a962f on benmosher:master.

1 similar comment
@coveralls

coveralls commented Feb 17, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling 1fe3b4a on rosswarren:master into b5a962f on benmosher:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling bd243b2 on rosswarren:master into 98e7048 on benmosher:master.

@ljharb

ljharb commented Mar 4, 2017

Copy link
Copy Markdown
Member

You could use fs methods to split up the path, without using a regex, but that's probably fine to leave for a future PR.

@rosswarren would you mind rebasing, so as to remove all merge commits?

@coveralls

coveralls commented Mar 4, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling 4d03274 on rosswarren:master into 98e7048 on benmosher:master.

@rosswarren

Copy link
Copy Markdown
Contributor Author

@ljharb yeah that would require some more thinking, there is probably a better solution for this. However, right now of our code importing scoped modules is full of extra line breaks so I am anxious to see this fixed 😄

And yeah no problem I rebased the branch 👍

@afairb

afairb commented Apr 12, 2017

Copy link
Copy Markdown

Hi. I see this PR is approved but not merged quite yet. Is there any chance it could get in soon as it's causing issues in my project when importing scoped modules? Thanks!

@ljharb

ljharb commented Apr 12, 2017

Copy link
Copy Markdown
Member

The Windows build is still failing.

@coveralls

coveralls commented Jun 5, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 95.165% when pulling 0ad9659 on rosswarren:master into 3c46d30 on benmosher:master.

@ljharb
ljharb requested review from benmosher and jfmengels June 5, 2017 16:12
@danny-andrews

danny-andrews commented Jul 25, 2017 •

Copy link
Copy Markdown
Contributor

Can we get this merged? I'm having this same issue. Is the Windows build still failing?

@danny-andrews

danny-andrews commented Jul 25, 2017 •

Copy link
Copy Markdown
Contributor

You could use fs methods to split up the path, without using a regex, but that's probably fine to leave for a future PR.

Just out of curiosity, how would you do this? @ljharb

@ljharb

ljharb commented Jul 25, 2017

Copy link
Copy Markdown
Member

@danny-andrews a combo of path.parse, splitting on path.delimiter, etc.

@ljharb
ljharb merged commit 40f5635 into import-js:master Jul 25, 2017
@danny-andrews

Copy link
Copy Markdown
Contributor

Siiiiick. Thanks for merging!

@danny-andrews

Copy link
Copy Markdown
Contributor

This looks like a good solution to this: https://github.com/mattdesl/require-package-name.

@ljharb

ljharb commented Aug 9, 2017

Copy link
Copy Markdown
Member

It doesn't seem to work for relative paths tho.

@danny-andrews

Copy link
Copy Markdown
Contributor

Brutal. Could use it in combination with https://www.npmjs.com/package/relative-require-regex?
Maybe not worth it for solving such a simple problem doe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

6 participants