Skip to content

Commit

Permalink
First release under the new namespace 'com.hanggrian'
Browse files Browse the repository at this point in the history
  • Loading branch information
hanggrian committed Jul 30, 2024
1 parent d7fef6b commit 7545025
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 28 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[![CircleCI](https://img.shields.io/circleci/build/gh/hanggrian/rulebook)](https://app.circleci.com/pipelines/github/hanggrian/rulebook/)
[![Codecov](https://img.shields.io/codecov/c/gh/hanggrian/rulebook)](https://app.codecov.io/gh/hanggrian/rulebook/) \
[![Maven Central](https://img.shields.io/maven-central/v/com.hanggrian.rulebook/rulebook-ktlint)](https://repo1.maven.org/maven2/com/hanggrian/rulebook/rulebook-ktlint/)
[![OpenJDK](https://img.shields.io/badge/jdk-11%2B-informational)](https://openjdk.java.net/projects/jdk8/) \
[![OpenJDK](https://img.shields.io/badge/jdk-11%2B-informational)](https://openjdk.org/projects/jdk/11/) \
[![Package Index](https://shields.io/pypi/v/rulebook-pylint)](https://pypi.org/project/rulebook-pylint/)
[![Package Index Test](https://shields.io/pypi/v/rulebook-pylint?label=testpypi&pypiBaseUrl=https://test.pypi.org)](https://test.pypi.org/project/rulebook-pylint/)
[![Python](https://img.shields.io/badge/python-3-informational)](https://www.python.org/download/releases/3.0/)
[![Python](https://img.shields.io/badge/python-3-informational)](https://docs.python.org/3/)

# Rulebook

Expand All @@ -15,7 +15,7 @@ across multiple languages.

Language | Linter | Variants
--- | --- | ---
Kotlin | [Ktlint](https://pinterest.github.io/ktlint/) | [Ktlint Official Style](https://pinterest.github.io/ktlint/1.0.1/rules/code-styles/)
Kotlin | [Ktlint](https://pinterest.github.io/ktlint/) | [Ktlint Style](https://pinterest.github.io/ktlint/1.0.1/rules/code-styles/)
Java | [Checkstyle](https://checkstyle.org/) | [Sun Style](https://checkstyle.sourceforge.io/sun_style.html) or [Google Java Style](https://google.github.io/styleguide/javaguide.html)
Groovy | [CodeNarc](https://codenarc.org/) | [Groovy Style](https://groovy-lang.org/style-guide.html)
Python | [Pylint](https://pylint.org/) | [Pylint Style](https://pylint.pycqa.org/en/latest/user_guide/configuration/all-options.html) or [Google Python Style](https://google.github.io/styleguide/pyguide.html)
Expand All @@ -32,9 +32,9 @@ repositories {
}
dependencies {
ktlint "com.hendraanggrian.rulebook:rulebook-ktlint:$version"
checkstyle "com.hendraanggrian.rulebook:rulebook-checkstyle:$version"
codenarc "com.hendraanggrian.rulebook:rulebook-codenarc:$version"
ktlint "com.hanggrian.rulebook:rulebook-ktlint:$version"
checkstyle "com.hanggrian.rulebook:rulebook-checkstyle:$version"
codenarc "com.hanggrian.rulebook:rulebook-codenarc:$version"
}
```

Expand All @@ -58,7 +58,7 @@ configurations {
}
dependencies {
ktlint "com.hendraanggrian.rulebook:rulebook-ktlint:$libraryVersion"
ktlint "com.hanggrian.rulebook:rulebook-ktlint:$libraryVersion"
}
// the rest of ktlint tasks' configuration
Expand All @@ -81,7 +81,7 @@ checkstyle {
}
dependencies {
checkstyle "com.hendraanggrian.rulebook:rulebook-checkstyle:$libraryVersion"
checkstyle "com.hanggrian.rulebook:rulebook-checkstyle:$libraryVersion"
}
```

Expand All @@ -102,7 +102,7 @@ codenarc {
}
dependencies {
codenarc "com.hendraanggrian.rulebook:rulebook-codenarc:$libraryVersion"
codenarc "com.hanggrian.rulebook:rulebook-codenarc:$libraryVersion"
}
```

Expand Down
2 changes: 2 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# - Modify 'disable':
# - Add 'missing-module-docstring'.
# - Add 'missing-class-docstring'.
# - Add 'missing-type-doc'.

[MAIN]

Expand Down Expand Up @@ -147,6 +148,7 @@ disable=
# custom
missing-module-docstring,
missing-class-docstring,
missing-type-doc

[REPORTS]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "rulebook-pylint"
version = "0.1-dev1"
version = "0.1"
license = { text = "Apache-2.0" }
description = "Extended lint rules for JVM and Python"
readme = "README.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public class IfElseFlatteningRule : Rule("if-else-flattening") {
if (`else` != null) {
`else`
.takeUnless { IF in it }
?.takeIf { it.isMultiline() }
?.takeIf { it.hasMultipleLines() }
?: return
emit(`if`.findChildByType(ELSE_KEYWORD)!!.startOffset, Messages[MSG_LIFT], false)
return
}
`if`
.takeIf { it.hasMultipleLines() }
.findChildByType(THEN)
?.takeIf { it.hasMultipleLines() }
?: return
emit(`if`.startOffset, Messages[MSG_INVERT], false)
}
Expand All @@ -65,8 +66,7 @@ public class IfElseFlatteningRule : Rule("if-else-flattening") {
const val MSG_LIFT = "if.else.flattening.lift"

private fun ASTNode.hasMultipleLines() =
findChildByType(THEN)!!
.findChildByType(BLOCK)
findChildByType(BLOCK)
?.children()
?.filter {
it.elementType != LBRACE && it.elementType != RBRACE && !it.isWhiteSpace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CodeBlockLineTrimmingChecker(TokenChecker):
def process_tokens(self, tokens: list[TokenInfo]) -> None:
token: TokenInfo
for i, token in enumerate(tokens):
# first line of filter
# target colon operator
if token.type != OP or token.string != ':':
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def process_tokens(self, tokens: list[TokenInfo]) -> None:
last_empty_token: TokenInfo | None = None
token: TokenInfo
for token in tokens:
# first line of filter
# target comment
if token.type != COMMENT:
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CommentLineTrimmingChecker(TokenChecker):
def process_tokens(self, tokens: list[TokenInfo]) -> None:
token: TokenInfo
for i, token in enumerate(tokens):
# first line of filter
# target comment
if token.type != COMMENT:
continue

Expand Down
10 changes: 4 additions & 6 deletions rulebook-pylint/rulebook_pylint/if_else_flattening_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ def _process(self, body: list[NodeNG]) -> None:
# checks for violation
else2: list[NodeNG] = if2.orelse

print("AAA")
for e in else2:
print(e)

if len(else2) > 0:
else_first_child: NodeNG = else2[0]
if self._else_has_if(else2):
Expand All @@ -58,13 +54,15 @@ def _process(self, body: list[NodeNG]) -> None:
if self._has_multiple_lines(if2.body):
self.add_message(self.MSG_INVERT, node=if2)

def _else_has_if(self, nodes: list[NodeNG]) -> bool:
@staticmethod
def _else_has_if(nodes: list[NodeNG]) -> bool:
for node in nodes:
if isinstance(node, If):
return True
return False

def _has_multiple_lines(self, nodes: list[NodeNG]) -> bool:
@staticmethod
def _has_multiple_lines(nodes: list[NodeNG]) -> bool:
length: int = len(nodes)
if length == 1:
return is_multiline(nodes[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class InnerClassPositionChecker(Checker):
"""See wiki: https://github.com/hanggrian/rulebook/wiki/Rules/#inner-class-organization
"""See wiki: https://github.com/hanggrian/rulebook/wiki/Rules/#inner-class-position
"""
MSG: str = 'inner-class-position'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SpecialFunctionPositionChecker(Checker):
msgs: dict[str, MessageDefinitionTuple] = Messages.of(MSG)

def visit_functiondef(self, node: FunctionDef) -> None:
# first line of filter
# target special function
if node.name not in self.SPECIAL_FUNCTIONS:
return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class StringSingleQuotingChecker(TokenChecker):
def process_tokens(self, tokens: list[TokenInfo]) -> None:
token: TokenInfo
for token in tokens:
# first line of filter
# target string
if token.type != STRING:
continue

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tokenize import TokenInfo, OP, NL
from tokenize import TokenInfo, OP, NL, COMMENT
from typing import TYPE_CHECKING

from pylint.typing import MessageDefinitionTuple
Expand All @@ -18,9 +18,12 @@ class TrailingCommaInsertionChecker(TokenChecker):
msgs: dict[str, MessageDefinitionTuple] = Messages.of(MSG)

def process_tokens(self, tokens: list[TokenInfo]) -> None:
# filter out comments
tokens = [t for t in tokens if t.type != COMMENT]

token: TokenInfo
for i, token in enumerate(tokens):
# first line of filter
# find closing parenthesis
if token.type != OP or token.string != ')':
continue

Expand Down
17 changes: 17 additions & 0 deletions rulebook-pylint/tests/test_trailing_comma_insertion_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ def foo(
):
self.checker.process_tokens(tokens)

def test_skip_inline_comment(self):
tokens = \
_tokenize_str(
'''
def foo(
bar: int, # bar
baz: int, # baz
):
print(
1, # 1
2, # 2
)
''',
)
with self.assertNoMessages():
self.checker.process_tokens(tokens)


if __name__ == '__main__':
main()

0 comments on commit 7545025

Please # to comment.