-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ruleset-bundler): improve package import heuristics (#1985)
- Loading branch information
Showing
5 changed files
with
39 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/ruleset-bundler/src/utils/__tests__/isPackageImport.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { isPackageImport } from '../isPackageImport'; | ||
|
||
describe('isPackageImport util', () => { | ||
it.each([ | ||
'nimma/legacy', | ||
'nimma', | ||
'lodash', | ||
'lodash/get', | ||
'lodash/get.js', | ||
'@stoplight/path', | ||
'@stoplight/spectral-core', | ||
'@stoplight/spectral-core/dist/file.js', | ||
])('given valid %s package import, should return true', input => { | ||
expect(isPackageImport(input)).toBe(true); | ||
}); | ||
|
||
it.each(['', '/nimma/legacy', 'path', 'https://cdn.skypack.dev/@stoplight/spectral-core'])( | ||
'given invalid %s import, should return false', | ||
input => { | ||
expect(isPackageImport(input)).toBe(false); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as validate from 'validate-npm-package-name'; | ||
|
||
const isValidPackageName = (packageName: string): boolean => validate(packageName).validForNewPackages; | ||
|
||
export const isPackageImport = (packageName: string): boolean => { | ||
const fragments = packageName.split('/'); | ||
if (packageName.startsWith('@') && fragments.length >= 2) { | ||
fragments.splice(0, 2, `${fragments[0]}/${fragments[1]}`); | ||
} | ||
|
||
return fragments.every(isValidPackageName); | ||
}; |
This file was deleted.
Oops, something went wrong.