From 4fdc4b63061d131ac8178234d211d6565ccea6ad Mon Sep 17 00:00:00 2001 From: Bryan English Date: Wed, 13 Dec 2023 16:38:32 -0500 Subject: [PATCH] add linting --- .eslintrc.js | 30 ++++++++++++++++++++++++ hook.js | 23 +++++++----------- index.js | 8 +++---- lib/get-esm-exports.js | 2 +- lib/get-exports.js | 2 +- lib/register.js | 7 +++--- package.json | 7 ++++++ test/fixtures/something.js | 2 +- test/hook/dynamic-import-default.js | 4 ++-- test/low-level/dynamic-import-default.js | 2 +- 10 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 .eslintrc.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..e51a071 --- /dev/null +++ b/.eslintrc.js @@ -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: '^_$' }] + } +} diff --git a/hook.js b/hook.js index d2ce686..7bbc29d 100644 --- a/hook.js +++ b/hook.js @@ -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 @@ -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) @@ -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:' } @@ -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 @@ -87,7 +83,7 @@ function needsToAddFileProtocol(urlObj) { * @param {string} line * @returns {boolean} */ -function isStarExportLine(line) { +function isStarExportLine (line) { return /^\* from /.test(line) } @@ -115,7 +111,7 @@ function isStarExportLine(line) { * module. * @returns {Promise} */ -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'] @@ -180,7 +176,6 @@ function createHook (meta) { return url } - specifiers.set(url.url, specifier) return { @@ -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 { diff --git a/index.js b/index.js index 8796665..50a8665 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/lib/get-esm-exports.js b/lib/get-esm-exports.js index c04799e..d2092fb 100644 --- a/lib/get-esm-exports.js +++ b/lib/get-esm-exports.js @@ -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', diff --git a/lib/get-exports.js b/lib/get-exports.js index cfa86d4..b90494b 100644 --- a/lib/get-exports.js +++ b/lib/get-exports.js @@ -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])) } diff --git a/lib/register.js b/lib/register.js index 42abf7e..a81c48c 100644 --- a/lib/register.js +++ b/lib/register.js @@ -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.') } @@ -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) diff --git a/package.json b/package.json index 1238315..d266ccc 100644 --- a/package.json +++ b/package.json @@ -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'" @@ -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" diff --git a/test/fixtures/something.js b/test/fixtures/something.js index 8cd50e5..8cd429e 100644 --- a/test/fixtures/something.js +++ b/test/fixtures/something.js @@ -4,7 +4,7 @@ 'use strict' -module.exports = function bar() { +module.exports = function bar () { return 42 } module.exports.foo = 42 diff --git a/test/hook/dynamic-import-default.js b/test/hook/dynamic-import-default.js index ab87729..fdb4753 100644 --- a/test/hook/dynamic-import-default.js +++ b/test/hook/dynamic-import-default.js @@ -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 } } diff --git a/test/low-level/dynamic-import-default.js b/test/low-level/dynamic-import-default.js index 945ef4b..7014186 100644 --- a/test/low-level/dynamic-import-default.js +++ b/test/low-level/dynamic-import-default.js @@ -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 } }