Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-typescript): Supports linting #18721

Merged
merged 32 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a2a8b5d
Add tsx to the rule
ThewBear Oct 16, 2019
ce6be47
Undo add tsx
ThewBear Oct 16, 2019
b5d4116
Add eslint support to gatsby-plugin-typescript
ThewBear Oct 16, 2019
6b3bcbe
Fix prettier error
ThewBear Oct 16, 2019
4565295
Check if there is no eslintLoader
ThewBear Oct 16, 2019
7175454
Update tests
ThewBear Oct 16, 2019
25ed534
Add tests
ThewBear Oct 16, 2019
aa29318
Fix linting
ThewBear Oct 16, 2019
b9c45f8
Update getState mock
ThewBear Oct 16, 2019
539c2dd
Fix tests
ThewBear Oct 16, 2019
fc149a7
Reformat getState fn
ThewBear Oct 16, 2019
4ee144f
Merge master
ThewBear Oct 16, 2019
7fd2eee
Update gatsby-node.js
ThewBear Oct 17, 2019
48f13e1
Update gatsby-node.js
ThewBear Oct 17, 2019
3834887
Fix lint
ThewBear Oct 17, 2019
e15d431
Update gatsby-node.js
ThewBear Oct 17, 2019
c1f9130
Update gatsby-node.js
ThewBear Oct 17, 2019
d9e779c
Fix lint
ThewBear Oct 17, 2019
95cf526
Update gatsby-node.js
ThewBear Oct 17, 2019
263fa5b
Merge remote-tracking branch 'upstream/master' into ThewBear-eslint-t…
Oct 25, 2019
d246659
Add typescript as peerDependencies
ThewBear Jan 26, 2020
9e76136
Merge pull request #5 from gatsbyjs/master
ThewBear Jan 26, 2020
bf56f6c
Update README.md
ThewBear Jan 26, 2020
21b1ba3
Update package.json
ThewBear Jan 26, 2020
5bdcbb5
Update gatsby-node.js
ThewBear Jan 26, 2020
31e1f96
Update gatsby-node.js
ThewBear Jan 26, 2020
cba44e5
Update gatsby-node.js
ThewBear Jan 26, 2020
3542e51
Update gatsby-node.js
ThewBear Jan 26, 2020
9c86f02
Update gatsby-node.js
ThewBear Jan 26, 2020
6a964ff
Update gatsby-node.js
ThewBear Jan 27, 2020
4038bb1
Update package.json
ThewBear Jan 27, 2020
c3bdbbf
Merge branch 'master' into ThewBear-eslint-typescript
wardpeet Mar 1, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/gatsby-plugin-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ Visual Studio Code is very good in this regard.

In addition, you can see the instructions in [TypeScript-Babel-Starter](https://github.com/Microsoft/TypeScript-Babel-Starter)
for setting up a `type-check` task.

## ESLint

This plugin supports linting TSX with [typescript-eslint](https://typescript-eslint.io) using [Gatsby's default ESLint config](https://www.gatsbyjs.org/docs/eslint/). To enable linting TSX, install `typescript`.

`npm install typescript`
8 changes: 7 additions & 1 deletion packages/gatsby-plugin-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.0",
"typescript": "^3.2.1"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
},
"repository": {
"type": "git",
Expand Down
122 changes: 120 additions & 2 deletions packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,22 @@ describe(`gatsby-plugin-typescript`, () => {
const actions = { setWebpackConfig: jest.fn() }
const jsLoader = {}
const loaders = { js: jest.fn(() => jsLoader) }
onCreateWebpackConfig({ actions, loaders })
const stage = `develop`
const eslintLoader = { loader: `eslint-loader` }
const webpackConfig = {
module: {
rules: [
{
enforce: `pre`,
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: [eslintLoader],
},
],
},
}
const getConfig = jest.fn(() => webpackConfig)
onCreateWebpackConfig({ actions, getConfig, loaders, stage })
expect(actions.setWebpackConfig).toHaveBeenCalledWith({
module: {
rules: [
Expand All @@ -60,13 +75,116 @@ describe(`gatsby-plugin-typescript`, () => {
],
},
})
expect(actions.setWebpackConfig).toHaveBeenCalledWith({
module: {
rules: [
{
enforce: `pre`,
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
use: [eslintLoader],
},
],
},
})
})

it(`does not set the webpack config if there isn't a js loader`, () => {
const actions = { setWebpackConfig: jest.fn() }
const loaders = { js: jest.fn() }
onCreateWebpackConfig({ actions, loaders })
const stage = `develop`
const getConfig = jest.fn()
onCreateWebpackConfig({ actions, getConfig, loaders, stage })
expect(actions.setWebpackConfig).not.toHaveBeenCalled()
})

it(`does not set the typescript-eslint webpack config if the built-in eslint-loader isn't set`, () => {
const actions = { setWebpackConfig: jest.fn() }
const jsLoader = {}
const loaders = {
js: jest.fn(() => jsLoader),
}
const stage = `develop`
const webpackConfig = {
module: {
rules: [
{
enforce: `pre`,
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: [],
},
],
},
}
const getConfig = jest.fn(() => webpackConfig)
onCreateWebpackConfig({ actions, getConfig, loaders, stage })
expect(actions.setWebpackConfig).toHaveBeenCalledWith({
module: {
rules: [
{
test: /\.tsx?$/,
use: jsLoader,
},
],
},
})
expect(actions.setWebpackConfig).not.toHaveBeenCalledWith({
module: {
rules: [
{
enforce: `pre`,
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
use: [],
},
],
},
})
})

it(`set the typescript-eslint webpack config only if in develop stage`, () => {
const actions = { setWebpackConfig: jest.fn() }
const jsLoader = {}
const loaders = { js: jest.fn(() => jsLoader) }
const stage = `build-html`
const eslintLoader = { loader: `eslint-loader` }
const webpackConfig = {
module: {
rules: [
{
enforce: `pre`,
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: [eslintLoader],
},
],
},
}
const getConfig = jest.fn(() => webpackConfig)
onCreateWebpackConfig({ actions, getConfig, loaders, stage })
expect(actions.setWebpackConfig).toHaveBeenCalledWith({
module: {
rules: [
{
test: /\.tsx?$/,
use: jsLoader,
},
],
},
})
expect(actions.setWebpackConfig).not.toHaveBeenCalledWith({
module: {
rules: [
{
enforce: `pre`,
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
use: [eslintLoader],
},
],
},
})
})
})
})
40 changes: 39 additions & 1 deletion packages/gatsby-plugin-typescript/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ function onCreateBabelConfig({ actions }, options) {
})
}

function onCreateWebpackConfig({ actions, loaders }) {
function onCreateWebpackConfig({
actions,
getConfig,
loaders,
stage,
reporter,
}) {
const jsLoader = loaders.js()

if (!jsLoader) {
Expand All @@ -33,6 +39,38 @@ function onCreateWebpackConfig({ actions, loaders }) {
],
},
})

if (stage === `develop`) {
let isTypescriptDepAvailable
try {
isTypescriptDepAvailable = require.resolve(`typescript`)
} catch (e) {
reporter.warn(
`"typescript" is not installed. Builtin ESLint won't be working on typescript files.`
)
}

if (isTypescriptDepAvailable) {
const builtInEslintRule = getConfig().module.rules.find(rule => {
ThewBear marked this conversation as resolved.
Show resolved Hide resolved
if (rule.enforce === `pre`) {
return rule.use.some(use => /eslint-loader/.test(use.loader))
}
return false
})

if (builtInEslintRule) {
const typescriptEslintRule = {
...builtInEslintRule,
test: /\.tsx?$/,
}
actions.setWebpackConfig({
module: {
rules: [typescriptEslintRule],
},
})
}
}
}
}

exports.resolvableExtensions = resolvableExtensions
Expand Down