Skip to content

Commit 6f4aa45

Browse files
authored
Adds linting (#51)
This'll work until eslint removes formatting support ๐Ÿ˜ฎโ€๐Ÿ’จ
1 parent 3a3cd49 commit 6f4aa45

44 files changed

Lines changed: 182 additions & 139 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

โ€Ž.eslintrc.yamlโ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
overrides:
2+
- files:
3+
- '**/*.{js,cjs,mjs}'
4+
5+
parser: '@babel/eslint-parser'
6+
parserOptions:
7+
ecmaVersion: latest
8+
requireConfigFile: false
9+
sourceType: 'script'
10+
babelOptions:
11+
plugins:
12+
- '@babel/plugin-syntax-import-assertions'
13+
14+
rules:
15+
"import/first": off
16+
17+
extends:
18+
- "standard"

โ€Ž.github/workflows/ci.ymlโ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ on:
55
- cron: '0 4 * * *'
66

77
jobs:
8+
lint:
9+
# The linting packages require modern Node.js versions in order to run.
10+
# Therefore, we run linting separately and only once.
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version: 'lts/*'
17+
- run: npm install
18+
- run: npm run lint
19+
820
build:
921
runs-on: ubuntu-latest
1022

โ€Žhook.jsโ€Ž

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const { randomBytes } = require('crypto')
66
const specifiers = new Map()
7-
const isWin = process.platform === "win32"
7+
const isWin = process.platform === 'win32'
88

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

1818
let getExports
19-
if (NODE_MAJOR >= 20 || (NODE_MAJOR == 18 && NODE_MINOR >= 19)) {
19+
if (NODE_MAJOR >= 20 || (NODE_MAJOR === 18 && NODE_MINOR >= 19)) {
2020
getExports = require('./lib/get-exports.js')
2121
} else {
2222
getExports = ({ url }) => import(url).then(Object.keys)
@@ -56,7 +56,7 @@ function deleteIitm (url) {
5656
return resultUrl
5757
}
5858

59-
function isNode16AndBiggerOrEqualsThan16_17_0() {
59+
function isNodeMajor16AndMinor17OrGreater () {
6060
return NODE_MAJOR === 16 && NODE_MINOR >= 17
6161
}
6262

@@ -68,11 +68,11 @@ function isNodeProtocol (urlObj) {
6868
return urlObj.protocol === 'node:'
6969
}
7070

71-
function needsToAddFileProtocol(urlObj) {
71+
function needsToAddFileProtocol (urlObj) {
7272
if (NODE_MAJOR === 17) {
7373
return !isFileProtocol(urlObj)
7474
}
75-
if (isNode16AndBiggerOrEqualsThan16_17_0()) {
75+
if (isNodeMajor16AndMinor17OrGreater()) {
7676
return !isFileProtocol(urlObj) && !isNodeProtocol(urlObj)
7777
}
7878
return !isFileProtocol(urlObj) && NODE_MAJOR < 18
@@ -87,7 +87,7 @@ function needsToAddFileProtocol(urlObj) {
8787
* @param {string} line
8888
* @returns {boolean}
8989
*/
90-
function isStarExportLine(line) {
90+
function isStarExportLine (line) {
9191
return /^\* from /.test(line)
9292
}
9393

@@ -124,7 +124,7 @@ function isStarExportLine(line) {
124124
*
125125
* @returns {Promise<ProcessedModule>}
126126
*/
127-
async function processModule({
127+
async function processModule ({
128128
srcUrl,
129129
context,
130130
parentGetSource,
@@ -152,7 +152,7 @@ async function processModule({
152152

153153
for (const n of exportNames) {
154154
if (isStarExportLine(n) === true) {
155-
const [_, modFile] = n.split('* from ')
155+
const [, modFile] = n.split('* from ')
156156
const normalizedModName = normalizeModName(modFile)
157157
const modUrl = new URL(modFile, srcUrl).toString()
158158
const modName = Buffer.from(modFile, 'hex') + Date.now() + randomBytes(4).toString('hex')
@@ -191,7 +191,7 @@ async function processModule({
191191
continue
192192
}
193193

194-
setters.set(`$${n}`+ns, `
194+
setters.set(`$${n}` + ns, `
195195
let $${n} = ${ns}.${n}
196196
export { $${n} as ${n} }
197197
set.${n} = (v) => {
@@ -214,9 +214,9 @@ async function processModule({
214214
*
215215
* @returns {string} The normalized identifier.
216216
*/
217-
function normalizeModName(name) {
217+
function normalizeModName (name) {
218218
return name
219-
.split('\/')
219+
.split('/')
220220
.pop()
221221
.replace(/(.+)\.(?:js|mjs)$/, '$1')
222222
.replaceAll(/(-.)/g, x => x[1].toUpperCase())
@@ -253,7 +253,6 @@ function createHook (meta) {
253253
return url
254254
}
255255

256-
257256
specifiers.set(url.url, specifier)
258257

259258
return {
@@ -268,9 +267,9 @@ function createHook (meta) {
268267
if (hasIitm(url)) {
269268
const realUrl = deleteIitm(url)
270269
const { imports, namespaces, setters: mapSetters } = await processModule({
271-
srcUrl: realUrl,
272-
context,
273-
parentGetSource
270+
srcUrl: realUrl,
271+
context,
272+
parentGetSource
274273
})
275274
const setters = Array.from(mapSetters.values())
276275

@@ -284,7 +283,7 @@ function createHook (meta) {
284283
const renamedDefaults = setters
285284
.map(s => {
286285
const matches = /let \$(.+) = (\$.+)\.default/.exec(s)
287-
if (matches === null) return
286+
if (matches === null) return undefined
288287
return `_['${matches[1]}'] = ${matches[2]}.default`
289288
})
290289
.filter(s => s)

โ€Žindex.jsโ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ const {
1212
toHook
1313
} = require('./lib/register')
1414

15-
function addHook(hook) {
15+
function addHook (hook) {
1616
importHooks.push(hook)
1717
toHook.forEach(([name, namespace]) => hook(name, namespace))
1818
}
1919

20-
function removeHook(hook) {
20+
function removeHook (hook) {
2121
const index = importHooks.indexOf(hook)
2222
if (index > -1) {
2323
importHooks.splice(index, 1)
2424
}
2525
}
2626

27-
function callHookFn(hookFn, namespace, name, baseDir) {
27+
function callHookFn (hookFn, namespace, name, baseDir) {
2828
const newDefault = hookFn(namespace, name, baseDir)
2929
if (newDefault && newDefault !== namespace) {
3030
namespace.default = newDefault
3131
}
3232
}
3333

34-
function Hook(modules, options, hookFn) {
34+
function Hook (modules, options, hookFn) {
3535
if ((this instanceof Hook) === false) return new Hook(modules, options, hookFn)
3636
if (typeof modules === 'function') {
3737
hookFn = modules

โ€Žlib/get-esm-exports.jsโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { Parser } = require('acorn')
4-
const { importAssertions } = require('acorn-import-assertions');
4+
const { importAssertions } = require('acorn-import-assertions')
55

66
const acornOpts = {
77
ecmaVersion: 'latest',

โ€Žlib/get-exports.jsโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { parse: getCjsExports } = require('cjs-module-lexer')
55
const fs = require('fs')
66
const { fileURLToPath } = require('url')
77

8-
function addDefault(arr) {
8+
function addDefault (arr) {
99
return Array.from(new Set(['default', ...arr]))
1010
}
1111

โ€Žlib/register.jsโ€Ž

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
//
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
44

5-
65
const importHooks = [] // TODO should this be a Set?
76
const setters = new WeakMap()
87
const specifiers = new Map()
98
const toHook = []
109

1110
const proxyHandler = {
12-
set(target, name, value) {
11+
set (target, name, value) {
1312
return setters.get(target)[name](value)
1413
},
1514

16-
defineProperty(target, property, descriptor) {
15+
defineProperty (target, property, descriptor) {
1716
if ((!('value' in descriptor))) {
1817
throw new Error('Getters/setters are not supported for exports property descriptors.')
1918
}
@@ -22,7 +21,7 @@ const proxyHandler = {
2221
}
2322
}
2423

25-
function register(name, namespace, set, specifier) {
24+
function register (name, namespace, set, specifier) {
2625
specifiers.set(name, specifier)
2726
setters.set(namespace, set)
2827
const proxy = new Proxy(namespace, proxyHandler)

โ€Žpackage.jsonโ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"scripts": {
77
"test": "c8 --reporter lcov --check-coverage --lines 50 imhotap --runner 'node test/runtest' --files test/{hook,low-level,other,get-esm-exports}/*",
88
"test:ts": "c8 --reporter lcov imhotap --runner 'node test/runtest' --files test/typescript/*.test.mts",
9-
"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'"
9+
"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'",
10+
"lint": "eslint .",
11+
"lint:fix": "eslint . --fix"
1012
},
1113
"repository": {
1214
"type": "git",
@@ -27,8 +29,17 @@
2729
},
2830
"homepage": "https://github.com/DataDog/import-in-the-middle#readme",
2931
"devDependencies": {
32+
"@babel/core": "^7.23.7",
33+
"@babel/eslint-parser": "^7.23.3",
34+
"@babel/plugin-syntax-import-assertions": "^7.23.3",
3035
"@types/node": "^18.0.6",
3136
"c8": "^7.8.0",
37+
"eslint": "^8.55.0",
38+
"eslint-config-standard": "^17.1.0",
39+
"eslint-plugin-import": "^2.29.0",
40+
"eslint-plugin-n": "^16.4.0",
41+
"eslint-plugin-node": "^11.1.0",
42+
"eslint-plugin-promise": "^6.1.1",
3243
"imhotap": "^2.1.0",
3344
"ts-node": "^10.9.1",
3445
"typescript": "^4.7.4"

โ€Žtest/fixtures/a.mjsโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const a = 'a'
22

3-
export function aFunc() {
3+
export function aFunc () {
44
return a
55
}
66

โ€Žtest/fixtures/b.mjsโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const b = 'b'
22

3-
export function bFunc() {
3+
export function bFunc () {
44
return b
55
}

0 commit comments

Comments
ย (0)