Skip to content

Commit

Permalink
Fix invalid property usage for const properties and seperate constant…
Browse files Browse the repository at this point in the history
…s from normal properties (#61)

* Split the file constant into an own spec structure to reduce complexity

* Add test class for the FileConstWriter

* Integrate FileConstantSpec to replace the PropertySpec usage

* Add test for the new structure

* Improve property tests

* Update file const write

* Improve property writing

* Add emitFileConst method

* Update ALLOWED_CONST_MODIFIERS content

* Improve allowed modifiers check

* Replace check with require

* Update validation checks and dedicated method for a property as constant

* Mark type as nullable

* Deprecate ConstClassName

* Update type check

* Improve property tests

* Update content from the ALLOWED_CLASS_CONST_MODIFIERS constant

* Update to the new spec structure for constant values

* Update method call to write the constants

* Rename constant writer

* Update constant property access in the test cases

* Remove old place for the constant spec structure

* Change name of the constant property objects

* Fix failing tests
  • Loading branch information
theEvilReaper authored Oct 9, 2023
1 parent 3cd44d9 commit 7d2b865
Show file tree
Hide file tree
Showing 22 changed files with 591 additions and 72 deletions.
10 changes: 2 additions & 8 deletions src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import net.theevilreaper.dartpoet.code.writer.DartFileWriter
import net.theevilreaper.dartpoet.extension.ExtensionSpec
import net.theevilreaper.dartpoet.function.FunctionSpec
import net.theevilreaper.dartpoet.util.*
import net.theevilreaper.dartpoet.util.ALLOWED_CONST_MODIFIERS
import net.theevilreaper.dartpoet.directive.DartDirective
import net.theevilreaper.dartpoet.directive.LibraryDirective
import net.theevilreaper.dartpoet.directive.PartDirective
import net.theevilreaper.dartpoet.property.PropertySpec
import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.util.DART_FILE_ENDING
import net.theevilreaper.dartpoet.util.isDartConventionFileName
import net.theevilreaper.dartpoet.util.toImmutableList
Expand All @@ -32,12 +31,7 @@ class DartFile internal constructor(
internal val extensions: List<ExtensionSpec> = builder.extensionStack
internal val docs = builder.docs
private val directives = builder.directives.toImmutableList()
internal val constants: Set<PropertySpec> = builder.constants.onEach {
// Only check modifiers when the size is not zero
if (it.modifiers.isNotEmpty()) {
hasAllowedModifiers(it.modifiers, ALLOWED_CONST_MODIFIERS, "file const")
}
}.toImmutableSet()
internal val constants: Set<ConstantPropertySpec> = builder.constants.toImmutableSet()

internal val imports: List<DartDirective> = if (directives.isEmpty()) {
emptyList()
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/net/theevilreaper/dartpoet/DartFileBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.theevilreaper.dartpoet.clazz.ClassSpec
import net.theevilreaper.dartpoet.code.CodeBlock
import net.theevilreaper.dartpoet.extension.ExtensionSpec
import net.theevilreaper.dartpoet.directive.Directive
import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.property.PropertySpec
import net.theevilreaper.dartpoet.util.DEFAULT_INDENT
import net.theevilreaper.dartpoet.util.isIndent
Expand All @@ -18,18 +19,18 @@ class DartFileBuilder(
internal val directives: MutableList<Directive> = mutableListOf()
internal val annotations: MutableList<AnnotationSpec> = mutableListOf()
internal val extensionStack: MutableList<ExtensionSpec> = mutableListOf()
internal val constants: MutableSet<PropertySpec> = mutableSetOf()
internal val constants: MutableSet<ConstantPropertySpec> = mutableSetOf()
internal var indent = DEFAULT_INDENT

/**
* Add a constant [PropertySpec] to the file.
* @param constant the property to add
*/
fun constant(constant: PropertySpec) = apply {
fun constant(constant: ConstantPropertySpec) = apply {
this.constants += constant
}

fun constants(vararg constants: PropertySpec) = apply {
fun constants(vararg constants: ConstantPropertySpec) = apply {
this.constants += constants
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.theevilreaper.dartpoet.meta.SpecData
import net.theevilreaper.dartpoet.meta.SpecMethods
import net.theevilreaper.dartpoet.function.constructor.ConstructorSpec
import net.theevilreaper.dartpoet.property.PropertySpec
import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.type.TypeName
import net.theevilreaper.dartpoet.type.asTypeName
import java.lang.reflect.Type
Expand All @@ -30,7 +31,7 @@ class ClassBuilder internal constructor(
internal val propertyStack: MutableList<PropertySpec> = mutableListOf()
internal val functionStack: MutableList<FunctionSpec> = mutableListOf()
internal val enumPropertyStack: MutableList<EnumPropertySpec> = mutableListOf()
internal val constantStack: MutableSet<PropertySpec> = mutableSetOf()
internal val constantStack: MutableSet<ConstantPropertySpec> = mutableSetOf()
internal var superClass: TypeName? = null
internal var inheritKeyWord: InheritKeyword? = null
internal var endWithNewLine = false
Expand All @@ -40,15 +41,15 @@ class ClassBuilder internal constructor(
* Add a constant [PropertySpec] to the file.
* @param constant the property to add
*/
fun constant(constant: PropertySpec) = apply {
fun constant(constant: ConstantPropertySpec) = apply {
this.constantStack += constant
}

/**
* Add an array of constant [PropertySpec] to the file.
* @param constants the array to add
*/
fun constants(vararg constants: PropertySpec) = apply {
fun constants(vararg constants: ConstantPropertySpec) = apply {
this.constantStack += constants
}

Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/net/theevilreaper/dartpoet/clazz/ClassSpec.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.theevilreaper.dartpoet.clazz

import net.theevilreaper.dartpoet.DartModifier
import net.theevilreaper.dartpoet.DartModifier.*
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.ClassWriter
import net.theevilreaper.dartpoet.util.*
import net.theevilreaper.dartpoet.util.toImmutableList
import net.theevilreaper.dartpoet.util.toImmutableSet

Expand Down Expand Up @@ -38,10 +36,7 @@ class ClassSpec internal constructor(
internal val constructors = builder.constructorStack.toImmutableSet()
internal val typeDefStack = builder.functionStack.filter { it.isTypeDef }.toImmutableSet()
internal val enumPropertyStack = builder.enumPropertyStack.toImmutableList()
internal val constantStack = builder.constantStack.onEach {
hasAllowedModifiers(it.modifiers, ALLOWED_CLASS_CONST_MODIFIERS, "class constants")
it.modifiers = it.modifiers.sorted().toSet()
}.toImmutableSet()
internal var constantStack = builder.constantStack.toImmutableSet()

/**
* Returns true when the class has no content to generate.
Expand All @@ -67,7 +62,9 @@ class ClassSpec internal constructor(
* Calls the [ClassWriter] to write the data from the spec into code for dart
* @param codeWriter the [CodeWriter] instance to apply the data
*/
internal fun write(codeWriter: CodeWriter) { ClassWriter().write(this, codeWriter) }
internal fun write(codeWriter: CodeWriter) {
ClassWriter().write(this, codeWriter)
}

/**
* Returns a [String] representation from the class spec.
Expand All @@ -85,7 +82,7 @@ class ClassSpec internal constructor(
* @return the created instance
*/
@JvmStatic
fun builder(name: String) = ClassBuilder(name, ClassType.CLASS, DartModifier.CLASS)
fun builder(name: String) = ClassBuilder(name, ClassType.CLASS, CLASS)

/**
* Create a new [ClassBuilder] instance for an anonymous dart class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import net.theevilreaper.dartpoet.extension.ExtensionSpec
import net.theevilreaper.dartpoet.function.FunctionSpec
import net.theevilreaper.dartpoet.function.constructor.ConstructorSpec
import net.theevilreaper.dartpoet.directive.Directive
import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.parameter.ParameterSpec
import net.theevilreaper.dartpoet.property.PropertySpec
import net.theevilreaper.dartpoet.util.CURLY_CLOSE
Expand Down Expand Up @@ -180,6 +181,23 @@ internal fun <T: Directive> List<T>.writeImports(
}
}

fun Set<ConstantPropertySpec>.emitConstants(
codeWriter: CodeWriter,
emitBlock: (ConstantPropertySpec) -> Unit = { it.write(codeWriter) }
) = with(codeWriter) {
if (isNotEmpty()) {
val emitNewLines = size > 1

forEachIndexed { index, property ->
if (index > 0) {
emit(if (emitNewLines) NEW_LINE else EMPTY_STRING)
}
emitBlock(property)
}
emit(NEW_LINE)
}
}

fun Set<PropertySpec>.emitProperties(
codeWriter: CodeWriter,
forceNewLines: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ClassWriter {
codeWriter.emit(NEW_LINE)
}

spec.constantStack.emitProperties(codeWriter)
spec.constantStack.emitConstants(codeWriter)

if (spec.constantStack.isNotEmpty()) {
codeWriter.emit(NEW_LINE)
Expand Down Expand Up @@ -137,11 +137,13 @@ class ClassWriter {
writer.emit(spec.name!!)
}
}

ClassType.ABSTRACT -> {
writer.emit("${type.keyword}·${ClassType.CLASS.keyword}·")
writer.emit(if (spec.modifiers.contains(PRIVATE)) PRIVATE.identifier else EMPTY_STRING)
writer.emit(spec.name!!)
}

else -> {
//TODO: Check if a library class needs a header
// A library class doesn't have any class header
Expand All @@ -160,7 +162,7 @@ class ClassWriter {

private fun List<EnumPropertySpec>.emit(
codeWriter: CodeWriter,
emitBlock: (EnumPropertySpec) -> Unit = { it.write(codeWriter) }
emitBlock: (EnumPropertySpec) -> Unit = { it.write(codeWriter) }
) = with(codeWriter) {
if (isNotEmpty()) {
forEachIndexed { index, enumPropertySpec ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.theevilreaper.dartpoet.code.writer

import net.theevilreaper.dartpoet.DartModifier.*
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec
import net.theevilreaper.dartpoet.util.SEMICOLON
import net.theevilreaper.dartpoet.util.SPACE

internal class ConstantPropertyWriter {

fun emit(spec: ConstantPropertySpec, codeWriter: CodeWriter) {
val modifiersAsString = spec.modifiers.joinToString(separator = SPACE, postfix = SPACE) { it.identifier }

codeWriter.emit(modifiersAsString)

if (spec.typeName != null) {
codeWriter.emitCode("%T·", spec.typeName)
}

if (spec.isPrivat) {
codeWriter.emit(PRIVATE.identifier)
}

codeWriter.emitCode("%L", spec.name)

codeWriter.emit("·=·")
codeWriter.emitCode(spec.initializer.build(), isConstantContext = true)
codeWriter.emit(SEMICOLON)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.theevilreaper.dartpoet.code.writer
import net.theevilreaper.dartpoet.DartFile
import net.theevilreaper.dartpoet.code.CodeWriter
import net.theevilreaper.dartpoet.code.emitExtensions
import net.theevilreaper.dartpoet.code.emitProperties
import net.theevilreaper.dartpoet.code.emitConstants
import net.theevilreaper.dartpoet.code.writeImports
import net.theevilreaper.dartpoet.util.NEW_LINE

Expand Down Expand Up @@ -40,7 +40,7 @@ class DartFileWriter {
writer.emit(NEW_LINE)
}

dartFile.constants.emitProperties(writer) {
dartFile.constants.emitConstants(writer) {
it.write(writer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,26 @@ import net.theevilreaper.dartpoet.util.EMPTY_STRING
import net.theevilreaper.dartpoet.util.SEMICOLON
import net.theevilreaper.dartpoet.util.SPACE

class PropertyWriter {
internal class PropertyWriter {

fun write(property: PropertySpec, writer: CodeWriter) {
if (property.hasDocs) {

property.docs.forEach { writer.emitDoc(it) }
}
property.annotations.emitAnnotations(writer) {
it.write(writer, inline = false)
}

val modifierString = property.modifiers.joinToString(separator = SPACE) { it.identifier }
val modifierString = property.modifiers.joinToString(
separator = SPACE,
postfix = if (property.modifiers.isNotEmpty()) SPACE else EMPTY_STRING
) { it.identifier }
writer.emit(modifierString)

if (!property.isConst) {
if (property.modifiers.isNotEmpty()) {
writer.emit(SPACE)
}
writer.emitCode("%T", property.type)
if (property.type != null) {
writer.emitCode("%T·", property.type)
}

writer.emit("·")

writer.emit(if (property.isPrivate) DartModifier.PRIVATE.identifier else EMPTY_STRING)
writer.emit(property.name)
emitInitBlock(property.initBlock, writer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import net.theevilreaper.dartpoet.type.TypeName
**/
class PropertyBuilder internal constructor(
var name: String,
var type: TypeName,
var type: TypeName? = null,
) {
internal val modifiers: MutableSet<DartModifier> = mutableSetOf()
internal val annotations: MutableList<AnnotationSpec> = mutableListOf()
Expand Down
Loading

0 comments on commit 7d2b865

Please sign in to comment.