Skip to content

Commit

Permalink
chore: lint code blocks in docs with ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 committed May 10, 2024
1 parent e2acdff commit 925a227
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 22 deletions.
21 changes: 21 additions & 0 deletions docs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"plugins": [
"markdown",
"unicorn"
],
"overrides": [
{
"files": ["*.md", "**/*.md"],
"processor": "markdown/markdown"
}
],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"indent": "off",
"no-undef": "off",
"no-unused-expressions": "off",
"no-unused-vars": "off",
"semi": "off",
"unicorn/prefer-node-protocol": "error"
}
}
2 changes: 2 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,8 @@ For `infoType` equal to `complete`:
For `infoType` equal to `basic`:
Promise is fulfilled with `Object` containing fewer attributes than when requested with `complete`. Here's an example of basic response:

<!-- eslint-skip -->

```js
{
auxAttributes:
Expand Down
2 changes: 1 addition & 1 deletion docs/api/browser-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ method:
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()

const url = require('url').format({
const url = require('node:url').format({
protocol: 'file',
slashes: true,
pathname: require('node:path').join(__dirname, 'index.html')
Expand Down
4 changes: 4 additions & 0 deletions docs/api/dialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ The `browserWindow` argument allows the dialog to attach itself to a parent wind
The `filters` specifies an array of file types that can be displayed or
selected when you want to limit the user to a specific type. For example:

<!-- eslint-skip -->

```js
{
filters: [
Expand Down Expand Up @@ -119,6 +121,8 @@ The `browserWindow` argument allows the dialog to attach itself to a parent wind
The `filters` specifies an array of file types that can be displayed or
selected when you want to limit the user to a specific type. For example:

<!-- eslint-skip -->

```js
{
filters: [
Expand Down
6 changes: 3 additions & 3 deletions docs/api/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ to register it to that session explicitly.
```js
const { app, BrowserWindow, net, protocol, session } = require('electron')
const path = require('node:path')
const url = require('url')
const url = require('node:url')

app.whenReady().then(() => {
const partition = 'persist:example'
Expand Down Expand Up @@ -124,7 +124,7 @@ Example:
```js
const { app, net, protocol } = require('electron')
const path = require('node:path')
const { pathToFileURL } = require('url')
const { pathToFileURL } = require('node:url')

protocol.registerSchemesAsPrivileged([
{
Expand Down Expand Up @@ -280,7 +280,7 @@ Example:

```js
const { protocol } = require('electron')
const { PassThrough } = require('stream')
const { PassThrough } = require('node:stream')

function createStream (text) {
const rv = new PassThrough() // PassThrough is also a Readable stream
Expand Down
2 changes: 1 addition & 1 deletion docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ To clear the handler, call `setPermissionCheckHandler(null)`.
```js
const { session } = require('electron')
const url = require('url')
const url = require('node:url')
session.fromPartition('some-partition').setPermissionCheckHandler((webContents, permission, requestingOrigin) => {
if (new URL(requestingOrigin).hostname === 'some-host' && permission === 'notifications') {
return true // granted
Expand Down
2 changes: 2 additions & 0 deletions docs/api/structures/printer-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The number represented by `status` means different things on different platforms
Below is an example of some of the additional options that may be set which
may be different on each platform.

<!-- eslint-skip -->

```js
{
name: 'Austin_4th_Floor_Printer___C02XK13BJHD4',
Expand Down
2 changes: 2 additions & 0 deletions docs/api/structures/trace-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

An example TraceConfig that roughly matches what Chrome DevTools records:

<!-- eslint-skip -->

```js
{
recording_mode: 'record-until-full',
Expand Down
2 changes: 2 additions & 0 deletions docs/api/web-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ console.log(webFrame.getResourceUsage())

This will generate:

<!-- eslint-skip -->

```js
{
images: {
Expand Down
2 changes: 1 addition & 1 deletion docs/development/creating-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,6 @@ Add your module to the module list found at `"lib/browser/api/module-list.ts"` l

```ts title='lib/browser/api/module-list.ts' @ts-nocheck
export const browserModuleList: ElectronInternal.ModuleEntry[] = [
{ name: 'apiName', loader: () => require('./api-name') },
{ name: 'apiName', loader: () => require('./api-name') }
];
```
2 changes: 2 additions & 0 deletions docs/tutorial/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ The `@babel/plugin-transform-modules-commonjs` plugin will transform
ESM imports down to `require` calls. The exact syntax will depend on the
[`importInterop` setting](https://babeljs.io/docs/babel-plugin-transform-modules-commonjs#importinterop).

<!-- eslint-disable no-var, quotes -->

```js @nolint @ts-nocheck title='@babel/plugin-transform-modules-commonjs'
import foo from "foo";
import { bar } from "bar";
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial/ipc.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ function createWindow () {
For the purposes of the tutorial, it's important to note that the `click` handler
sends a message (either `1` or `-1`) to the renderer process through the `update-counter` channel.

<!-- eslint-skip -->

```js @ts-type={mainWindow:Electron.BrowserWindow}
click: () => mainWindow.webContents.send('update-counter', -1)
```
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ security-conscious developers might want to assume the very opposite.

```js title='main.js (Main Process)'
const { session } = require('electron')
const { URL } = require('url')
const { URL } = require('node:url')

session
.fromPartition('some-partition')
Expand Down Expand Up @@ -610,7 +610,7 @@ sometimes be fooled - a `startsWith('https://example.com')` test would let
`https://example.com.attacker.com` through.

```js title='main.js (Main Process)'
const { URL } = require('url')
const { URL } = require('node:url')
const { app } = require('electron')

app.on('web-contents-created', (event, contents) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"eslint": "^8.41.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-markdown": "^5.0.0",
"eslint-plugin-mocha": "^7.0.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
Expand Down
54 changes: 40 additions & 14 deletions script/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ function spawnAndCheckExitCode (cmd, args, opts) {
}
}

async function runEslint (eslint, filenames, { fix, verbose }) {
const formatter = await eslint.loadFormatter();
let successCount = 0;
const results = await eslint.lintFiles(filenames);
for (const result of results) {
successCount += result.errorCount === 0 ? 1 : 0;
if (verbose && result.errorCount === 0 && result.warningCount === 0) {
console.log(`${result.filePath}: no errors or warnings`);
}
}
console.log(formatter.format(results));
if (fix) {
await ESLint.outputFixes(results);
}

return successCount === filenames.length;
}

function cpplint (args) {
args.unshift(`--root=${SOURCE_ROOT}`);
const cmd = IS_WINDOWS ? 'cpplint.bat' : 'cpplint.py';
Expand Down Expand Up @@ -141,20 +159,8 @@ const LINTERS = [{
overrideConfigFile: path.join(ELECTRON_ROOT, '.eslintrc.json'),
resolvePluginsRelativeTo: ELECTRON_ROOT
});
const formatter = await eslint.loadFormatter();
let successCount = 0;
const results = await eslint.lintFiles(filenames);
for (const result of results) {
successCount += result.errorCount === 0 ? 1 : 0;
if (opts.verbose && result.errorCount === 0 && result.warningCount === 0) {
console.log(`${result.filePath}: no errors or warnings`);
}
}
console.log(formatter.format(results));
if (opts.fix) {
await ESLint.outputFixes(results);
}
if (successCount !== filenames.length) {
const clean = await runEslint(eslint, filenames, { fix: opts.fix, verbose: opts.verbose });
if (!clean) {
console.error('Linting had errors');
process.exit(1);
}
Expand Down Expand Up @@ -357,6 +363,26 @@ const LINTERS = [{
}
}

const eslint = new ESLint({
// Do not use the lint cache on CI builds
cache: !process.env.CI,
cacheLocation: `node_modules/.eslintcache.${crypto.createHash('md5').update(fs.readFileSync(__filename)).digest('hex')}`,
fix: opts.fix,
overrideConfigFile: path.join(ELECTRON_ROOT, 'docs', '.eslintrc.json'),
resolvePluginsRelativeTo: ELECTRON_ROOT
});
const clean = await runEslint(
eslint,
docs.filter(
// TODO(dsanders11): Once we move to newer ESLint and the flat config,
// switch to using `ignorePatterns` and `warnIgnore: false` instead of
// explicitly filtering out this file that we don't want to lint
(filename) => !filename.endsWith('docs/breaking-changes.md')
),
{ fix: opts.fix, verbose: opts.verbose }
);
errors ||= !clean;

if (errors) {
process.exit(1);
}
Expand Down

0 comments on commit 925a227

Please sign in to comment.