Skip to content

Commit

Permalink
Wildcard WIP 2
Browse files Browse the repository at this point in the history
  • Loading branch information
NoriSte committed Mar 9, 2024
1 parent c3d7aa0 commit d4cf1f1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
17 changes: 16 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
{"api":"1.0.0","editorType":["figma"],"id":"select-and-inspect","name":"Select and Inspect","main":"build/main.js","ui":"build/ui.js","networkAccess":{"allowedDomains":["none"]},"documentAccess":"dynamic-page"}
{
"api": "1.0.0",
"editorType": [
"figma"
],
"id": "select-and-inspect",
"name": "Select and Inspect",
"main": "build/main.js",
"ui": "build/ui.js",
"networkAccess": {
"allowedDomains": [
"none"
]
},
"documentAccess": "dynamic-page"
}
44 changes: 31 additions & 13 deletions src/core/processCurrentSelection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logExpression, logExpressionError, logStringifiedExpression } from '../utils/loggers'
import { stringifiedValueIsTheSame, stringifyError } from '../utils/generic'
import type { LogOptions } from '../types'
import { type IterationWildcardExpression, type LogOptions, getIterationWildcardExpressionError, isIterationWildcardExpression } from '../types'

export function processCurrentSelection(selection: ReadonlyArray<SceneNode>, configuration: LogOptions) {
processItems(
Expand Down Expand Up @@ -32,11 +32,12 @@ function processItem(item: unknown, logOptions: LogOptions) {
const trimmedExpression = expression.trim()
if (expression === '')
continue
processExpression(item, trimmedExpression, logOptions)
processExpression('', item, trimmedExpression, logOptions)
}
}

function processExpression(
prefix: string,
item: unknown,
expression: string,
logOptions: LogOptions,
Expand All @@ -47,14 +48,29 @@ function processExpression(
let errored = false

try {
if (expression.includes('[*]') && !expression.startsWith('[*]')) {
processWildCardExpression(item, expression, logOptions)
if (isIterationWildcardExpression(expression)) {
processWildCardExpression('└── ', item, expression, logOptions)
return
}

const iterationWildcardExpressionError = getIterationWildcardExpressionError(expression)
if (iterationWildcardExpressionError !== 'noError') {
switch (iterationWildcardExpressionError) {
case 'startsWith[*]':
logExpressionError(expression, '`[*]` cannot be at the start of the expression, try something like `children[*]` instead')
logExpressionError(`${expression} skipped`)
return

case 'endsWith[*].':
logExpressionError(expression, '`[*].` cannot be at the end of the expression, try something like `children[*].name` instead')
logExpressionError(`${expression} skipped`)
return
}
}

value = evaluateExpression(item, expression)

logExpression(expression, value)
logExpression('└── ', expression, value)
}
catch (e) {
errored = true
Expand All @@ -76,28 +92,30 @@ function processExpression(
// TODO: strong type literals + check recursivity (reactions[*].actions[*]) + give meaningful errors to the user

function processWildCardExpression(
prefix: string,
item: unknown,
expression: string,
expression: IterationWildcardExpression,
logOptions: LogOptions,
) {
const parts = expression.split('[*]')
const arrayExpression = parts[0]
const propertyExpression = parts[1]

console.log({ arrayExpression, propertyExpression })

if (!arrayExpression)
throw new Error('Array expression is missing (this is a TS-only protection)')

const array = evaluateExpression(item, arrayExpression)
if (!Array.isArray(array)) {
logExpressionError(expression, `The expression ${arrayExpression} does not evaluate to an array`)
const maybeArray = evaluateExpression(item, arrayExpression)
if (!Array.isArray(maybeArray)) {
if (!logOptions.hideExpressionErrors)
logExpressionError(expression, `The expression ${arrayExpression} does not evaluate to an array`)
return
}

for (const arrayItem of array) {
for (let i = 0, n = maybeArray.length; i < n; i++) {
console.log({ arrayExpression, propertyExpression, array: maybeArray, i, n })
const arrayItem = maybeArray[i]
if (propertyExpression)
processExpression(arrayItem, propertyExpression, logOptions)
processExpression(`└── ${arrayExpression}[${i}]`, arrayItem, propertyExpression, logOptions)
else
processItem(arrayItem, logOptions)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function main() {
signedLog('Load configuration')

loadSettingsAsync(defaultConfiguration, settingsKey).then((configuration) => {
logExpression('Configuration loaded')
logExpression('└── ', 'Configuration loaded')

// console.log(configuration)
const selectAndInspect = createSelectAndInspect({
Expand Down
42 changes: 42 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,45 @@ export const uiEventSchema = z.union([
value: z.boolean(),
}),
])

type ItemsIterationWildcardExpression = `${string}[*]`
type PropertiesIterationWildcardExpression = `${string}[*].${string}`
export type IterationWildcardExpression = ItemsIterationWildcardExpression | PropertiesIterationWildcardExpression

export function isIterationWildcardExpression(expression: unknown): expression is IterationWildcardExpression {
if (typeof expression !== 'string')
return false

if (!expression.includes('[*]'))
return false

if (expression.startsWith('[*]'))
return false

if (expression.endsWith('[*]'))
return true

if (!expression.includes('[*].'))
return false

if (expression.endsWith('[*].'))
return false

return true
}

export function getIterationWildcardExpressionError(expression: unknown) {
if (typeof expression !== 'string')
return 'notAString'

if (!expression.includes('[*]'))
return 'notIncludes[*]'

if (expression.startsWith('[*]'))
return 'startsWith[*]'

if (expression.endsWith('[*].'))
return 'endsWith[*].'

return 'noError'
}
4 changes: 2 additions & 2 deletions src/utils/loggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export function signedLog(...args: unknown[]) {
...args,
)
}
export function logExpression(expression: string, ...args: unknown[]) {
export function logExpression(prefix: string, expression: string, ...args: unknown[]) {
console.log(
`%c └── ${expression}`,
`%c ${prefix}${expression}`,
'color: #FF7AAC; font-style: italic;',
...args,
)
Expand Down

0 comments on commit d4cf1f1

Please sign in to comment.