Skip to content

Commit

Permalink
Adds linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Jan 9, 2024
1 parent 3a3cd49 commit 7ef70e3
Show file tree
Hide file tree
Showing 44 changed files with 170 additions and 139 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
overrides:
- files:
- '**/*.{js,cjs,mjs}'

parser: '@babel/eslint-parser'
parserOptions:
ecmaVersion: latest
requireConfigFile: false
sourceType: 'script'
babelOptions:
plugins:
- '@babel/plugin-syntax-import-assertions'

rules:
"import/first": off

extends:
- "standard"
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
- run: npm test
- name: Rename coverage file
run: >
Expand Down
31 changes: 15 additions & 16 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,7 +56,7 @@ function deleteIitm (url) {
return resultUrl
}

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

Expand All @@ -68,11 +68,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 (isNodeMajor16AndMinor17OrGreater()) {
return !isFileProtocol(urlObj) && !isNodeProtocol(urlObj)
}
return !isFileProtocol(urlObj) && NODE_MAJOR < 18
Expand All @@ -87,7 +87,7 @@ function needsToAddFileProtocol(urlObj) {
* @param {string} line
* @returns {boolean}
*/
function isStarExportLine(line) {
function isStarExportLine (line) {
return /^\* from /.test(line)
}

Expand Down Expand Up @@ -124,7 +124,7 @@ function isStarExportLine(line) {
*
* @returns {Promise<ProcessedModule>}
*/
async function processModule({
async function processModule ({
srcUrl,
context,
parentGetSource,
Expand Down Expand Up @@ -152,7 +152,7 @@ async function processModule({

for (const n of exportNames) {
if (isStarExportLine(n) === true) {
const [_, modFile] = n.split('* from ')
const [, modFile] = n.split('* from ')
const normalizedModName = normalizeModName(modFile)
const modUrl = new URL(modFile, srcUrl).toString()
const modName = Buffer.from(modFile, 'hex') + Date.now() + randomBytes(4).toString('hex')
Expand Down Expand Up @@ -191,7 +191,7 @@ async function processModule({
continue
}

setters.set(`$${n}`+ns, `
setters.set(`$${n}` + ns, `
let $${n} = ${ns}.${n}
export { $${n} as ${n} }
set.${n} = (v) => {
Expand All @@ -214,9 +214,9 @@ async function processModule({
*
* @returns {string} The normalized identifier.
*/
function normalizeModName(name) {
function normalizeModName (name) {
return name
.split('\/')
.split('/')
.pop()
.replace(/(.+)\.(?:js|mjs)$/, '$1')
.replaceAll(/(-.)/g, x => x[1].toUpperCase())
Expand Down Expand Up @@ -253,7 +253,6 @@ function createHook (meta) {
return url
}


specifiers.set(url.url, specifier)

return {
Expand All @@ -268,9 +267,9 @@ function createHook (meta) {
if (hasIitm(url)) {
const realUrl = deleteIitm(url)
const { imports, namespaces, setters: mapSetters } = await processModule({
srcUrl: realUrl,
context,
parentGetSource
srcUrl: realUrl,
context,
parentGetSource
})
const setters = Array.from(mapSetters.values())

Expand All @@ -284,7 +283,7 @@ function createHook (meta) {
const renamedDefaults = setters
.map(s => {
const matches = /let \$(.+) = (\$.+)\.default/.exec(s)
if (matches === null) return
if (matches === null) return undefined
return `_['${matches[1]}'] = ${matches[2]}.default`
})
.filter(s => s)
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
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"test": "c8 --reporter lcov --check-coverage --lines 50 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'"
"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'",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
"repository": {
"type": "git",
Expand All @@ -27,8 +29,16 @@
},
"homepage": "https://github.com/DataDog/import-in-the-middle#readme",
"devDependencies": {
"@babel/eslint-parser": "^7.23.3",
"@babel/plugin-syntax-import-assertions": "^7.23.3",
"@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-node": "^11.1.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/a.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const a = 'a'

export function aFunc() {
export function aFunc () {
return a
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/b.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const b = 'b'

export function bFunc() {
export function bFunc () {
return b
}
8 changes: 4 additions & 4 deletions test/fixtures/cyclical-a.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { testB } from './cyclical-b.mjs';
import { testB } from './cyclical-b.mjs'

export function testA() {
console.log("testA");
export function testA () {
console.log('testA')
}

testB();
testB()
10 changes: 5 additions & 5 deletions test/fixtures/cyclical-b.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testA } from './cyclical-a.mjs';
import { testA } from './cyclical-a.mjs'

export function testB() {
console.log("testB");
testA();
}
export function testB () {
console.log('testB')
testA()
}
10 changes: 5 additions & 5 deletions test/fixtures/env.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let env = {FOO: 'baz'};
let env = { FOO: 'baz' }

function setEnv(newEnv) {
console.log('setting env, env.FOO is', newEnv.FOO);
env = newEnv;
function setEnv (newEnv) {
console.log('setting env, env.FOO is', newEnv.FOO)
env = newEnv
}

export { setEnv, env };
export { setEnv, env }
26 changes: 13 additions & 13 deletions test/fixtures/export-types/declarations.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const o = { name5: 1, name6: 1 };
const o = { name5: 1, name6: 1 }
const array = [1, 1]

// Exporting declarations
export let name1 = 1, name2 = 1/*, … */; // also var
export const name3 = 1, name4 = 1/*, … */; // also var, let
export function functionName() { return 1 }
export class ClassName { getFoo() { return 1 } }
export function* generatorFunctionName() { return 1 }
export const { name5, name6: bar } = o;
export const [ name7, name8 ] = array;
export async function asyncFunctionName() { return 1 }
export async function* asyncGeneratorFunctionName() { yield 1 }
export const name1 = 1; export const name2 = 1/*, … */ // also var
export const name3 = 1; export const name4 = 1/*, … */ // also var, let
export function functionName () { return 1 }
export class ClassName { getFoo () { return 1 } }
export function * generatorFunctionName () { return 1 }
export const { name5, name6: bar } = o
export const [name7, name8] = array
export async function asyncFunctionName () { return 1 }
export async function * asyncGeneratorFunctionName () { yield 1 }
export const arrowFunction = () => {
return 1;
return 1
}
export const asyncArrowFunction = async () => {
return 1;
}
return 1
}
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-class-anon.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default class { getFoo() { return 1 } }
export default class { getFoo () { return 1 } }
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-class.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default class ClassName { getFoo() { return 1 } }
export default class ClassName { getFoo () { return 1 } }
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-expression-array.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default [1]
export default [1]
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-expression-num.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 1
export default 1
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-expression-string.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 'dog'
export default 'dog'
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-function-anon.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function () { return 1 }
export default function () { return 1 }
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-function.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function functionName() { return 1 }
export default function functionName () { return 1 }
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-generator-anon.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function* () { return 1 }
export default function * () { return 1 }
2 changes: 1 addition & 1 deletion test/fixtures/export-types/default-generator.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function* generatorFunctionName() { return 1 }
export default function * generatorFunctionName () { return 1 }
2 changes: 1 addition & 1 deletion test/fixtures/export-types/fn-default-export.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function () {
return 1
}
}
4 changes: 2 additions & 2 deletions test/fixtures/export-types/import-default-export.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import fnDefaultExport from "./fn-default-export.mjs";
export default fnDefaultExport
import fnDefaultExport from './fn-default-export.mjs'
export default fnDefaultExport
Loading

0 comments on commit 7ef70e3

Please sign in to comment.