diff --git a/fixtures/output/no-markdown-with-formatters/javascript.js b/fixtures/output/no-markdown-with-formatters/javascript.js new file mode 100644 index 0000000000..0a25a0a6f0 --- /dev/null +++ b/fixtures/output/no-markdown-with-formatters/javascript.js @@ -0,0 +1,72 @@ +// This file is generated by ChatGPT + +// eslint-disable-next-line no-console +const log = console.log + +// Define a class using ES6 class syntax +class Person { + constructor(name, age) { + this.name = name + this.age = age + } + + // Define a method within the class + sayHello() { + log(`Hello, my name is ${this.name} and I am ${this.age} years old.`) + } +} + +// Create an array of objects +const people = [ + new Person('Alice', 30), + new Person('Bob', 25), + new Person('Charlie', 35), +] + +// Use the forEach method to iterate over the array +people.forEach((person) => { + person.sayHello() +}) + +// Use a template literal to create a multiline string +const multilineString = ` + This is a multiline string + that spans multiple lines. +` + +// Use destructuring assignment to extract values from an object +const { name, age } = people[0] +log(`First person in the array is ${name} and they are ${age} years old.`, multilineString) + +// Use the spread operator to create a new array +const numbers = [1, 2, 3] +const newNumbers = [...numbers, 4, 5] +log(newNumbers) + +// Use a try-catch block for error handling +try { + // Attempt to parse an invalid JSON string + JSON.parse('invalid JSON') +} +catch (error) { + console.error('Error parsing JSON:', error.message) +} + +// Use a ternary conditional operator +const isEven = num => num % 2 === 0 +const number = 7 +log(`${number} is ${isEven(number) ? 'even' : 'odd'}.`) + +// Use a callback function with setTimeout for asynchronous code +setTimeout(() => { + log('This code runs after a delay of 2 seconds.') +}, 2000) + +let a, b, c, d, foo + +if (a + || b + || c || d + || (d && b) +) + foo() diff --git a/fixtures/output/no-markdown-with-formatters/jsx.jsx b/fixtures/output/no-markdown-with-formatters/jsx.jsx new file mode 100644 index 0000000000..1a5b5934d3 --- /dev/null +++ b/fixtures/output/no-markdown-with-formatters/jsx.jsx @@ -0,0 +1,24 @@ +export function HelloWorld({ + greeting = 'hello', + greeted = '"World"', + silent = false, + onMouseOver, +}) { + if (!greeting) + return null + + // TODO: Don't use random in render + const num = Math.floor (Math.random() * 1e+7).toString() + .replace(/\.\d+/ig, '') + + return
+ { greeting.slice(0, 1).toUpperCase() + greeting.slice(1).toLowerCase() } + {greeting.endsWith(',') + ? ' ' + : ", " } + + { greeted } + + { (silent) ? '.' : '!'} +
+} diff --git a/fixtures/output/no-markdown-with-formatters/markdown.md b/fixtures/output/no-markdown-with-formatters/markdown.md new file mode 100644 index 0000000000..b049fd9262 --- /dev/null +++ b/fixtures/output/no-markdown-with-formatters/markdown.md @@ -0,0 +1,33 @@ +# Header + +_Look,_ code blocks are formatted _too!_ + +```js +// This should be handled by ESLint instead of Prettier +function identity(x) { + if (foo) { + console.log('bar'); + } + } +``` + +```css +/* This should be handled by Prettier */ +.foo { color:red;} +``` + +| Pilot | Airport | Hours | +| -------- | :-----: | ----: | +| John Doe | SKG | 1338 | +| Jane Roe | JFK | 314 | + +--- + +- List +- with a [link] (/to/somewhere) +- and [another one] + + [another one]: http://example.com 'Example title' + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Curabitur consectetur maximus risus, sed maximus tellus tincidunt et. diff --git a/fixtures/output/no-markdown-with-formatters/tsx.tsx b/fixtures/output/no-markdown-with-formatters/tsx.tsx new file mode 100644 index 0000000000..40f93c4ce4 --- /dev/null +++ b/fixtures/output/no-markdown-with-formatters/tsx.tsx @@ -0,0 +1,23 @@ +export function Component1() { + return
+} + +export function jsx2() { + const props = { a: 1, b: 2 } + return < a foo= 'bar' bar={`foo` } > +
Inline Text
+ + Block Text + +
+ Mixed +
Foo
+ Text Bar +
+

+ foobarbaz +

+ +} diff --git a/fixtures/output/no-markdown-with-formatters/typescript.ts b/fixtures/output/no-markdown-with-formatters/typescript.ts new file mode 100644 index 0000000000..2446fc7bce --- /dev/null +++ b/fixtures/output/no-markdown-with-formatters/typescript.ts @@ -0,0 +1,83 @@ +// Define a TypeScript interface +interface Person { + name: string + age: number +} + +// Create an array of objects with the defined interface +const people: Person[] = [ + { name: 'Alice', age: 30 }, + { name: 'Bob', age: 25 }, + { name: 'Charlie', age: 35 }, +] + +// eslint-disable-next-line no-console +const log = console.log + +// Use a for...of loop to iterate over the array +for (const person of people) + log(`Hello, my name is ${person.name} and I am ${person.age} years old.`) + +// Define a generic function +function identity< T >(arg: T): T { + return arg +} + +// Use the generic function with type inference +const result = identity( + 'TypeScript is awesome', +) +log(result) + +// Use optional properties in an interface +interface Car { + make: string + model?: string +} + +// Create objects using the interface +const car1: Car = { make: 'Toyota' } +const car2: Car = { + make: 'Ford', + model: 'Focus', +} + +// Use union types +type Fruit = 'apple' | 'banana' | 'orange' +const favoriteFruit: Fruit = 'apple' + +// Use a type assertion to tell TypeScript about the type +const inputValue: any = '42' +const numericValue = inputValue as number + +// Define a class with access modifiers +class Animal { + private name: string + constructor(name: string) { + this.name = name + } + + protected makeSound(sound: string) { + log(`${this.name} says ${sound}`) + } +} + +// Extend a class +class Dog extends Animal { + constructor(private alias: string) { + super(alias) + } + + bark() { + this.makeSound('Woof!') + } +} + +const dog = new Dog('Buddy') +dog.bark() + +function fn(): string { + return `hello${1}` +} + +log(car1, car2, favoriteFruit, numericValue, fn()) diff --git a/src/configs/formatters.ts b/src/configs/formatters.ts index 8ec219cd5a..9ee4493bd6 100644 --- a/src/configs/formatters.ts +++ b/src/configs/formatters.ts @@ -1,4 +1,4 @@ -import { GLOB_CSS, GLOB_LESS, GLOB_POSTCSS, GLOB_SCSS } from '../globs' +import { GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs' import type { VendoredPrettierOptions } from '../vender/prettier-types' import { ensurePackages, interopDefault } from '../utils' import type { FlatConfigItem, OptionsFormatters, StylisticConfig } from '../types' @@ -7,6 +7,7 @@ import { StylisticConfigDefaults } from './stylistic' export async function formatters( options: OptionsFormatters | true = {}, stylistic: StylisticConfig = {}, + markdownEnabled = true, ): Promise { await ensurePackages([ 'eslint-plugin-format', @@ -154,10 +155,14 @@ export async function formatters( } if (options.markdown) { - const formater = options.markdown === true ? 'prettier' : options.markdown + const formater = options.markdown === true + ? 'prettier' + : options.markdown configs.push({ - files: ['**/*.__markdown_content__'], + files: markdownEnabled + ? ['**/*.__markdown_content__'] + : [GLOB_MARKDOWN], languageOptions: { parser: pluginFormat.parserPlain, }, diff --git a/src/factory.ts b/src/factory.ts index cabbb172b0..cec1ceb3f9 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -181,6 +181,7 @@ export async function antfu( configs.push(formatters( options.formatters, typeof stylisticOptions === 'boolean' ? {} : stylisticOptions, + options.markdown !== false, )) } diff --git a/src/types.ts b/src/types.ts index 7a0ac5266a..a22a62af02 100644 --- a/src/types.ts +++ b/src/types.ts @@ -244,7 +244,9 @@ export interface OptionsConfig extends OptionsComponentExts { yaml?: boolean /** - * Enable Markdown support. + * Enable linting for **code snippets** in Markdown. + * + * For formatting Markdown content, enable also `formatters.markdown`. * * @default true */ diff --git a/test/fixtures.test.ts b/test/fixtures.test.ts index 298e648e4d..af0aa64676 100644 --- a/test/fixtures.test.ts +++ b/test/fixtures.test.ts @@ -64,6 +64,18 @@ runWithConfig( }, ) +runWithConfig( + 'no-markdown-with-formatters', + { + jsx: false, + vue: false, + markdown: false, + formatters: { + markdown: true, + }, + }, +) + function runWithConfig(name: string, configs: OptionsConfig, ...items: FlatConfigItem[]) { it.concurrent(name, async ({ expect }) => { const from = resolve('fixtures/input')