Skip to content

Commit

Permalink
fix: formatter config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pnodet committed Sep 3, 2024
1 parent 81e138e commit 60742f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 66 deletions.
93 changes: 32 additions & 61 deletions src/configs/format.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,8 @@
import pluginFormat from 'eslint-plugin-format';
import { GLOB_SRC, GLOB_SRC_JS, GLOB_SRC_TS } from '../globs';
import { GLOB_SRC } from '../globs';
import type { FormatConfigOptions } from '../options';

export const format = (options?: FormatConfigOptions) => {
if (options === false) {
return [];
}

let config = {
arrowParens: 'avoid',
bracketSameLine: false,
bracketSpacing: true,
endOfLine: 'lf',
htmlWhitespaceSensitivity: 'css',
jsxSingleQuote: true,
overrides: [
{
files: ['**/*.json'],
options: { trailingComma: 'none', useTabs: false },
},
{
files: ['**/*.yml', '**/*.yaml'],
options: { singleQuote: false, useTabs: false },
},
],
printWidth: 80,
proseWrap: 'always',
quoteProps: 'preserve',
requirePragma: false,
semi: true,
singleAttributePerLine: false,
singleQuote: true,
tabWidth: 2,
trailingComma: 'all',
useTabs: false,
...options?.prettierOptions,
};

const dprint = options?.dprint ?? false;
const formatter: 'dprint' | 'prettier' = dprint ? 'dprint' : 'prettier';

if (options?.dprint) {
config = {
indentWidth: 2,
quoteStyle: 'preferSingle',
useTabs: false,
...options.dprintOptions,
};
}

const eslintConfigs: any = [
{
name: 'nivalis/formatter/setup',
Expand Down Expand Up @@ -207,24 +161,41 @@ export const format = (options?: FormatConfigOptions) => {
},
];

if (formatter === 'dprint') {
eslintConfigs.push(
if (options === false) {
return eslintConfigs;
}

const config = {
arrowParens: 'avoid',
bracketSameLine: false,
bracketSpacing: true,
endOfLine: 'lf',
htmlWhitespaceSensitivity: 'css',
jsxSingleQuote: true,
overrides: [
{
files: [GLOB_SRC_JS],
name: 'nivalis/formatter/drpint/js',
rules: {
'format/dprint': ['error', { ...config, language: 'javascript' }],
},
files: ['**/*.json'],
options: { trailingComma: 'none', useTabs: false },
},
{
files: [GLOB_SRC_TS],
name: 'nivalis/formatter/dprint/ts',
rules: {
'format/dprint': ['error', { ...config, language: 'typescript' }],
},
files: ['**/*.yml', '**/*.yaml'],
options: { singleQuote: false, useTabs: false },
},
);
} else {
],
printWidth: 80,
proseWrap: 'always',
quoteProps: 'preserve',
requirePragma: false,
semi: true,
singleAttributePerLine: false,
singleQuote: true,
tabWidth: 2,
trailingComma: 'all',
useTabs: false,
...options?.options,
};

if (options?.formatter === 'prettier') {
eslintConfigs.push({
files: [GLOB_SRC],
name: 'nivalis/formatter/prettier',
Expand Down
8 changes: 3 additions & 5 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { node } from './configs/node';
import { regexp } from './configs/regexp';
import { stylistic } from './configs/stylistic';
import { unicorn } from './configs/unicorn';
import { format } from './configs/format';
import { HAS_NEXTJS, HAS_TAILWINDCSS, HAS_TYPESCRIPT } from './environment';
import type { ConfigOptions } from './options';

Expand Down Expand Up @@ -51,11 +52,8 @@ export const nivalis = async (options?: ConfigOptions) => {
composer = composer.append(...ts.typescript(options?.typescript));
}

if (options?.format !== false && options?.format?.formatter === 'prettier') {
const fmt = await import('./configs/format');

composer = composer.append(...fmt.format(options?.format));
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
composer = composer.append(...format(options?.format));

if (options?.format !== false && options?.format?.formatter === 'dprint') {
composer = composer.append(nivalisCustom());
Expand Down

0 comments on commit 60742f1

Please sign in to comment.