Skip to content

Commit

Permalink
Merge pull request #3698 from alphagov/eslint-markdown
Browse files Browse the repository at this point in the history
Enable ESLint + Stylelint for code inside Markdown
  • Loading branch information
colinrotherham committed Jun 7, 2023
2 parents 0d37c6c + 53d7884 commit e7564bb
Show file tree
Hide file tree
Showing 19 changed files with 480 additions and 156 deletions.
38 changes: 36 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ module.exports = {
// Enable dotfile linting
'!.*',
'node_modules',
'node_modules/.*'
'node_modules/.*',

// Prevent CHANGELOG history changes
'CHANGELOG.md'
],
overrides: [
{
Expand All @@ -18,7 +21,13 @@ module.exports = {
'plugin:n/recommended',
'plugin:promise/recommended'
],
files: ['**/*.{cjs,js,mjs}'],
files: [
'**/*.{cjs,js,mjs}',

// Check markdown `*.md` contains valid code blocks
// https://github.com/eslint/eslint-plugin-markdown#advanced-configuration
'**/*.md/*.{cjs,js,mjs}'
],
parserOptions: {
ecmaVersion: 'latest'
},
Expand Down Expand Up @@ -98,6 +107,31 @@ module.exports = {
env: {
jest: true
}
},
{
// Add plugin for markdown `*.md` code blocks
extends: ['plugin:markdown/recommended'],
files: ['**/*.md'],
plugins: ['markdown'],
processor: 'markdown/markdown'
},
{
files: ['**/coding-standards/js.md/*.{cjs,js,mjs}'],
env: {
browser: true
},
rules: {
// Ignore unused example code
'no-undef': 'off',
'no-unused-vars': 'off',

// Ignore paths to example modules
'import/no-unresolved': 'off',
'n/no-missing-import': 'off',

// Allow `var` in example code
'no-var': 'off'
}
}
],
parserOptions: {
Expand Down
15 changes: 11 additions & 4 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { ESLint } = require('eslint')

module.exports = {
const commands = {
// ESLint's configuration makes it ignore built files in `dist` or `packages/govuk-frontend/dist`
// that we want left alone, as well as the polyfills.
// The glob used by lint-staged to trigger the linting on commit isn't aware
Expand All @@ -11,9 +11,16 @@ module.exports = {
// as recommended by lint-staged.
//
// https://github.com/okonet/lint-staged#how-can-i-ignore-files-from-eslintignore
'*.{cjs,js,mjs}': filterTask('npm run lint:js:cli -- --fix'),
'*.{json,md,yaml,yml}': 'npm run lint:prettier:cli -- --write',
'*.scss': 'npm run lint:scss:cli -- --fix --allow-empty-input'
eslint: filterTask('npm run lint:js:cli -- --fix'),
prettier: 'npm run lint:prettier:cli -- --write',
stylelint: 'npm run lint:scss:cli -- --fix --allow-empty-input'
}

module.exports = {
'*.{cjs,js,mjs}': commands.eslint,
'*.{json,yaml,yml}': commands.prettier,
'*.md': [commands.eslint, commands.stylelint, commands.prettier],
'*.scss': commands.stylelint
}

// Configure paths to ignore
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const Button = require('govuk-frontend/dist/govuk/components/button/button')

For example using `import`:

```js
```mjs
import Button from 'govuk-frontend/dist/govuk-esm/components/button/button.mjs'
```

Expand Down
Loading

0 comments on commit e7564bb

Please sign in to comment.