Skip to content

Commit

Permalink
add tests for path pattern resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Jan 24, 2024
1 parent d3e2912 commit 748a481
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"no-return-assign": 0,
"no-use-before-define": [2, "nofunc"],
"no-nested-ternary": 0,
"array-bracket-spacing": [2, "always"],
"array-bracket-spacing": [2, "never"],
"prefer-destructuring": 0,
"class-methods-use-this": 0,
"no-confusing-arrow": 0
Expand Down
14 changes: 7 additions & 7 deletions resolvewithplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const esmStrGlobRe = /(\*)/g
const esmStrPathCharRe = /([./])/g
const protocolNode = /^node:/
const protocolFile = /^file:/
const supportedExtensions = [ '.js', '.mjs', '.ts', '.tsx', '.json', '.node' ]
const supportedExtensions = ['.js', '.mjs', '.ts', '.tsx', '.json', '.node']
const supportedIndexNames = supportedExtensions.map(extn => `index${extn}`)
const node_modules = 'node_modules'
const packagejson = 'package.json'
Expand Down Expand Up @@ -101,7 +101,7 @@ const firstSyncFilePath = (dir, fileslist) => {
// c. let I = I - 1
// 5. return DIRS
const getasnode_module_paths = (start, parts = start.split(path.sep)) => {
const next_module_paths = (parts, tuple = [ [], [] ]) => {
const next_module_paths = (parts, tuple = [[], []]) => {
if (!parts.length)
return tuple[1]

Expand Down Expand Up @@ -169,7 +169,6 @@ const getesmkeyvalglobreplaced = (esmkey, esmval, pathlocal) => {
const isesmkeymatchRe = new RegExp(
esmkey.replace(esmStrPathCharRe, '\\$1').replace(esmStrGlobRe, '(.*)'))

// eslint-disable-next-line prefer-destructuring
const globmatch = (pathlocal.match(isesmkeymatchRe) || [])[1]
return globmatch && esmval.replace('*', globmatch)
}
Expand Down Expand Up @@ -247,7 +246,7 @@ const esmparselist = (list, spec, specifier, opts, key = list[0]) => {
}

const esmparse = (spec, specifier, opts = {}) => {
const priority = opts.priority || [ specruntime, specdefault ]
const priority = opts.priority || [specruntime, specdefault]
let indexval = false

if (typeof spec === 'string')
Expand Down Expand Up @@ -514,7 +513,7 @@ const esmparseimportpkg = (pspecifier, start, opts) => {
// b. LOAD_AS_DIRECTORY(DIR/X)
//
const getasnode_module = (targetpath, start, opts) => {
const [ pname, pspecifier ] = gettargetnameandspecifier(targetpath)
const [pname, pspecifier] = gettargetnameandspecifier(targetpath)

if (isESMImportSubpathRe.test(pname))
return esmparseimportpkg(targetpath, start, opts)
Expand Down Expand Up @@ -576,7 +575,7 @@ const createopts = (moduleId, parent, opts) => {
// packagejson mock to more easily test different patternsn
opts.packagejsonmap = opts.packagejsonmap || null
if (!Array.isArray(opts.priority)) {
opts.priority = opts.isbrowser ? [ specbrowser ] : []
opts.priority = opts.isbrowser ? [specbrowser] : []
opts.priority.push(spectype)
opts.priority.push(specruntime)
opts.priority.push(specdefault)
Expand Down Expand Up @@ -609,5 +608,6 @@ export default Object.assign(resolvewith, {
})

export {
gettargetindextop
gettargetindextop,
getesmkeyvalglobreplaced
}
48 changes: 35 additions & 13 deletions tests/tests-basic/tests-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import url from 'url'
import path from 'path'
import test from 'node:test'
import assert from 'node:assert/strict'

import resolvewithplus, {
gettargetindextop
gettargetindextop,
getesmkeyvalglobreplaced
} from '../../resolvewithplus.js'

const tofileurl = p => url.pathToFileURL(p).href
Expand Down Expand Up @@ -349,10 +351,10 @@ test('should handle mixed exports, import', () => {
type: 'module',
exports: {
'./package.json': './package.json',
'.': [ {
'.': [{
import: resolvingpackagejsonmodulerelpath,
require: resolvingpackagejsonmodulerelpathother
}, './index.cjs' ],
}, './index.cjs'],
'./helpers': {
import: resolvingpackagejsonmodulerelpathother,
require: resolvingpackagejsonmodulerelpathother
Expand All @@ -361,10 +363,10 @@ test('should handle mixed exports, import', () => {
import: resolvingpackagejsonmodulerelpathother,
types: resolvingpackagejsonmodulerelpathother
},
'./yargs': [ {
'./yargs': [{
import: resolvingpackagejsonmodulerelpathother,
require: resolvingpackagejsonmodulerelpathother
}, './yargs' ]
}, './yargs']
}
}
}
Expand All @@ -382,10 +384,10 @@ test('should handle mixed exports, commonjs', () => {
type: 'commonjs',
exports: {
'./package.json': './package.json',
'.': [ {
'.': [{
import: resolvingpackagejsonmodulerelpathother,
require: resolvingpackagejsonmodulerelpath
}, resolvingpackagejsonmodulerelpathother ],
}, resolvingpackagejsonmodulerelpathother],
'./helpers': {
import: resolvingpackagejsonmodulerelpathother,
require: resolvingpackagejsonmodulerelpathother
Expand All @@ -394,10 +396,10 @@ test('should handle mixed exports, commonjs', () => {
import: resolvingpackagejsonmodulerelpathother,
types: './browser.d.ts'
},
'./yargs': [ {
'./yargs': [{
import: resolvingpackagejsonmodulerelpathother,
require: resolvingpackagejsonmodulerelpathother
}, resolvingpackagejsonmodulerelpathother ]
}, resolvingpackagejsonmodulerelpathother]
}
}
}
Expand Down Expand Up @@ -514,7 +516,7 @@ test('resolve full path for older main, browser and export fields', () => {

test('should return browser or import whichiver first', () => {
const resolvedbrowser = resolvewithplus('test', import.meta.url, {
priority: [ 'browser', 'import', 'default' ],
priority: ['browser', 'import', 'default'],
packagejsonmap: {
[resolvingpackagejsonpath]: {
name: 'test',
Expand All @@ -533,7 +535,7 @@ test('should return browser or import whichiver first', () => {
assert.strictEqual(resolvedbrowser, resolvingpackagejsonmoduleurlpath)

const resolveddefault = resolvewithplus('test', import.meta.url, {
priority: [ 'default', 'browser', 'import' ],
priority: ['default', 'browser', 'import'],
packagejsonmap: {
[resolvingpackagejsonpath]: {
name: 'test',
Expand Down Expand Up @@ -588,7 +590,7 @@ test('should detect module type from package.json', () => {
assert.strictEqual(resolvedmodule, resolvingpackagejsonmoduleurlpath)

const resolvedmodule2 = resolvewithplus('test', import.meta.url, {
priority: [ 'import', 'browser', 'default' ],
priority: ['import', 'browser', 'default'],
packagejsonmap: {
[resolvingpackagejsonpath]: {
name: 'test',
Expand Down Expand Up @@ -663,7 +665,7 @@ test('gettargetindextop should resolve a fullpath', () => {
version: '4.1.0',
description: 'Light-weight Fetch ...',
module: resolvingpackagejsonmodulerelpath
}, { priority: [ 'import' ] }, dir)
}, { priority: ['import'] }, dir)

assert.strictEqual(
indexpathmodule,
Expand All @@ -680,3 +682,23 @@ test('gettargetindextop should resolve a fullpath', () => {
indexpathmain,
url.fileURLToPath(resolvingpackagejsonmoduleurlpath))
})

test('getesmkeyvalglobreplaced should expand globby path groups', () => {
[[
'src/mystuff/index.js',
'mystuff/*', 'src/mystuff/*', 'mystuff/index.js'
], [
'types/mystuff/index.d.ts',
'mystuff/*', 'types/mystuff/*', 'mystuff/index.d.ts'
], [
'./src/features/x.js',
'./features/*.js', './src/features/*.js', './features/x.js'
], [
'./src/features/y/y.js',
'./features/*.js', './src/features/*.js', './features/y/y.js'
], [
'./src/internal/z.js',
'#internal/*.js', './src/internal/*.js', '#internal/z.js'
]].map(tuple => console.log(tuple) || assert.strictEqual(
getesmkeyvalglobreplaced(...tuple.slice(1)), tuple[0]))
})

0 comments on commit 748a481

Please sign in to comment.