Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add linting #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true
},
extends: [
'eslint:recommended',
'standard'
],
overrides: [
{
env: {
node: true
},
files: [
'.eslintrc.{js,cjs}'
],
parserOptions: {
sourceType: 'script'
}
}
],
parserOptions: {
ecmaVersion: 'latest'
},
rules: {
'no-unused-vars': ['error', { varsIgnorePattern: '^_$' }]
}
}
23 changes: 9 additions & 14 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const { randomBytes } = require('crypto')
const specifiers = new Map()
const isWin = process.platform === "win32"
const isWin = process.platform === 'win32'

// FIXME: Typescript extensions are added temporarily until we find a better
// way of supporting arbitrary extensions
Expand All @@ -16,7 +16,7 @@ const NODE_MINOR = Number(NODE_VERSION[1])
let entrypoint

let getExports
if (NODE_MAJOR >= 20 || (NODE_MAJOR == 18 && NODE_MINOR >= 19)) {
if (NODE_MAJOR >= 20 || (NODE_MAJOR === 18 && NODE_MINOR >= 19)) {
getExports = require('./lib/get-exports.js')
} else {
getExports = (url) => import(url).then(Object.keys)
Expand Down Expand Up @@ -56,10 +56,6 @@ function deleteIitm (url) {
return resultUrl
}

function isNode16AndBiggerOrEqualsThan16_17_0() {
return NODE_MAJOR === 16 && NODE_MINOR >= 17
}

function isFileProtocol (urlObj) {
return urlObj.protocol === 'file:'
}
Expand All @@ -68,11 +64,11 @@ function isNodeProtocol (urlObj) {
return urlObj.protocol === 'node:'
}

function needsToAddFileProtocol(urlObj) {
function needsToAddFileProtocol (urlObj) {
if (NODE_MAJOR === 17) {
return !isFileProtocol(urlObj)
}
if (isNode16AndBiggerOrEqualsThan16_17_0()) {
if (NODE_MAJOR === 16 && NODE_MINOR >= 17) {
return !isFileProtocol(urlObj) && !isNodeProtocol(urlObj)
}
return !isFileProtocol(urlObj) && NODE_MAJOR < 18
Expand All @@ -87,7 +83,7 @@ function needsToAddFileProtocol(urlObj) {
* @param {string} line
* @returns {boolean}
*/
function isStarExportLine(line) {
function isStarExportLine (line) {
return /^\* from /.test(line)
}

Expand Down Expand Up @@ -115,7 +111,7 @@ function isStarExportLine(line) {
* module.
* @returns {Promise<ProcessedModule>}
*/
async function processModule({ srcUrl, context, parentGetSource }) {
async function processModule ({ srcUrl, context, parentGetSource }) {
const exportNames = await getExports(srcUrl, context, parentGetSource)
const imports = [`import * as namespace from ${JSON.stringify(srcUrl)}`]
const namespaces = ['namespace']
Expand Down Expand Up @@ -180,7 +176,6 @@ function createHook (meta) {
return url
}


specifiers.set(url.url, specifier)

return {
Expand All @@ -195,9 +190,9 @@ function createHook (meta) {
if (hasIitm(url)) {
const realUrl = deleteIitm(url)
const { imports, namespaces, setters } = await processModule({
srcUrl: realUrl,
context,
parentGetSource
srcUrl: realUrl,
context,
parentGetSource
})

return {
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ const {
toHook
} = require('./lib/register')

function addHook(hook) {
function addHook (hook) {
importHooks.push(hook)
toHook.forEach(([name, namespace]) => hook(name, namespace))
}

function removeHook(hook) {
function removeHook (hook) {
const index = importHooks.indexOf(hook)
if (index > -1) {
importHooks.splice(index, 1)
}
}

function callHookFn(hookFn, namespace, name, baseDir) {
function callHookFn (hookFn, namespace, name, baseDir) {
const newDefault = hookFn(namespace, name, baseDir)
if (newDefault && newDefault !== namespace) {
namespace.default = newDefault
}
}

function Hook(modules, options, hookFn) {
function Hook (modules, options, hookFn) {
if ((this instanceof Hook) === false) return new Hook(modules, options, hookFn)
if (typeof modules === 'function') {
hookFn = modules
Expand Down
2 changes: 1 addition & 1 deletion lib/get-esm-exports.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { Parser } = require('acorn')
const { importAssertions } = require('acorn-import-assertions');
const { importAssertions } = require('acorn-import-assertions')

const acornOpts = {
ecmaVersion: 'latest',
Expand Down
2 changes: 1 addition & 1 deletion lib/get-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { parse: getCjsExports } = require('cjs-module-lexer')
const fs = require('fs')
const { fileURLToPath } = require('url')

function addDefault(arr) {
function addDefault (arr) {
return Array.from(new Set(['default', ...arr]))
}

Expand Down
7 changes: 3 additions & 4 deletions lib/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.


const importHooks = [] // TODO should this be a Set?
const setters = new WeakMap()
const specifiers = new Map()
const toHook = []

const proxyHandler = {
set(target, name, value) {
set (target, name, value) {
return setters.get(target)[name](value)
},

defineProperty(target, property, descriptor) {
defineProperty (target, property, descriptor) {
if ((!('value' in descriptor))) {
throw new Error('Getters/setters are not supported for exports property descriptors.')
}
Expand All @@ -22,7 +21,7 @@ const proxyHandler = {
}
}

function register(name, namespace, set, specifier) {
function register (name, namespace, set, specifier) {
specifiers.set(name, specifier)
setters.set(namespace, set)
const proxy = new Proxy(namespace, proxyHandler)
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"description": "Intercept imports in Node.js",
"main": "index.js",
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "c8 --reporter lcov --check-coverage --lines 70 imhotap --runner 'node test/runtest' --files test/{hook,low-level,other,get-esm-exports}/*",
"test:ts": "c8 --reporter lcov imhotap --runner 'node test/runtest' --files test/typescript/*.test.mts",
"coverage": "c8 --reporter html imhotap --runner 'node test/runtest' --files test/{hook,low-level,other,get-esm-exports}/* && echo '\nNow open coverage/index.html\n'"
Expand All @@ -29,6 +31,11 @@
"devDependencies": {
"@types/node": "^18.0.6",
"c8": "^7.8.0",
"eslint": "^8.55.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-n": "^16.4.0",
"eslint-plugin-promise": "^6.1.1",
"imhotap": "^2.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/something.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

'use strict'

module.exports = function bar() {
module.exports = function bar () {
return 42
}
module.exports.foo = 42
4 changes: 2 additions & 2 deletions test/hook/dynamic-import-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const { strictEqual } = require('assert')
Hook((exports, name) => {
if (name.match(/something.js/)) {
const orig = exports.default
exports.default = function bar() {
exports.default = function bar () {
return orig() + 15
}
}
if (name.match(/something.mjs/)) {
const orig = exports.default
return function bar() {
return function bar () {
return orig() + 15
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/low-level/dynamic-import-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { strictEqual } = require('assert')
addHook((name, exports) => {
if (name.match(/something\.m?js/)) {
const orig = exports.default
exports.default = function bar() {
exports.default = function bar () {
return orig() + 15
}
}
Expand Down
Loading