Skip to content

Commit 5b9d6a3

Browse files
authored
chore: fix linting errors (#116)
* chore: fix linting errors * chore: fix linting errors * chore: add general eslint override for test files * chore: switch fs promises to fs-extra so tests lint correctly
1 parent aa21da6 commit 5b9d6a3

File tree

8 files changed

+1798
-1812
lines changed

8 files changed

+1798
-1812
lines changed

.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ module.exports = {
2424
env: {
2525
jest: true,
2626
},
27+
overrides: [
28+
{
29+
// Tests use lots of nested callbacks
30+
files: ['**/__tests__/*.ts'],
31+
rules: {
32+
'max-nested-callbacks': 'off',
33+
'import/first': 'off',
34+
},
35+
},
36+
],
2737
}

.prettierrc.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
const rules = require('@netlify/eslint-config-node/.prettierrc.json')
2+
13
module.exports = {
2-
...require("@netlify/eslint-config-node/.prettierrc.json"),
3-
endOfLine: "auto",
4+
...rules,
5+
endOfLine: 'auto',
46
}

src/__tests__/gatsby-node.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
jest.mock('../plugin-data', () => {
2-
return {
3-
__esModule: true,
4-
default: jest.fn().mockReturnValue({
5-
publicFolder: jest.fn().mockReturnValue('mock-file-path'),
6-
}),
7-
}
8-
})
9-
jest.mock('../build-headers-program', () => {
10-
return {
11-
__esModule: true,
12-
default: jest.fn(),
13-
}
14-
})
15-
jest.mock('fs-extra', () => {
16-
return {
17-
__esModule: true,
18-
default: jest.fn(),
19-
existsSync: jest.fn(),
20-
readFile: jest.fn(),
21-
writeFile: jest.fn(),
22-
}
23-
})
1+
jest.mock('../plugin-data', () => ({
2+
__esModule: true,
3+
default: jest.fn().mockReturnValue({
4+
publicFolder: jest.fn().mockReturnValue('mock-file-path'),
5+
}),
6+
}))
7+
jest.mock('../build-headers-program', () => ({
8+
__esModule: true,
9+
default: jest.fn(),
10+
}))
11+
jest.mock('fs-extra', () => ({
12+
__esModule: true,
13+
default: jest.fn(),
14+
existsSync: jest.fn(),
15+
readFile: jest.fn(),
16+
writeFile: jest.fn(),
17+
}))
2418

2519
// Importing writeFile here gives us access to the mocked method to assert the correct content is written to the file within test cases
2620
import { writeFile } from 'fs-extra'
2721
import { testPluginOptionsSchema } from 'gatsby-plugin-utils'
22+
2823
import { pluginOptionsSchema, onPostBuild } from '../gatsby-node'
2924

3025
describe(`gatsby-node.js`, () => {
@@ -46,7 +41,7 @@ describe(`gatsby-node.js`, () => {
4641
mergeSecurityHeaders: `this should be a boolean`,
4742
mergeLinkHeaders: `this should be a boolean`,
4843
mergeCachingHeaders: `this should be a boolean`,
49-
transformHeaders: (too, many, args) => ``,
44+
transformHeaders: (too, many, args) => [too, many, args],
5045
generateMatchPathRewrites: `this should be a boolean`,
5146
})
5247

src/constants.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _ from "lodash"
1+
import _ from 'lodash'
22

33
// Gatsby values
44
export const BUILD_HTML_STAGE = `build-html`
@@ -7,17 +7,19 @@ export const BUILD_CSS_STAGE = `build-css`
77
// Plugin values
88
export const NETLIFY_HEADERS_FILENAME = `_headers`
99

10+
// Default options including transform to manipulate headers for
11+
// sorting and rewrites for client only paths
1012
export const DEFAULT_OPTIONS = {
1113
headers: {},
1214
mergeSecurityHeaders: true,
1315
mergeLinkHeaders: true,
1416
mergeCachingHeaders: true,
15-
transformHeaders: _.identity, // optional transform for manipulating headers for sorting, etc
16-
generateMatchPathRewrites: true, // generate rewrites for client only paths
17+
transformHeaders: _.identity,
18+
generateMatchPathRewrites: true,
1719
}
1820

1921
export const SECURITY_HEADERS = {
20-
"/*": [
22+
'/*': [
2123
`X-Frame-Options: DENY`,
2224
`X-XSS-Protection: 1; mode=block`,
2325
`X-Content-Type-Options: nosniff`,
@@ -28,8 +30,8 @@ export const SECURITY_HEADERS = {
2830
export const IMMUTABLE_CACHING_HEADER = `Cache-Control: public, max-age=31536000, immutable`
2931

3032
export const CACHING_HEADERS = {
31-
"/static/*": [IMMUTABLE_CACHING_HEADER],
32-
"/sw.js": [`Cache-Control: no-cache`],
33+
'/static/*': [IMMUTABLE_CACHING_HEADER],
34+
'/sw.js': [`Cache-Control: no-cache`],
3335
}
3436

3537
export const LINK_REGEX = /^(Link: <\/)(.+)(>;.+)/

src/create-redirects.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ export default async function writeRedirectsFile(pluginData: any, redirects: any
3838
const value = rest[key]
3939

4040
if (typeof value === `string` && value.includes(` `)) {
41-
console.warn(
42-
`Invalid redirect value "${value}" specified for key "${key}". ` + `Values should not contain spaces.`,
43-
)
41+
console.warn(`Invalid redirect value "${value}" specified for key "${key}". Values should not contain spaces.`)
4442
} else if (NETLIFY_REDIRECT_KEYWORDS_ALLOWLIST.has(key)) {
4543
pieces.push(`${key}=${value}`)
4644
}
@@ -49,10 +47,7 @@ export default async function writeRedirectsFile(pluginData: any, redirects: any
4947
return pieces.join(` `)
5048
})
5149

52-
rewrites = rewrites.map(({
53-
fromPath,
54-
toPath
55-
}: any) => `${fromPath} ${toPath} 200`)
50+
rewrites = rewrites.map(({ fromPath, toPath }: any) => `${fromPath} ${toPath} 200`)
5651

5752
let commentFound = false
5853

src/gatsby-node.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// https://www.netlify.com/docs/headers-and-basic-auth/
2-
3-
import { promises as fs } from 'fs'
42
import { join } from 'path'
53

4+
import { writeFile } from 'fs-extra'
65
import { generatePageDataPath } from 'gatsby-core-utils'
76
import WebpackAssetsManifest from 'webpack-assets-manifest'
87

@@ -14,9 +13,7 @@ import makePluginData from './plugin-data'
1413
const assetsManifest = {}
1514

1615
/** @type {import("gatsby").GatsbyNode["pluginOptionsSchema"]} */
17-
export const pluginOptionsSchema = ({
18-
Joi
19-
}: any) => {
16+
export const pluginOptionsSchema = ({ Joi }: any) => {
2017
const MATCH_ALL_KEYS = /^/
2118

2219
// headers is a specific type used by Netlify: https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/#headers
@@ -44,10 +41,7 @@ export const pluginOptionsSchema = ({
4441
// Inject a webpack plugin to get the file manifests so we can translate all link headers
4542
/** @type {import("gatsby").GatsbyNode["onCreateWebpackConfig"]} */
4643

47-
export const onCreateWebpackConfig = ({
48-
actions,
49-
stage
50-
}: any) => {
44+
export const onCreateWebpackConfig = ({ actions, stage }: any) => {
5145
if (stage !== BUILD_HTML_STAGE && stage !== BUILD_CSS_STAGE) {
5246
return
5347
}
@@ -63,11 +57,7 @@ export const onCreateWebpackConfig = ({
6357
}
6458

6559
/** @type {import("gatsby").GatsbyNode["onPostBuild"]} */
66-
export const onPostBuild = async ({
67-
store,
68-
pathPrefix,
69-
reporter
70-
}: any, userPluginOptions: any) => {
60+
export const onPostBuild = async ({ store, pathPrefix, reporter }: any, userPluginOptions: any) => {
7161
const pluginData = makePluginData(store, assetsManifest, pathPrefix)
7262
const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions }
7363

@@ -113,7 +103,7 @@ export const onPostBuild = async ({
113103

114104
if (!needsFunctions) {
115105
reporter.info(`[gatsby-plugin-netlify] No Netlify functions needed. Skipping...`)
116-
await fs.writeFile(join(program.directory, `.cache`, `.nf-skip-gatsby-functions`), ``)
106+
await writeFile(join(program.directory, `.cache`, `.nf-skip-gatsby-functions`), ``)
117107
}
118108

119109
await Promise.all([

src/plugin-data.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import path from "path"
1+
import path from 'path'
22

3-
export function buildPrefixer(prefix: any, ...paths: any[]) {
4-
return (...subpaths: any[]) => path.join(prefix, ...paths, ...subpaths);
5-
}
3+
const buildPrefixer =
4+
(prefix: any, ...paths: any[]) =>
5+
(...subpaths: any[]) =>
6+
path.join(prefix, ...paths, ...subpaths)
67

78
// This function assembles data across the manifests and store to match a similar
89
// shape of `static-entry.js`. With it, we can build headers that point to the correct
910
// hashed filenames and ensure we pull in the componentChunkName.
10-
export default function makePluginData(store: any, assetsManifest: any, pathPrefix: any) {
11+
const makePluginData = (store: any, assetsManifest: any, pathPrefix: any) => {
1112
const { program, pages, components } = store.getState()
1213
const publicFolder = buildPrefixer(program.directory, `public`)
14+
// eslint-disable-next-line node/global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
1315
const stats = require(publicFolder(`webpack.stats.json`))
1416
// Get all the files, not just the first
1517
const chunkManifest = stats.assetsByChunkName
@@ -25,3 +27,5 @@ export default function makePluginData(store: any, assetsManifest: any, pathPref
2527
publicFolder,
2628
}
2729
}
30+
31+
export { makePluginData as default, buildPrefixer }

0 commit comments

Comments
 (0)