diff --git a/doc-build/md009.md b/doc-build/md009.md index 5eabc577..8ff10ca6 100644 --- a/doc-build/md009.md +++ b/doc-build/md009.md @@ -1,21 +1,19 @@ This rule is triggered on any lines that end with unexpected whitespace. To fix this, remove the trailing space from the end of the line. -Note: Trailing space is allowed in indented and fenced code blocks because some -languages require it. - The `br_spaces` parameter allows an exception to this rule for a specific number of trailing spaces, typically used to insert an explicit line break. The default -value allows 2 spaces to indicate a hard break (\
element). +value allows 2 spaces to indicate a hard break (\
element). (You must set +`br_spaces` to a value >= 2 for this parameter to take effect. Setting +`br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces.) -Note: You must set `br_spaces` to a value >= 2 for this parameter to take -effect. Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing -spaces. +By default, trailing space is allowed in indented and fenced code blocks because +some programming languages require that. To report such instances, set the +`code_blocks` parameter to `true`. By default, this rule will not trigger when the allowed number of spaces is used, even when it doesn't create a hard break (for example, at the end of a -paragraph). To report such instances as well, set the `strict` parameter to -`true`. +paragraph). To report such instances, set the `strict` parameter to `true`. ```markdown Text text text diff --git a/doc/Rules.md b/doc/Rules.md index eda7730e..a9a675b3 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -295,6 +295,7 @@ Aliases: `no-trailing-spaces` Parameters: - `br_spaces`: Spaces for line break (`integer`, default `2`) +- `code_blocks`: Include code blocks (`boolean`, default `false`) - `list_item_empty_lines`: Allow spaces for empty lines in list items (`boolean`, default `false`) - `strict`: Include unnecessary breaks (`boolean`, default `false`) @@ -304,21 +305,19 @@ Fixable: Some violations can be fixed by tooling This rule is triggered on any lines that end with unexpected whitespace. To fix this, remove the trailing space from the end of the line. -Note: Trailing space is allowed in indented and fenced code blocks because some -languages require it. - The `br_spaces` parameter allows an exception to this rule for a specific number of trailing spaces, typically used to insert an explicit line break. The default -value allows 2 spaces to indicate a hard break (\
element). +value allows 2 spaces to indicate a hard break (\
element). (You must set +`br_spaces` to a value >= 2 for this parameter to take effect. Setting +`br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces.) -Note: You must set `br_spaces` to a value >= 2 for this parameter to take -effect. Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing -spaces. +By default, trailing space is allowed in indented and fenced code blocks because +some programming languages require that. To report such instances, set the +`code_blocks` parameter to `true`. By default, this rule will not trigger when the allowed number of spaces is used, even when it doesn't create a hard break (for example, at the end of a -paragraph). To report such instances as well, set the `strict` parameter to -`true`. +paragraph). To report such instances, set the `strict` parameter to `true`. ```markdown Text text text diff --git a/doc/md009.md b/doc/md009.md index 180008bc..115a2e88 100644 --- a/doc/md009.md +++ b/doc/md009.md @@ -7,6 +7,7 @@ Aliases: `no-trailing-spaces` Parameters: - `br_spaces`: Spaces for line break (`integer`, default `2`) +- `code_blocks`: Include code blocks (`boolean`, default `false`) - `list_item_empty_lines`: Allow spaces for empty lines in list items (`boolean`, default `false`) - `strict`: Include unnecessary breaks (`boolean`, default `false`) @@ -16,21 +17,19 @@ Fixable: Some violations can be fixed by tooling This rule is triggered on any lines that end with unexpected whitespace. To fix this, remove the trailing space from the end of the line. -Note: Trailing space is allowed in indented and fenced code blocks because some -languages require it. - The `br_spaces` parameter allows an exception to this rule for a specific number of trailing spaces, typically used to insert an explicit line break. The default -value allows 2 spaces to indicate a hard break (\
element). +value allows 2 spaces to indicate a hard break (\
element). (You must set +`br_spaces` to a value >= 2 for this parameter to take effect. Setting +`br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces.) -Note: You must set `br_spaces` to a value >= 2 for this parameter to take -effect. Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing -spaces. +By default, trailing space is allowed in indented and fenced code blocks because +some programming languages require that. To report such instances, set the +`code_blocks` parameter to `true`. By default, this rule will not trigger when the allowed number of spaces is used, even when it doesn't create a hard break (for example, at the end of a -paragraph). To report such instances as well, set the `strict` parameter to -`true`. +paragraph). To report such instances, set the `strict` parameter to `true`. ```markdown Text text text diff --git a/lib/configuration-strict.d.ts b/lib/configuration-strict.d.ts index d356278b..73055d21 100644 --- a/lib/configuration-strict.d.ts +++ b/lib/configuration-strict.d.ts @@ -245,6 +245,10 @@ export interface ConfigurationStrict { * Spaces for line break */ br_spaces?: number; + /** + * Include code blocks + */ + code_blocks?: boolean; /** * Allow spaces for empty lines in list items */ @@ -273,6 +277,10 @@ export interface ConfigurationStrict { * Spaces for line break */ br_spaces?: number; + /** + * Include code blocks + */ + code_blocks?: boolean; /** * Allow spaces for empty lines in list items */ diff --git a/lib/md009.mjs b/lib/md009.mjs index 55394ad6..f27a641b 100644 --- a/lib/md009.mjs +++ b/lib/md009.mjs @@ -13,14 +13,18 @@ export default { "function": function MD009(params, onError) { let brSpaces = params.config.br_spaces; brSpaces = Number((brSpaces === undefined) ? 2 : brSpaces); + const codeBlocks = params.config.code_blocks; + const includeCode = (codeBlocks === undefined) ? false : !!codeBlocks; const listItemEmptyLines = !!params.config.list_item_empty_lines; const strict = !!params.config.strict; const codeBlockLineNumbers = new Set(); - for (const codeBlock of filterByTypesCached([ "codeFenced" ])) { - addRangeToSet(codeBlockLineNumbers, codeBlock.startLine + 1, codeBlock.endLine - 1); - } - for (const codeBlock of filterByTypesCached([ "codeIndented" ])) { - addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine); + if (!includeCode) { + for (const codeBlock of filterByTypesCached([ "codeFenced" ])) { + addRangeToSet(codeBlockLineNumbers, codeBlock.startLine + 1, codeBlock.endLine - 1); + } + for (const codeBlock of filterByTypesCached([ "codeIndented" ])) { + addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine); + } } const listItemLineNumbers = new Set(); if (listItemEmptyLines) { diff --git a/schema/.markdownlint.jsonc b/schema/.markdownlint.jsonc index f89ccd80..def3ba78 100644 --- a/schema/.markdownlint.jsonc +++ b/schema/.markdownlint.jsonc @@ -42,6 +42,8 @@ "MD009": { // Spaces for line break "br_spaces": 2, + // Include code blocks + "code_blocks": false, // Allow spaces for empty lines in list items "list_item_empty_lines": false, // Include unnecessary breaks diff --git a/schema/.markdownlint.yaml b/schema/.markdownlint.yaml index d9d0b188..097e9994 100644 --- a/schema/.markdownlint.yaml +++ b/schema/.markdownlint.yaml @@ -37,6 +37,8 @@ MD007: MD009: # Spaces for line break br_spaces: 2 + # Include code blocks + code_blocks: false # Allow spaces for empty lines in list items list_item_empty_lines: false # Include unnecessary breaks diff --git a/schema/build-config-schema.mjs b/schema/build-config-schema.mjs index 56ab797d..3b5a7c2b 100644 --- a/schema/build-config-schema.mjs +++ b/schema/build-config-schema.mjs @@ -161,6 +161,12 @@ for (const rule of rules) { "default": 2 }; // @ts-ignore + subscheme.properties.code_blocks = { + "description": "Include code blocks", + "type": "boolean", + "default": false + }; + // @ts-ignore subscheme.properties.list_item_empty_lines = { "description": "Allow spaces for empty lines in list items", "type": "boolean", diff --git a/schema/markdownlint-config-schema-strict.json b/schema/markdownlint-config-schema-strict.json index 3fdc7176..3ba059ad 100644 --- a/schema/markdownlint-config-schema-strict.json +++ b/schema/markdownlint-config-schema-strict.json @@ -512,6 +512,11 @@ "minimum": 0, "default": 2 }, + "code_blocks": { + "description": "Include code blocks", + "type": "boolean", + "default": false + }, "list_item_empty_lines": { "description": "Allow spaces for empty lines in list items", "type": "boolean", @@ -563,6 +568,11 @@ "minimum": 0, "default": 2 }, + "code_blocks": { + "description": "Include code blocks", + "type": "boolean", + "default": false + }, "list_item_empty_lines": { "description": "Allow spaces for empty lines in list items", "type": "boolean", diff --git a/schema/markdownlint-config-schema.json b/schema/markdownlint-config-schema.json index f1b8341f..85dff84f 100644 --- a/schema/markdownlint-config-schema.json +++ b/schema/markdownlint-config-schema.json @@ -512,6 +512,11 @@ "minimum": 0, "default": 2 }, + "code_blocks": { + "description": "Include code blocks", + "type": "boolean", + "default": false + }, "list_item_empty_lines": { "description": "Allow spaces for empty lines in list items", "type": "boolean", @@ -563,6 +568,11 @@ "minimum": 0, "default": 2 }, + "code_blocks": { + "description": "Include code blocks", + "type": "boolean", + "default": false + }, "list_item_empty_lines": { "description": "Allow spaces for empty lines in list items", "type": "boolean", diff --git a/test/markdownlint-test.mjs b/test/markdownlint-test.mjs index b9f70572..a10497a9 100644 --- a/test/markdownlint-test.mjs +++ b/test/markdownlint-test.mjs @@ -984,6 +984,9 @@ test("customFileSystemSync", (t) => { t.plan(2); const file = "/dir/file.md"; const fsApi = { + "access": () => { throw new Error("access"); }, + "accessSync": () => { throw new Error("accessSync"); }, + "readFile": () => { throw new Error("readFile"); }, // @ts-ignore "readFileSync": (p) => { t.is(p, file); @@ -1001,11 +1004,14 @@ test("customFileSystemAsync", (t) => new Promise((resolve) => { t.plan(3); const file = "/dir/file.md"; const fsApi = { + "access": () => { throw new Error("access"); }, + "accessSync": () => { throw new Error("accessSync"); }, // @ts-ignore "readFile": (p, o, cb) => { t.is(p, file); cb(null, "# Heading"); - } + }, + "readFileSync": () => { throw new Error("readFileSync"); } }; lintAsync({ "files": file, @@ -1095,7 +1101,7 @@ test("readme", async(t) => { }); test("validateJsonUsingConfigSchemaStrict", async(t) => { - t.plan(215); + t.plan(218); // @ts-ignore const ajv = new Ajv(ajvOptions); const validateSchemaStrict = ajv.compile(configSchemaStrict); diff --git a/test/snapshots/markdownlint-test-scenarios.mjs.md b/test/snapshots/markdownlint-test-scenarios.mjs.md index b2ac0def..dc7c2f22 100644 --- a/test/snapshots/markdownlint-test-scenarios.mjs.md +++ b/test/snapshots/markdownlint-test-scenarios.mjs.md @@ -75772,6 +75772,427 @@ Generated by [AVA](https://avajs.dev). `, } +## trailing-spaces-in-code-default.md + +> Snapshot 1 + + { + errors: [ + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 1', + errorRange: [ + 14, + 1, + ], + fixInfo: { + deleteCount: 1, + editColumn: 14, + }, + lineNumber: 4, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 3', + errorRange: [ + 14, + 3, + ], + fixInfo: { + deleteCount: 3, + editColumn: 14, + }, + lineNumber: 6, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 4', + errorRange: [ + 14, + 4, + ], + fixInfo: { + deleteCount: 4, + editColumn: 14, + }, + lineNumber: 7, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + ], + fixed: `# Trailing Spaces in Code - Default␊ + ␊ + const a = 10;␊ + const b = 10;␊ + const c = 10; ␊ + const d = 10;␊ + const e = 10;␊ + ␊ + {MD009:-5} {MD009:-3} {MD009:-2}␊ + ␊ + const a = 10;␊ + const b = 10; ␊ + const c = 10; ␊ + const d = 10; ␊ + const e = 10; ␊ + ␊ + \`\`\`js␊ + const a = 10;␊ + const b = 10; ␊ + const c = 10; ␊ + const d = 10; ␊ + const e = 10; ␊ + \`\`\`␊ + ␊ + ␊ + `, + } + +## trailing-spaces-in-code-false.md + +> Snapshot 1 + + { + errors: [ + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 1', + errorRange: [ + 14, + 1, + ], + fixInfo: { + deleteCount: 1, + editColumn: 14, + }, + lineNumber: 4, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 3', + errorRange: [ + 14, + 3, + ], + fixInfo: { + deleteCount: 3, + editColumn: 14, + }, + lineNumber: 6, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 4', + errorRange: [ + 14, + 4, + ], + fixInfo: { + deleteCount: 4, + editColumn: 14, + }, + lineNumber: 7, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + ], + fixed: `# Trailing Spaces in Code - False␊ + ␊ + const a = 10;␊ + const b = 10;␊ + const c = 10; ␊ + const d = 10;␊ + const e = 10;␊ + ␊ + {MD009:-5} {MD009:-3} {MD009:-2}␊ + ␊ + const a = 10;␊ + const b = 10; ␊ + const c = 10; ␊ + const d = 10; ␊ + const e = 10; ␊ + ␊ + \`\`\`js␊ + const a = 10;␊ + const b = 10; ␊ + const c = 10; ␊ + const d = 10; ␊ + const e = 10; ␊ + \`\`\`␊ + ␊ + ␊ + `, + } + +## trailing-spaces-in-code-true.md + +> Snapshot 1 + + { + errors: [ + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 1', + errorRange: [ + 14, + 1, + ], + fixInfo: { + deleteCount: 1, + editColumn: 14, + }, + lineNumber: 4, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 3', + errorRange: [ + 14, + 3, + ], + fixInfo: { + deleteCount: 3, + editColumn: 14, + }, + lineNumber: 6, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 4', + errorRange: [ + 14, + 4, + ], + fixInfo: { + deleteCount: 4, + editColumn: 14, + }, + lineNumber: 7, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 1', + errorRange: [ + 18, + 1, + ], + fixInfo: { + deleteCount: 1, + editColumn: 18, + }, + lineNumber: 12, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 3', + errorRange: [ + 18, + 3, + ], + fixInfo: { + deleteCount: 3, + editColumn: 18, + }, + lineNumber: 14, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 4', + errorRange: [ + 18, + 4, + ], + fixInfo: { + deleteCount: 4, + editColumn: 18, + }, + lineNumber: 15, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 1', + errorRange: [ + 14, + 1, + ], + fixInfo: { + deleteCount: 1, + editColumn: 14, + }, + lineNumber: 21, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 3', + errorRange: [ + 14, + 3, + ], + fixInfo: { + deleteCount: 3, + editColumn: 14, + }, + lineNumber: 23, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + { + errorContext: null, + errorDetail: 'Expected: 0 or 2; Actual: 4', + errorRange: [ + 14, + 4, + ], + fixInfo: { + deleteCount: 4, + editColumn: 14, + }, + lineNumber: 24, + ruleDescription: 'Trailing spaces', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md', + ruleNames: [ + 'MD009', + 'no-trailing-spaces', + ], + severity: 'error', + }, + ], + fixed: `# Trailing Spaces in Code - True␊ + ␊ + const a = 10;␊ + const b = 10;␊ + const c = 10; ␊ + const d = 10;␊ + const e = 10;␊ + ␊ + {MD009:-5} {MD009:-3} {MD009:-2}␊ + ␊ + const a = 10;␊ + const b = 10;␊ + const c = 10; ␊ + const d = 10;␊ + const e = 10;␊ + ␊ + {MD009:-5} {MD009:-3} {MD009:-2}␊ + ␊ + \`\`\`js␊ + const a = 10;␊ + const b = 10;␊ + const c = 10; ␊ + const d = 10;␊ + const e = 10;␊ + \`\`\`␊ + ␊ + {MD009:-6} {MD009:-4} {MD009:-3}␊ + ␊ + ␊ + `, + } + ## trailing-spaces-in-lists-allowed-strict.md > Snapshot 1 diff --git a/test/snapshots/markdownlint-test-scenarios.mjs.snap b/test/snapshots/markdownlint-test-scenarios.mjs.snap index 3e71001a..1a3c8359 100644 Binary files a/test/snapshots/markdownlint-test-scenarios.mjs.snap and b/test/snapshots/markdownlint-test-scenarios.mjs.snap differ diff --git a/test/trailing-spaces-in-code-default.md b/test/trailing-spaces-in-code-default.md new file mode 100644 index 00000000..cad8c611 --- /dev/null +++ b/test/trailing-spaces-in-code-default.md @@ -0,0 +1,27 @@ +# Trailing Spaces in Code - Default + +const a = 10; +const b = 10; +const c = 10; +const d = 10; +const e = 10; + +{MD009:-5} {MD009:-3} {MD009:-2} + + const a = 10; + const b = 10; + const c = 10; + const d = 10; + const e = 10; + +```js +const a = 10; +const b = 10; +const c = 10; +const d = 10; +const e = 10; +``` + + diff --git a/test/trailing-spaces-in-code-false.md b/test/trailing-spaces-in-code-false.md new file mode 100644 index 00000000..d217f231 --- /dev/null +++ b/test/trailing-spaces-in-code-false.md @@ -0,0 +1,30 @@ +# Trailing Spaces in Code - False + +const a = 10; +const b = 10; +const c = 10; +const d = 10; +const e = 10; + +{MD009:-5} {MD009:-3} {MD009:-2} + + const a = 10; + const b = 10; + const c = 10; + const d = 10; + const e = 10; + +```js +const a = 10; +const b = 10; +const c = 10; +const d = 10; +const e = 10; +``` + + diff --git a/test/trailing-spaces-in-code-true.md b/test/trailing-spaces-in-code-true.md new file mode 100644 index 00000000..f7a27c08 --- /dev/null +++ b/test/trailing-spaces-in-code-true.md @@ -0,0 +1,34 @@ +# Trailing Spaces in Code - True + +const a = 10; +const b = 10; +const c = 10; +const d = 10; +const e = 10; + +{MD009:-5} {MD009:-3} {MD009:-2} + + const a = 10; + const b = 10; + const c = 10; + const d = 10; + const e = 10; + +{MD009:-5} {MD009:-3} {MD009:-2} + +```js +const a = 10; +const b = 10; +const c = 10; +const d = 10; +const e = 10; +``` + +{MD009:-6} {MD009:-4} {MD009:-3} + +