Skip to content

Commit

Permalink
separate check flags for duplication and optimization (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenyou authored Jun 8, 2024
1 parent e1d81a7 commit 42d72fb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-hotels-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"scalawind": patch
---

separate check flags for duplication and optimization
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,18 @@ val styles = "text-black bg-white hover:text-white hover:bg-black"

## Classes Validation

Passing the flag `-cv` or `--classes-validation` to enable this feature.

### Check Duplication

Passing the flag `-cd` or `--check-duplication` to enable this feature.

When writing a long list of utility classes, it's sometime necessary to check if we accidentally duplicate our class, with class validation feature enabled, we check it for you:

<img width="767" alt="Screenshot 2024-06-08 at 13 41 44" src="https://github.com/nguyenyou/scalawind/assets/38455472/d20d635a-3496-4f3d-a8bf-56c66eb1cd5d">
### Usage Optimization
Passing the flag `-co` or `--check-optimization` to enable this feature.
In Tailwind, we have `margin` and `padding` classes that can be used in three different fashions:
- One-direction: `mt-2`, `mb-2`, `ml-2` and `mr-2`
Expand Down
2 changes: 1 addition & 1 deletion integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "integration-test",
"type": "module",
"scripts": {
"generate": "scalawind generate --classes-validation -pcr -o ./src/scalawind.scala",
"generate": "scalawind generate --check-duplication --check-optimization -pcr -o ./src/scalawind.scala",
"test:scala": "scala-cli test src --test-only 'tests.only*'"
},
"dependencies": {
Expand Down
12 changes: 9 additions & 3 deletions packages/scalawind/src/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const initOptionsSchema = z.object({
previewCompliedResult: z.boolean(),
laminar: z.boolean(),
scalajsReact: z.boolean(),
classesValidation: z.boolean()
checkDuplication: z.boolean(),
checkOptimization: z.boolean()
})

export const generate = new Command()
Expand All @@ -38,8 +39,13 @@ export const generate = new Command()
false
)
.option(
"-cv, --classes-validation",
"enable classes validation",
"-cd, --check-duplication",
"enable check duplication",
false
)
.option(
"-co, --check-optimization",
"enable check optimization",
false
)
.option(
Expand Down
3 changes: 3 additions & 0 deletions packages/scalawind/src/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@ export function generateContent(options) {
return ({ name: mod, value: mod})
})

const hasValidation = options.checkDuplication || options.checkOptimization

const generatedScalawind = template({
modifiers,
standard,
arbitrary,
opacityColors,
hasValidation,
...options,
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{{#if classesValidation}}
def validate(classes: List[String]): Unit = {
{{#if checkDuplication}}
checkDuplication(classes)
{{/if}}
{{#if checkOptimization}}
val optimizationSuggestions = checkOptimization(classes)
if optimizationSuggestions.nonEmpty then
report.errorAndAbort(s"[Optimization] ${optimizationSuggestions.mkString(", ")}")
{{/if}}
}

{{#if checkDuplication}}
def checkDuplication(classes: List[String]): Unit = {
val duplicates = classes.groupBy(identity).collect { case (x, List(_, _, _*)) => x }
if duplicates.nonEmpty then
report.errorAndAbort(s"[Duplication] ${duplicates.mkString(", ")}")
}
{{/if}}

{{#if checkOptimization}}
def checkOptimization(classes: List[String]): List[String] = {
val properties = List("p", "m")

Expand Down
4 changes: 3 additions & 1 deletion packages/scalawind/src/generate/templates/swMacro.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def methodNameToTailwindClass(rawName: String) = {

def swImpl(tailwindExpr: Expr[Tailwind])(using Quotes): Expr[String] = {
import quotes.reflect.*
{{#if hasValidation}}
{{> classesValidation this }}
{{/if}}

def extractClassNames(term: Term, prefix: String = "", important: Boolean = false): List[String] = {
var stack = List((term, prefix, important))
Expand Down Expand Up @@ -79,7 +81,7 @@ def swImpl(tailwindExpr: Expr[Tailwind])(using Quotes): Expr[String] = {

val term = tailwindExpr.asTerm
val classList = extractClassNames(term).reverse
{{#if classesValidation}}
{{#if hasValidation}}
validate(classList)
{{/if}}
val combinedClasses = classList.mkString(" ")
Expand Down
3 changes: 2 additions & 1 deletion packages/scalawind/tests/cases/basic/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ test('basic test', () => {
previewCompliedResult: true,
laminar: false,
scalajsReact: true,
classesValidation: true
checkDuplication: true,
checkOptimization: true,
})
const filepath = path.join(__dirname, "./expected.txt")
// utils.writeFile(filepath, actual)
Expand Down
2 changes: 2 additions & 0 deletions packages/scalawind/tests/cases/full/full.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ test('full config test', () => {
previewCompliedResult: true,
laminar: true,
scalajsReact: false,
checkDuplication: false,
checkOptimization: false,
})
const filepath = path.join(__dirname, "./expected.txt")
// utils.writeFile(filepath, actual)
Expand Down

0 comments on commit 42d72fb

Please sign in to comment.