Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add naming rules #1697

Merged
merged 4 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ if (node.isRoot()) {

### Added
* Wrap blocks in case the max line length is exceeded or in case the block contains a new line `wrapping` ([#1643](https://github.com/pinterest/ktlint/issue/1643))

* patterns can be read in from `stdin` with the `--patterns-from-stdin` command line options/flags ([#1606](https://github.com/pinterest/ktlint/pull/1606))
* Add basic formatting for context receiver in `indent` rule and new experimental rule `context-receiver-wrapping` ([#1672](https://github.com/pinterest/ktlint/issue/1672))
* Add naming rules for packages (`package-naming`), classes (`class-naming`), objects (`object-naming`), functions (`function-naming`) and properties (`property-naming`) ([#44](https://github.com/pinterest/ktlint/issue/44))

### Fixed

Expand Down
32 changes: 32 additions & 0 deletions docs/rules/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ Rewrites the function signature to a single line when possible (e.g. when not ex

Rule id: `function-signature`

## Naming

### Class naming

Enforce naming of class.

Rule id: `experimental:class-naming`

### Function naming

Enforce naming of function.

Rule id: `experimental:function-naming`

### Object naming

Enforce naming of object.

Rule id: `experimental:object-naming`

### Package naming

Enforce naming of package.

Rule id: `experimental:package-naming`

### Property naming

Enforce naming of property.

Rule id: `experimental:property-naming`

## Spacing

### Fun keyword spacing
Expand Down
24 changes: 12 additions & 12 deletions ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.pinterest.ktlint.core

import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.codeStyleSetProperty
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.CODE_STYLE_PROPERTY
import com.pinterest.ktlint.core.api.EditorConfigDefaults
import com.pinterest.ktlint.core.api.EditorConfigDefaults.Companion.emptyEditorConfigDefaults
import com.pinterest.ktlint.core.api.EditorConfigDefaults.Companion.EMPTY_EDITOR_CONFIG_DEFAULTS
import com.pinterest.ktlint.core.api.EditorConfigOverride
import com.pinterest.ktlint.core.api.EditorConfigOverride.Companion.emptyEditorConfigOverride
import com.pinterest.ktlint.core.api.EditorConfigOverride.Companion.EMPTY_EDITOR_CONFIG_OVERRIDE
import com.pinterest.ktlint.core.api.EditorConfigProperties
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import com.pinterest.ktlint.core.internal.EditorConfigFinder
import com.pinterest.ktlint.core.internal.EditorConfigGenerator
import com.pinterest.ktlint.core.internal.EditorConfigLoader
import com.pinterest.ktlint.core.internal.RuleExecutionContext
import com.pinterest.ktlint.core.internal.SuppressHandler
import com.pinterest.ktlint.core.internal.ThreadSafeEditorConfigCache.Companion.threadSafeEditorConfigCache
import com.pinterest.ktlint.core.internal.ThreadSafeEditorConfigCache.Companion.THREAD_SAFER_EDITOR_CONFIG_CACHE
import com.pinterest.ktlint.core.internal.VisitorProvider
import com.pinterest.ktlint.core.internal.createRuleExecutionContext
import com.pinterest.ktlint.core.internal.toQualifiedRuleId
Expand Down Expand Up @@ -47,7 +47,7 @@ public object KtLint {
internal const val UTF8_BOM = "\uFEFF"
public const val STDIN_FILE: String = "<stdin>"

internal val editorConfigLoader = EditorConfigLoader(FileSystems.getDefault())
internal val EDITOR_CONFIG_LOADER = EditorConfigLoader(FileSystems.getDefault())

/**
* Parameters to invoke [KtLint.lint] and [KtLint.format] API's.
Expand Down Expand Up @@ -94,8 +94,8 @@ public object KtLint {
@Deprecated("Marked for removal in KtLint 0.48. Use 'editorConfigDefaults' to specify default property values")
val editorConfigPath: String? = null,
val debug: Boolean = false,
val editorConfigDefaults: EditorConfigDefaults = emptyEditorConfigDefaults,
val editorConfigOverride: EditorConfigOverride = emptyEditorConfigOverride,
val editorConfigDefaults: EditorConfigDefaults = EMPTY_EDITOR_CONFIG_DEFAULTS,
val editorConfigOverride: EditorConfigOverride = EMPTY_EDITOR_CONFIG_OVERRIDE,
val isInvokedFromCli: Boolean = false,
) {
internal val ruleRunners: Set<RuleRunner> =
Expand Down Expand Up @@ -337,7 +337,7 @@ public object KtLint {
* Reduce memory usage by cleaning internal caches.
*/
public fun trimMemory() {
threadSafeEditorConfigCache.clear()
THREAD_SAFER_EDITOR_CONFIG_CACHE.clear()
}

/**
Expand All @@ -357,7 +357,7 @@ public object KtLint {
* '.editorconfig' files which need to be observed.
*/
public fun reloadEditorConfigFile(path: Path) {
threadSafeEditorConfigCache.reloadIfExists(
THREAD_SAFER_EDITOR_CONFIG_CACHE.reloadIfExists(
Resource.Resources.ofPath(path, StandardCharsets.UTF_8),
)
}
Expand Down Expand Up @@ -387,11 +387,11 @@ public object KtLint {
val codeStyle =
params
.editorConfigOverride
.properties[codeStyleSetProperty]
.properties[CODE_STYLE_PROPERTY]
?.parsed
?.safeAs<DefaultEditorConfigProperties.CodeStyleValue>()
?: codeStyleSetProperty.defaultValue
return EditorConfigGenerator(editorConfigLoader).generateEditorconfig(
?: CODE_STYLE_PROPERTY.defaultValue
return EditorConfigGenerator(EDITOR_CONFIG_LOADER).generateEditorconfig(
filePath,
params.getRules(),
params.debug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import mu.KotlinLogging
import org.w3c.dom.Element
import org.xml.sax.SAXException

private val logger = KotlinLogging.logger {}.initKtLintKLogger()
private val LOGGER = KotlinLogging.logger {}.initKtLintKLogger()

/**
* Baseline of lint errors to be ignored in subsequent calls to ktlint.
Expand Down Expand Up @@ -64,18 +64,18 @@ public fun loadBaseline(path: String): Baseline {
status = VALID,
)
} catch (e: IOException) {
logger.error { "Unable to parse baseline file: $path" }
LOGGER.error { "Unable to parse baseline file: $path" }
} catch (e: ParserConfigurationException) {
logger.error { "Unable to parse baseline file: $path" }
LOGGER.error { "Unable to parse baseline file: $path" }
} catch (e: SAXException) {
logger.error { "Unable to parse baseline file: $path" }
LOGGER.error { "Unable to parse baseline file: $path" }
}

// Baseline can not be parsed.
try {
baselineFile.delete()
} catch (e: IOException) {
logger.error { "Unable to delete baseline file: $path" }
LOGGER.error { "Unable to delete baseline file: $path" }
}
return Baseline(status = INVALID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@ import org.ec4j.core.model.EditorConfig
*/
public data class EditorConfigDefaults(public val value: EditorConfig) {
public companion object {
private val editorConfigDefaultsLoader = EditorConfigDefaultsLoader()
private val EDITOR_CONFIG_DEFAULTS_LOADER = EditorConfigDefaultsLoader()

/**
* Loads properties from [path]. [path] may either locate a file (also allows specifying a file with a name other
* than ".editorconfig") or a directory in which a file with name ".editorconfig" is expected to exist. Properties
* from all globs are returned.
*
* If [path] is not valid then the [emptyEditorConfigDefaults] is returned.
* If [path] is not valid then the [EMPTY_EDITOR_CONFIG_DEFAULTS] is returned.
*
* The property "root" which denotes whether the parent directory is to be checked for the existence of a fallback
* ".editorconfig" is ignored entirely.
*/
public fun load(path: Path?): EditorConfigDefaults =
if (path == null) {
emptyEditorConfigDefaults
EMPTY_EDITOR_CONFIG_DEFAULTS
} else {
editorConfigDefaultsLoader.load(path)
EDITOR_CONFIG_DEFAULTS_LOADER.load(path)
}

/**
* Empty representation of [EditorConfigDefaults].
*/
public val emptyEditorConfigDefaults: EditorConfigDefaults = EditorConfigDefaults(EditorConfig.builder().build())
public val EMPTY_EDITOR_CONFIG_DEFAULTS: EditorConfigDefaults = EditorConfigDefaults(EditorConfig.builder().build())

@Deprecated(
message = "Marked for removal in KtLint 0.49",
replaceWith = ReplaceWith("EMPTY_EDITOR_CONFIG_DEFAULTS"),
)
@Suppress("ktlint:experimental:property-naming")
public val emptyEditorConfigDefaults: EditorConfigDefaults = EMPTY_EDITOR_CONFIG_DEFAULTS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public class EditorConfigOverride {
* Get the empty [EditorConfigOverride]. As it does not contain any properties, all properties fall back on
* their respective default values.
*/
public val emptyEditorConfigOverride: EditorConfigOverride = EditorConfigOverride()
public val EMPTY_EDITOR_CONFIG_OVERRIDE: EditorConfigOverride = EditorConfigOverride()

@Deprecated(
message = "Marked for removal in KtLint 0.49",
replaceWith = ReplaceWith("EMPTY_EDITOR_CONFIG_OVERRIDE"),
)
@Suppress("ktlint:experimental:property-naming")
public val emptyEditorConfigOverride: EditorConfigOverride = EMPTY_EDITOR_CONFIG_OVERRIDE
}
}
Loading