Skip to content

Commit

Permalink
make addon detection cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Feb 1, 2024
1 parent 1084b28 commit c1754f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
7 changes: 3 additions & 4 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ exports.parse = function parse (src, type = 'module', strictMode = false) {
type,
resolutions: [],
namedImports: [],
exports: null
exports: null,
addons: []
}

if (type === 'json') {
Expand Down Expand Up @@ -39,7 +40,6 @@ exports.parse = function parse (src, type = 'module', strictMode = false) {
const isWildcard = i.d === -1 && parseNames(src.slice(i.ss + 6, i.s), names)
const resolution = {
isImport: true,
isAddon: i.n === 'node-gyp-build',
position: [i.ss, i.s - q, i.e + q],
input: i.n,
output: null
Expand All @@ -56,7 +56,6 @@ exports.parse = function parse (src, type = 'module', strictMode = false) {
} else if (i.ss !== i.s) {
result.resolutions.push({
isImport: true,
isAddon: false,
position: [i.ss, i.s - 1, -1],
input: null,
output: null
Expand All @@ -69,7 +68,7 @@ exports.parse = function parse (src, type = 'module', strictMode = false) {
return result
}

srp(src, result.resolutions)
srp(src, result)
return result
}

Expand Down
25 changes: 13 additions & 12 deletions sloppy-require-parser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const CALL_WITH_STRING = /^\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*\)/
const IS_ADDON = /^\s*\.addon\s*\(/
const IS_ADDON = /^\s*\.addon\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)?\s*\)/

module.exports = parseCJS

function parseCJS (src, resolutions = []) {
function parseCJS (src, result) {
const seen = []
const seenAddons = []

let addons = false
let i = src.indexOf('require')
Expand All @@ -27,22 +28,22 @@ function parseCJS (src, resolutions = []) {
const req = m[1].slice(1, -1)
if (seen.indexOf(req) === -1) {
seen.push(req)
resolutions.push(add(req, false))
result.resolutions.push({ isImport: false, position: null, input: req, output: null })
}
} else {
const m = suffix.match(IS_ADDON)
if (m) {
const req = m[1] ? m[1].slice(1, -1) : '.'
if (seenAddons.indexOf(req) === -1) {
seenAddons.push(req)
result.addons.push({ input: req, output: null })
}
}
} else if (addons === false && IS_ADDON.test(suffix)) {
addons = true
resolutions.push(add(null, true))
}
}

i = src.indexOf('require', i + 7)
}

return resolutions
}

function add (input, isAddon) {
return { isImport: false, isAddon, position: null, input, output: null }
}

function newWord (src, i) {
Expand Down
12 changes: 2 additions & 10 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,13 @@ test('script that falls back', function (t) {
})

test('detects addons', function (t) {
{
const res = parse(`
import gyp from 'node-gyp-build'
import some from 'else'
`)

t.alike(res.resolutions.map(r => r.isAddon), [true, false])
}

{
const res = parse(`
const some = require('something')
const addon = require.addon()'
const addon2 = require.addon('./here')
`, 'script')

t.alike(res.resolutions.map(r => r.isAddon), [false, true])
t.alike(res.addons.map(a => a.input), ['.', './here'])
}
})

0 comments on commit c1754f9

Please sign in to comment.