From 0afc8ac12e99ae0791dc12f989400875a85b646b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 10 Feb 2021 15:20:37 +0100 Subject: [PATCH] tools: add support for mjs and cjs JS snippet linting Refs: https://github.com/nodejs/node/pull/37162 Refs: https://github.com/nodejs/remark-preset-lint-node/pull/176 PR-URL: https://github.com/nodejs/node/pull/37311 Refs: https://github.com/eslint/eslint-plugin-markdown/pull/172 Reviewed-By: Rich Trott --- .eslintrc.js | 21 +++++++++++++-------- Makefile | 1 + doc/api/buffer.md | 2 -- doc/api/module.md | 10 +++++----- doc/api/stream.md | 1 - doc/api/wasi.md | 2 +- doc/guides/writing-tests.md | 1 - 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 92e00d7c9201d8..291436cdc4f9b5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -53,12 +53,6 @@ module.exports = { overrides: [ { files: [ - 'doc/api/esm.md/*.js', - 'doc/api/fs.md/*.js', - 'doc/api/module.md/*.js', - 'doc/api/modules.md/*.js', - 'doc/api/packages.md/*.js', - 'doc/api/wasi.md/*.js', 'test/es-module/test-esm-type-flag.js', 'test/es-module/test-esm-type-flag-alias.js', '*.mjs', @@ -71,10 +65,21 @@ module.exports = { processor: 'markdown/markdown', }, { - files: ['**/*.md/*.js'], - parserOptions: { ecmaFeatures: { impliedStrict: true } }, + files: ['**/*.md/*.cjs', '**/*.md/*.js'], + parserOptions: { + sourceType: 'script', + ecmaFeatures: { impliedStrict: true } + }, rules: { strict: 'off' }, }, + { + files: [ + '**/*.md/*.mjs', + 'doc/api/esm.md/*.js', + 'doc/api/packages.md/*.js', + ], + parserOptions: { sourceType: 'module' }, + }, ], rules: { // ESLint built-in rules diff --git a/Makefile b/Makefile index 86bddc8452556d..d103773a616e1f 100644 --- a/Makefile +++ b/Makefile @@ -1220,6 +1220,7 @@ lint-js-fix: .PHONY: lint-js-doc # Note that on the CI `lint-js-ci` is run instead. # Lints the JavaScript code with eslint. +lint-js-doc: LINT_JS_TARGETS=doc lint-js lint-js-doc: @if [ "$(shell $(node_use_openssl))" != "true" ]; then \ echo "Skipping $@ (no crypto)"; \ diff --git a/doc/api/buffer.md b/doc/api/buffer.md index ab357fe12486f1..764762c986fa09 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -2647,7 +2647,6 @@ buf.writeFloatBE(0xcafebabe, 0); console.log(buf); // Prints: - ``` ### `buf.writeFloatLE(value[, offset])` @@ -2856,7 +2855,6 @@ buf.writeIntBE(0x1234567890ab, 0, 6); console.log(buf); // Prints: - ``` ### `buf.writeIntLE(value, offset, byteLength)` diff --git a/doc/api/module.md b/doc/api/module.md index 1cae60804bf30a..307d08360c4e8b 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -29,13 +29,13 @@ if a module is maintained by a third party or not. `module` in this context isn't the same object that's provided by the [module wrapper][]. To access it, require the `Module` module: -```js +```mjs // module.mjs // In an ECMAScript module import { builtinModules as builtin } from 'module'; ``` -```js +```cjs // module.cjs // In a CommonJS module const builtin = require('module').builtinModules; @@ -51,7 +51,7 @@ added: v12.2.0 string. * Returns: {require} Require function -```js +```mjs import { createRequire } from 'module'; const require = createRequire(import.meta.url); @@ -114,13 +114,13 @@ To enable source map parsing, Node.js must be run with the flag [`--enable-source-maps`][], or with code coverage enabled by setting [`NODE_V8_COVERAGE=dir`][]. -```js +```mjs // module.mjs // In an ECMAScript module import { findSourceMap, SourceMap } from 'module'; ``` -```js +```cjs // module.cjs // In a CommonJS module const { findSourceMap, SourceMap } = require('module'); diff --git a/doc/api/stream.md b/doc/api/stream.md index 0896bf5e856a86..b49110af53e2e9 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -2090,7 +2090,6 @@ const myWritable = new Writable({ }); // Later, abort the operation closing the stream controller.abort(); - ``` #### `writable._construct(callback)`