Skip to content

Commit

Permalink
fix: get actual protocol for windows instead of protocol + drive (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 5, 2022
1 parent 357a7c0 commit 15140cc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
8 changes: 0 additions & 8 deletions src/_utils.ts
Expand Up @@ -38,11 +38,3 @@ export function matchAll (regex, string, addition) {
}
return matches
}

// 2+ letters, to exclude Windows drive letters
const ProtocolRegex = /^(?<proto>.{2,}):.+$/

export function getProtocol (id: string): string | null {
const proto = id.match(ProtocolRegex)
return proto ? proto.groups.proto : null
}
3 changes: 1 addition & 2 deletions src/syntax.ts
Expand Up @@ -2,8 +2,7 @@ import { promises as fsp } from 'fs'
import { extname } from 'pathe'
import { readPackageJSON } from 'pkg-types'
import { ResolveOptions, resolvePath } from './resolve'
import { isNodeBuiltin } from './utils'
import { getProtocol } from './_utils'
import { isNodeBuiltin, getProtocol } from './utils'

const ESM_RE = /([\s;]|^)(import[\w,{}\s*]*from|import\s*['"*{]|export\b\s*(?:[*{]|default|type|function|const|var|let|async function)|import\.meta\b)/m

Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Expand Up @@ -51,3 +51,12 @@ export function isNodeBuiltin (id: string = '') {
id = id.replace(/^node:/, '').split('/')[0]
return BUILTIN_MODULES.has(id)
}

// 2+ letters, to exclude Windows drive letters
// "{2,}?" to make in ungreedy and dont take "file://C" as protocol
const ProtocolRegex = /^(?<proto>.{2,}?):.+$/

export function getProtocol (id: string): string | null {
const proto = id.match(ProtocolRegex)
return proto ? proto.groups.proto : null
}
15 changes: 14 additions & 1 deletion test/utils.test.mjs
@@ -1,4 +1,4 @@
import { isNodeBuiltin, sanitizeFilePath } from 'mlly'
import { isNodeBuiltin, sanitizeFilePath, getProtocol } from 'mlly'
import { expect } from 'chai'

describe('isNodeBuiltin', () => {
Expand Down Expand Up @@ -41,3 +41,16 @@ describe('sanitizeFilePath', () => {
expect(sanitizeFilePath()).to.equal('')
})
})

describe('getProtocol', () => {
it('no protocol', () => {
expect(getProtocol('/src/a.ts')).to.equal(null)
expect(getProtocol('C:/src/a.ts')).to.equal(null)
})

it('file protocol', () => {
expect(getProtocol('file://src/a.ts')).to.equal('file')
expect(getProtocol('file://C:/src/a.ts')).to.equal('file')
expect(getProtocol('file:///C:/src/a.ts')).to.equal('file')
})
})

0 comments on commit 15140cc

Please sign in to comment.