Проблемы с ESLint / Prettier / Husky в приложении React-Native - PullRequest
0 голосов
/ 04 мая 2020

У меня проблема, когда я фиксирую, у меня есть Husky, который проверяет наличие ошибок отступа / обычных ошибок (например, реквизиты не используются .... et c). Мое приложение является приложением TypeScript React-Native.

Я получаю следующее:

  25:1   error  Expected indentation of 4 spaces but found 2      indent
  26:1   error  Expected indentation of 2 spaces but found 0      indent
  27:1   error  Expected indentation of 4 spaces but found 2      indent
  28:1   error  Expected indentation of 6 spaces but found 4      indent
  29:1   error  Expected indentation of 8 spaces but found 6      indent
  30:1   error  Expected indentation of 8 spaces but found 6      indent
  31:1   error  Expected indentation of 10 spaces but found 8     indent
  32:1   error  Expected indentation of 10 spaces but found 8     indent
  33:1   error  Expected indentation of 10 spaces but found 8     indent
  34:1   error  Expected indentation of 10 spaces but found 8     indent
  35:1   error  Expected indentation of 10 spaces but found 8     indent
  36:1   error  Expected indentation of 8 spaces but found 6      indent
  37:1   error  Expected indentation of 6 spaces but found 4      indent
  39:1   error  Expected indentation of 6 spaces but found 4      indent
  40:1   error  Expected indentation of 8 spaces but found 6      indent
  41:1   error  Expected indentation of 8 spaces but found 6      indent
  42:1   error  Expected indentation of 8 spaces but found 6      indent
  43:1   error  Expected indentation of 8 spaces but found 6      indent
  44:1   error  Expected indentation of 6 spaces but found 4      indent
  45:1   error  Expected indentation of 4 spaces but found 2      indent
  46:1   error  Expected indentation of 2 spaces but found 0      indent

Мой VSCode установлен на 2 пробела,

Мое eslint.rc правило равен "indent": ["error", 2], а мой prettier.rc установлен на "tabWidth": 2, Я не понимаю, почему он выдает ошибки, код отформатирован так, как и должно быть. Я даже побежал красивее command-shift-f на Ма c.

Есть идеи?

1 Ответ

1 голос
/ 04 мая 2020

Более красивая документация гласит, что

Какой бы инструмент для подкладки вы не использовали sh для интеграции, шаги в целом схожи. Сначала отключите все существующие правила форматирования в вашей подкладке, которые могут конфликтовать с тем, как Преттир хочет отформатировать ваш код. Затем вы можете либо добавить расширение к вашему инструменту для рисования, чтобы отформатировать файл с помощью Prettier - так что вам нужна только одна команда для форматирования файла, либо запустить linter, а затем Prettier как отдельные шаги.

В вашем случае, я бы предложил 1. Добавьте prettier в конец массива extends в eslintr c, чтобы отключить правила форматирования

{
  "extends": ["prettier"]
}

Затем вы можете комбинировать лайку с lint-staged , чтобы запускать хуки до фиксации для ваших подготовленных файлов. Например: в package.json определите husky

"husky": {"hooks": {"pre-commit": "lint-staged"}}

Создайте .lintstagedrc.js в root папке

module.exports = {
  '*.{js,jsx,ts,tsx}': ['eslint'],
  '*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)': ['prettier --write'],
};

Он запустит eslint для проверки ошибок linting, а затем отформатирует ваш код с более красивым.

...