Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit a7fc764

Browse files
authored
chore: update eslint rules (#363)
This updates @netlify/eslint-config-node to v7 and fixes all issues. moved the unit tests to `test/unit` Also removes some optional chaining and nullish coalescing operator as we test against node 8. This is in preparation for an upcoming fix and the introduction of tests for typescript types.
1 parent 27ffd46 commit a7fc764

File tree

12 files changed

+1912
-1365
lines changed

12 files changed

+1912
-1365
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
overrides: [
99
...overrides,
1010
{
11-
files: 'test/*.js',
11+
files: 'test/**/*.+(t|j)s',
1212
rules: {
1313
'no-magic-numbers': 'off',
1414
'promise/prefer-await-to-callbacks': 'off',

package-lock.json

Lines changed: 1855 additions & 1319 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"prepack": "npm run build",
1515
"prepublishOnly": "npm ci && npm test",
1616
"test": "run-s format test:dev",
17-
"format": "run-s format:check-fix:*",
18-
"format:ci": "run-s format:check:*",
17+
"format": "run-s build format:check-fix:*",
18+
"format:ci": "run-s build format:check:*",
1919
"format:check-fix:lint": "run-e format:check:lint format:fix:lint",
2020
"format:check:lint": "cross-env-shell eslint $npm_package_config_eslint",
2121
"format:fix:lint": "cross-env-shell eslint --fix $npm_package_config_eslint",
@@ -28,12 +28,12 @@
2828
"test:ci:ava": "nyc -r lcovonly -r text -r json ava"
2929
},
3030
"config": {
31-
"eslint": "--ignore-pattern README.md --ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{src,scripts,.github}/**/*.{ts,js,md,html}\" \"*.{ts,js,md,html}\" \".*.{ts,js,md,html}\"",
31+
"eslint": "--ignore-pattern README.md --ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{src,scripts,.github,test}/**/*.{ts,js,md,html}\" \"*.{ts,js,md,html}\" \".*.{ts,js,md,html}\"",
3232
"prettier": "--ignore-path .gitignore --loglevel=warn \"{src,scripts,.github}/**/*.{ts,js,md,yml,json,html}\" \"*.{ts,js,yml,json,html}\" \".*.{ts,js,yml,json,html}\" \"!**/package-lock.json\" \"!package-lock.json\""
3333
},
3434
"ava": {
3535
"files": [
36-
"test/*.js"
36+
"test/unit/*.js"
3737
],
3838
"verbose": true
3939
},
@@ -53,7 +53,7 @@
5353
"devDependencies": {
5454
"@commitlint/cli": "^13.0.0",
5555
"@commitlint/config-conventional": "^13.0.0",
56-
"@netlify/eslint-config-node": "^5.1.8",
56+
"@netlify/eslint-config-node": "^7.0.0",
5757
"ava": "^2.4.0",
5858
"husky": "^7.0.4",
5959
"npm-run-all": "^4.1.5",

src/function/handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Context } from './context'
2-
import type { Event } from './event'
3-
import type { Response, BuilderResponse } from './response'
1+
import type { Context } from './context.js'
2+
import type { Event } from './event.js'
3+
import type { Response, BuilderResponse } from './response.js'
44

55
export interface HandlerCallback<ResponseType extends Response = Response> {
66
// eslint-disable-next-line @typescript-eslint/no-explicit-any

src/function/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export { Context as HandlerContext } from './context'
2-
export { Event as HandlerEvent } from './event'
3-
export { Handler, HandlerCallback } from './handler'
4-
export { Response as HandlerResponse } from './response'
1+
export { Context as HandlerContext } from './context.js'
2+
export { Event as HandlerEvent } from './event.js'
3+
export { Handler, HandlerCallback } from './handler.js'
4+
export { Response as HandlerResponse } from './response.js'
55
export {
66
getSecrets,
77
getSecretsForBuild,
@@ -10,5 +10,5 @@ export {
1010
getNetlifyGraphTokenForBuild,
1111
GraphTokenResponse,
1212
HasHeaders,
13-
} from '../lib/graph'
14-
export { NetlifySecrets } from '../lib/secrets_helper'
13+
} from '../lib/graph.js'
14+
export { NetlifySecrets } from '../lib/secrets_helper.js'

src/lib/builder.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import isPromise from 'is-promise'
22

3-
import { HandlerContext, HandlerEvent } from '../function'
4-
import { BuilderHandler, Handler, HandlerCallback } from '../function/handler'
5-
import { Response, BuilderResponse } from '../function/response'
3+
import { BuilderHandler, Handler, HandlerCallback } from '../function/handler.js'
4+
import { HandlerContext, HandlerEvent } from '../function/index.js'
5+
import { Response, BuilderResponse } from '../function/response.js'
66

7-
import { BUILDER_FUNCTIONS_FLAG, HTTP_STATUS_METHOD_NOT_ALLOWED, METADATA_VERSION } from './consts'
7+
import { BUILDER_FUNCTIONS_FLAG, HTTP_STATUS_METHOD_NOT_ALLOWED, METADATA_VERSION } from './consts.js'
88

99
const augmentResponse = (response: BuilderResponse) => {
1010
if (!response) {
1111
return response
1212
}
13-
const metadata = { version: METADATA_VERSION, builder_function: BUILDER_FUNCTIONS_FLAG, ttl: response.ttl ?? 0 }
13+
const metadata = { version: METADATA_VERSION, builder_function: BUILDER_FUNCTIONS_FLAG, ttl: response.ttl || 0 }
1414

1515
return {
1616
...response,
@@ -36,8 +36,9 @@ const wrapHandler =
3636
queryStringParameters: {},
3737
}
3838

39-
// eslint-disable-next-line promise/prefer-await-to-callbacks
40-
const wrappedCallback = (error: unknown, response: BuilderResponse) => callback?.(error, augmentResponse(response))
39+
const wrappedCallback = (error: unknown, response: BuilderResponse) =>
40+
// eslint-disable-next-line promise/prefer-await-to-callbacks
41+
callback ? callback(error, augmentResponse(response)) : null
4142
const execution = handler(modifiedEvent, context, wrappedCallback)
4243

4344
if (isPromise(execution)) {

src/lib/graph.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Context as HandlerContext, Context } from '../function/context'
2-
import { Event as HandlerEvent } from '../function/event'
3-
import { BaseHandler, HandlerCallback } from '../function/handler'
4-
import { Response } from '../function/response'
1+
import { Context as HandlerContext, Context } from '../function/context.js'
2+
import { Event as HandlerEvent } from '../function/event.js'
3+
import { BaseHandler, HandlerCallback } from '../function/handler.js'
4+
import { Response } from '../function/response.js'
55

6-
import { getSecrets, NetlifySecrets } from './secrets_helper'
6+
import { getSecrets, NetlifySecrets } from './secrets_helper.js'
77
// Fine-grained control during the preview, less necessary with a more proactive OneGraph solution
8-
export { getSecrets, getSecretsForBuild } from './secrets_helper'
9-
export { getNetlifyGraphToken, getNetlifyGraphTokenForBuild, GraphTokenResponse, HasHeaders } from './graph_token'
8+
export { getSecrets, getSecretsForBuild } from './secrets_helper.js'
9+
export { getNetlifyGraphToken, getNetlifyGraphTokenForBuild, GraphTokenResponse, HasHeaders } from './graph_token.js'
1010

1111
export interface ContextWithSecrets extends Context {
1212
secrets: NetlifySecrets

src/lib/graph_token.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ const graphTokenFromEnv = function (): GraphTokenResponse {
5050
return { token }
5151
}
5252

53-
const tokenFallback = function (event: HasHeaders): GraphTokenResponse {
53+
const tokenFallback = function (event: HasHeaders & { authlifyToken?: string | null }): GraphTokenResponse {
5454
// Backwards compatibility with older version of cli that doesn't inject header
55-
const token = (event as { authlifyToken?: string | null })?.authlifyToken
56-
if (token) {
57-
return { token }
55+
if (event && event.authlifyToken) {
56+
return { token: event.authlifyToken }
5857
}
5958

6059
// If we're in dev-mode with next.js, the plugin won't be there to inject

src/lib/schedule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Handler } from '../function'
1+
import type { Handler } from '../function/index.js'
22

33
/**
44
* Declares a function to run on a cron schedule.

src/lib/secrets_helper.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { graphRequest } from './graph_request'
2-
import { getNetlifyGraphToken, getNetlifyGraphTokenForBuild, GraphTokenResponseError, HasHeaders } from './graph_token'
1+
import { graphRequest } from './graph_request.js'
2+
import {
3+
getNetlifyGraphToken,
4+
getNetlifyGraphTokenForBuild,
5+
GraphTokenResponseError,
6+
HasHeaders,
7+
} from './graph_token.js'
38

49
const services = {
510
gitHub: null,
@@ -65,12 +70,19 @@ const serviceNormalizeOverrides: ServiceNormalizeOverrides = {
6570
}
6671

6772
const formatSecrets = (result: GraphSecretsResponse | undefined) => {
68-
const responseServices = result?.data?.me?.serviceMetadata?.loggedInServices
69-
70-
if (!responseServices) {
73+
if (
74+
!result ||
75+
!result.data ||
76+
!result.data.me ||
77+
!result.data.me.serviceMetadata ||
78+
!result.data.me.serviceMetadata.loggedInServices
79+
) {
7180
return {}
7281
}
7382

83+
// TODO use optional chaining once we drop node 12 or lower
84+
const responseServices = result.data.me.serviceMetadata.loggedInServices
85+
7486
const newSecrets = responseServices.reduce((acc: NetlifySecrets, service) => {
7587
const normalized = serviceNormalizeOverrides[service.service] || camelize(service.friendlyServiceName)
7688
return { ...acc, [normalized]: service }
@@ -134,7 +146,7 @@ const findLoggedInServicesQuery = `query FindLoggedInServicesQuery {
134146
const getSecretsForToken = async (token: string): Promise<NetlifySecrets> => {
135147
const body = JSON.stringify({ query: findLoggedInServicesQuery })
136148

137-
// eslint-disable-next-line node/no-unsupported-features/node-builtins
149+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
138150
const resultBody = await graphRequest(token, new TextEncoder().encode(body))
139151
const result: GraphSecretsResponse = JSON.parse(resultBody)
140152

src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { builder } from './lib/builder'
2-
export { schedule } from './lib/schedule'
3-
export * from './function'
1+
export { builder } from './lib/builder.js'
2+
export { schedule } from './lib/schedule.js'
3+
export * from './function/index.js'

test/builder.js renamed to test/unit/builder.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const test = require('ava')
22

3-
const { builder } = require('../dist/lib/builder')
4-
5-
const { invokeLambda } = require('./helpers/main')
3+
const { builder } = require('../../dist/lib/builder')
4+
const { invokeLambda } = require('../helpers/main')
65

76
const METADATA_OBJECT = { metadata: { version: 1, builder_function: true, ttl: 0 } }
87

0 commit comments

Comments
 (0)