SwiftLint: исключить файл для определенного правила
Я пытаюсь сделать что-то подобное в моем файле .swiftlint.yml:
force_cast:
severity: warning # explicitly
excluded:
- Dog.swift
У меня есть этот код, и мне не нравится предупреждение force_try, которое я получаю для него:
let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
forIndexPath: indexPath) as! DogViewCell
Я хочу подавить предупреждение для этого файла, исключив этот файл из правила.
Есть ли способ сделать это?
Ответы
Ответ 1
Ну, если вы не хотите, чтобы к конкретному файлу применялись какие-то особые правила, вы можете использовать технику, упомянутую @Benno Kress. Для этого вам нужно добавить комментарий в ваш файл swift, как указано ниже.
Правила будут отключены до конца файла или до тех пор, пока линтер не увидит соответствующий комментарий включения:
// swiftlint:disable <rule1>
YOUR CODE WHERE NO rule1 is applied
// swiftlint:enable <rule1>
Также можно пропустить некоторые файлы, настроив swiftlint. Добавьте файл " .swiftlint.yml " в каталог, где вы будете запускать SwiftLint.
Добавьте следующее содержимое, чтобы исключить некоторые файлы. Допустим, файл1, файл2... и т.д.
excluded:
- file1
- file2
- folder1
- folder1/ExcludedFile.swift
Чтобы отключить некоторые правила, полностью добавьте следующее к тому же файлу " .swiftlint.yml ".
disabled_rules: # rule identifiers to exclude from running
- colon
- comma
- control_statement
Для получения дополнительной информации обратитесь по следующим ссылкам.
https://swifting.io/blog/2016/03/29/11-swiftlint/
https://github.com/realm/SwiftLint#disable-rules-in-code
Ответ 2
Я просто избавляюсь с помощью force_cast
Шаг 1:
cd path-to-your-project
Шаг 2:
touch .swiftlint.yml
Шаг 3: откройте .swiftlint.yml и добавьте
disabled_rules: # rule identifiers to exclude from running
- force_cast
![enter image description here]()
Ссылка - https://github.com/realm/SwiftLint#disable-rules-in-code
Ответ 3
Вы можете написать //swiftlint:disable force_cast
в начале файла, в котором вы хотите подавить предупреждение для этого правила. Он отключается до конца файла или до тех пор, пока вы не добавите строку //swiftlint:enable force_cast
.
Источник: https://github.com/realm/SwiftLint#disable-rules-in-code
Ответ 4
Настройте SwiftLint, добавив файл .swiftlint.yml из каталога, из которого вы будете запускать SwiftLint. Вот полный набор опций, которые вы можете использовать в файле .swiftlint.yaml
disabled_rules: # rule identifiers to exclude from running
- colon
- comma
- control_statement
opt_in_rules: # some rules are only opt-in
- empty_count
# Find all the available rules by running:
# swiftlint rules
included: # paths to include during linting. '--path' is ignored if present.
- Source
excluded: # paths to ignore during linting. Takes precedence over 'included'.
- Carthage
- Pods
- Source/ExcludedFolder
- Source/ExcludedFile.swift
- Source/*/ExcludedFile.swift # Exclude files with a wildcard
analyzer_rules: # Rules run by 'swiftlint analyze' (experimental)
- explicit_self
# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
force_try:
severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 110
# they can set both implicitly with an array
type_body_length:
- 300 # warning
- 400 # error
# or they can set both explicitly
file_length:
warning: 500
error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
min_length: 4 # only warning
max_length: # warning and error
warning: 40
error: 50
excluded: iPhone # excluded via string
identifier_name:
min_length: # only min_length
error: 4 # only error
excluded: # excluded via string array
- id
- URL
- GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)
Ссылка: github.com/realm/SwiftLint#disable-rules-in-code