Skip to content

Commit

Permalink
feat: granular stylistic control
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 27, 2023
1 parent 91dee42 commit 30c870d
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 67 deletions.
10 changes: 5 additions & 5 deletions fixtures/input/javascript.js
Expand Up @@ -7,13 +7,13 @@ var log = console.log
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
this.age = age;
}

// Define a method within the class
sayHello() {
log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
// 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
Expand Down
4 changes: 2 additions & 2 deletions fixtures/input/vue.vue
Expand Up @@ -9,10 +9,10 @@

<script setup>
// Define reactive data and props
import { ref } from 'vue';
import { ref } from '@vue/reactivity';
const greeting = ref('Hello, Vue 3!' + 1);
let counter = ref(0);
let counter = ref(0)
// Define a function
const incrementCounter = () => {
Expand Down
62 changes: 62 additions & 0 deletions fixtures/output/no-style/javascript.js
@@ -0,0 +1,62 @@
// 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);
79 changes: 79 additions & 0 deletions fixtures/output/no-style/typescript.ts
@@ -0,0 +1,79 @@
// 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())
22 changes: 22 additions & 0 deletions fixtures/output/no-style/vue-ts.vue
@@ -0,0 +1,22 @@
<script setup lang="ts">
// Define reactive data and props
import { ref } from 'vue';
const greeting = ref('Hello, Vue 3!');
const counter = ref<number | string>(0);
// Define a function
function incrementCounter () {
counter.value++;
}
</script>

<template>
<div>
<h1>{{ greeting }}</h1>
<button @click="incrementCounter">
Click me!
</button>
<p>Counter: {{ counter }}</p>
</div>
</template>
24 changes: 24 additions & 0 deletions fixtures/output/no-style/vue.vue
@@ -0,0 +1,24 @@
<script setup>
// Define reactive data and props
import { ref } from 'vue';
const greeting = ref(`Hello, Vue 3!${ 1}`);
const counter = ref(0)
// Define a function
function incrementCounter () {
counter.value++;
}
</script>

<template>
<div>
<h1>
{{ greeting }}
</h1>
<button @click="incrementCounter">
Click me!
</button>
<p>Counter: {{ counter }}</p>
</div>
</template>
14 changes: 12 additions & 2 deletions src/configs/imports.ts
@@ -1,7 +1,12 @@
import type { FlatESLintConfigItem } from 'eslint-define-config'
import { pluginImport } from '../plugins'
import type { OptionsStylistic } from '../types'

export function imports(options: OptionsStylistic = {}): FlatESLintConfigItem[] {
const {
stylistic = true,
} = options

export function imports(): FlatESLintConfigItem[] {
return [
{
plugins: {
Expand All @@ -10,13 +15,18 @@ export function imports(): FlatESLintConfigItem[] {
rules: {
'import/export': 'error',
'import/first': 'error',
'import/newline-after-import': ['error', { considerComments: true, count: 1 }],
'import/no-duplicates': 'error',
'import/no-mutable-exports': 'error',
'import/no-named-default': 'error',
'import/no-self-import': 'error',
'import/no-webpack-loader-syntax': 'error',
'import/order': 'error',

...stylistic
? {
'import/newline-after-import': ['error', { considerComments: true, count: 1 }],
}
: {},
},
},
]
Expand Down
16 changes: 13 additions & 3 deletions src/configs/jsdoc.ts
@@ -1,21 +1,24 @@
import type { FlatESLintConfigItem } from 'eslint-define-config'
import { pluginJsdoc } from '../plugins'
import type { OptionsStylistic } from '../types'

export function jsdoc(options: OptionsStylistic = {}): FlatESLintConfigItem[] {
const {
stylistic = true,
} = options

export function jsdoc(): FlatESLintConfigItem[] {
return [
{
plugins: {
jsdoc: pluginJsdoc,
},
rules: {
'jsdoc/check-access': 'warn',
'jsdoc/check-alignment': 'warn',
'jsdoc/check-param-names': 'warn',
'jsdoc/check-property-names': 'warn',
'jsdoc/check-types': 'warn',
'jsdoc/empty-tags': 'warn',
'jsdoc/implements-on-classes': 'warn',
'jsdoc/multiline-blocks': 'warn',
'jsdoc/no-defaults': 'warn',
'jsdoc/no-multi-asterisks': 'warn',
'jsdoc/require-param-name': 'warn',
Expand All @@ -26,6 +29,13 @@ export function jsdoc(): FlatESLintConfigItem[] {
'jsdoc/require-returns-description': 'warn',
'jsdoc/require-yields-check': 'warn',
'jsdoc/valid-types': 'warn',

...stylistic
? {
'jsdoc/check-alignment': 'warn',
'jsdoc/multiline-blocks': 'warn',
}
: {},
},
},
]
Expand Down
36 changes: 25 additions & 11 deletions src/configs/jsonc.ts
@@ -1,8 +1,14 @@
import type { FlatESLintConfigItem } from 'eslint-define-config'
import { GLOB_JSON, GLOB_JSON5, GLOB_JSONC } from '../globs'
import { parserJsonc, pluginJsonc } from '../plugins'
import type { OptionsOverrides, OptionsStylistic } from '../types'

export function jsonc(options: OptionsStylistic & OptionsOverrides = {}): FlatESLintConfigItem[] {
const {
stylistic = true,
overrides = {},
} = options

export function jsonc(): FlatESLintConfigItem[] {
return [
{
plugins: {
Expand All @@ -15,11 +21,7 @@ export function jsonc(): FlatESLintConfigItem[] {
parser: parserJsonc,
},
rules: {
'jsonc/array-bracket-spacing': ['error', 'never'],
'jsonc/comma-dangle': ['error', 'never'],
'jsonc/comma-style': ['error', 'last'],
'jsonc/indent': ['error', 2],
'jsonc/key-spacing': ['error', { afterColon: true, beforeColon: false }],

'jsonc/no-bigint-literals': 'error',
'jsonc/no-binary-expression': 'error',
'jsonc/no-binary-numeric-literals': 'error',
Expand All @@ -43,14 +45,26 @@ export function jsonc(): FlatESLintConfigItem[] {
'jsonc/no-undefined-value': 'error',
'jsonc/no-unicode-codepoint-escapes': 'error',
'jsonc/no-useless-escape': 'error',
'jsonc/object-curly-newline': ['error', { consistent: true, multiline: true }],
'jsonc/object-curly-spacing': ['error', 'always'],
'jsonc/object-property-newline': ['error', { allowMultiplePropertiesPerLine: true }],
'jsonc/quote-props': 'error',
'jsonc/quotes': 'error',
'jsonc/space-unary-ops': 'error',
'jsonc/valid-json-number': 'error',
'jsonc/vue-custom-block/no-parsing-error': 'error',

...stylistic
? {
'jsonc/array-bracket-spacing': ['error', 'never'],
'jsonc/comma-dangle': ['error', 'never'],
'jsonc/comma-style': ['error', 'last'],
'jsonc/indent': ['error', 2],
'jsonc/key-spacing': ['error', { afterColon: true, beforeColon: false }],
'jsonc/object-curly-newline': ['error', { consistent: true, multiline: true }],
'jsonc/object-curly-spacing': ['error', 'always'],
'jsonc/object-property-newline': ['error', { allowMultiplePropertiesPerLine: true }],
'jsonc/quote-props': 'error',
'jsonc/quotes': 'error',
}
: {},

...overrides,
},
},
]
Expand Down

0 comments on commit 30c870d

Please sign in to comment.