@wyw-in-js/processor-utils 2.0.0-alpha.0 → 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,6 +39,7 @@ export class BaseProcessor {
39
39
  isValidValue(value) {
40
40
  return typeof value === "function" || isCSSable(value) || hasEvalMeta(value);
41
41
  }
42
+ /* eslint-enable @typescript-eslint/member-ordering */
42
43
  toString() {
43
44
  return this.tagSourceCode();
44
45
  }
@@ -1 +1 @@
1
- {"mappings":"AAEA,SAAS,mBAAmB;AAS5B,SAAS,wBAAwB;AACjC,SAAS,yCAAyC;AAQlD,OAAO,yBAAyB;AAChC,SAAS,iBAAiB;AAE1B,SAAS,sBAAsB;AAc/B,OAAO,MAAe,cAAc;CAClC,OAAc,OAAO,OAAO,OAAO;CAEnC,AAAgB,YAAwB,EAAE;CAE1C,AAAgB;CAEhB,AAAgB,eAAkC,EAAE;CAEpD,AAAO,iBAAmC,EAAE;CAE5C,AAAgB;CAEhB,AAAU;CAEV,AAAU;CAIV,AAAO,YACL,QACA,AAAO,WACP,AAAmB,YACnB,AAAgB,UAChB,AAAmB,UAInB,AAAgB,aAChB,AAAgB,cAChB,AAAmB,KACnB,AAAmB,SACnB,AAAmB,SACnB;EAZO;EACY;EACH;EACG;EAIH;EACA;EACG;EACA;EACA;AAEnB,iBACE,QACA,CAAC,SAAS,EACV,iDACD;EAED,MAAM,EAAE,WAAW,SAAS,oBAC1B,KAAK,aACL,KAAK,KACL,KAAK,SACL,KAAK,QACN;AAED,OAAK,YAAY;AACjB,OAAK,OAAO;AAEZ,GAAC,GAAG,KAAK,WAAW;;CAetB,AAAO,cAAc,YAAuC;AAC1D,OAAK,UAAU,KACb,kCAAkC;GAChC,GAAG;GACH,KAAK,WAAW,OAAO,KAAK,UAAU,OAAO;GAC7C,OAAO,WAAW,SAAS,KAAK,UAAU,SAAS;GACpD,CAAC,CACH;;CAGH,AAAO,aAAa,OAAgC;AAClD,SACE,OAAO,UAAU,cAAc,UAAU,MAAM,IAAI,YAAY,MAAM;;CAIzE,AAAO,WAAmB;AACxB,SAAO,KAAK,eAAe;;CAG7B,AAAU,gBAAwB;AAChC,SAAO,iBAAiB,KAAK,OAAO","names":[],"sources":["../src/BaseProcessor.ts"],"version":3,"sourcesContent":["/* eslint-disable class-methods-use-this */\nimport type { Artifact, ExpressionValue } from '@wyw-in-js/shared';\nimport { hasEvalMeta } from '@wyw-in-js/shared';\n\nimport type {\n AstService,\n Expression,\n Identifier,\n MemberExpression,\n SourceLocation,\n} from './ast';\nimport { expressionToCode } from './ast';\nimport { createProcessorDiagnosticArtifact } from './diagnostics';\nimport type {\n IInterpolation,\n Params,\n ProcessorDiagnostic,\n Value,\n ValueCache,\n} from './types';\nimport getClassNameAndSlug from './utils/getClassNameAndSlug';\nimport { isCSSable } from './utils/toCSS';\nimport type { IFileContext, IOptions } from './utils/types';\nimport { validateParams } from './utils/validateParams';\n\nexport type { Expression };\n\nexport type ProcessorParams = ConstructorParameters<typeof BaseProcessor>;\nexport type TailProcessorParams = ProcessorParams extends [Params, ...infer T]\n ? T\n : never;\n\nexport type TagSource = {\n imported: string;\n source: string;\n};\n\nexport abstract class BaseProcessor {\n public static SKIP = Symbol('skip');\n\n public readonly artifacts: Artifact[] = [];\n\n public readonly className: string;\n\n public readonly dependencies: ExpressionValue[] = [];\n\n public interpolations: IInterpolation[] = [];\n\n public readonly slug: string;\n\n protected callee: Identifier | MemberExpression;\n\n protected evaluated:\n | Record<'dependencies' | 'expression', Value[]>\n | undefined;\n\n public constructor(\n params: Params,\n public tagSource: TagSource,\n protected readonly astService: AstService,\n public readonly location: SourceLocation | null,\n protected readonly replacer: (\n replacement: Expression | ((tagPath: unknown) => Expression),\n isPure: boolean\n ) => void,\n public readonly displayName: string,\n public readonly isReferenced: boolean,\n protected readonly idx: number,\n protected readonly options: IOptions,\n protected readonly context: IFileContext\n ) {\n validateParams(\n params,\n ['callee'],\n 'Unknown error: a callee param is not specified'\n );\n\n const { className, slug } = getClassNameAndSlug(\n this.displayName,\n this.idx,\n this.options,\n this.context\n );\n\n this.className = className;\n this.slug = slug;\n\n [[, this.callee]] = params;\n }\n\n /**\n * A replacement for tag referenced in a template literal.\n */\n public abstract get asSelector(): string;\n\n /**\n * A replacement for the tag in evaluation time.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with an object with metadata.\n */\n public abstract get value(): Expression;\n\n public addDiagnostic(diagnostic: ProcessorDiagnostic): void {\n this.artifacts.push(\n createProcessorDiagnosticArtifact({\n ...diagnostic,\n end: diagnostic.end ?? this.location?.end ?? null,\n start: diagnostic.start ?? this.location?.start ?? null,\n })\n );\n }\n\n public isValidValue(value: unknown): value is Value {\n return (\n typeof value === 'function' || isCSSable(value) || hasEvalMeta(value)\n );\n }\n\n public toString(): string {\n return this.tagSourceCode();\n }\n\n protected tagSourceCode(): string {\n return expressionToCode(this.callee);\n }\n\n public abstract build(values: ValueCache): void;\n\n /**\n * Perform a replacement for the tag in evaluation time.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with an object with metadata.\n */\n public abstract doEvaltimeReplacement(): void;\n\n /**\n * Perform a replacement for the tag with its runtime version.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with a component.\n * If some parts require evaluated data for render,\n * they will be replaced with placeholders.\n */\n public abstract doRuntimeReplacement(): void;\n}\n"],"file":"BaseProcessor.js"}
1
+ {"mappings":"AAEA,SAAS,mBAAmB;AAS5B,SAAS,wBAAwB;AACjC,SAAS,yCAAyC;AASlD,OAAO,yBAAyB;AAChC,SAAS,iBAAiB;AAE1B,SAAS,sBAAsB;AAc/B,OAAO,MAAe,cAAc;CAClC,OAAc,OAAO,OAAO,OAAO;CAEnC,AAAgB,YAAwB,EAAE;CAE1C,AAAgB;CAEhB,AAAgB,eAAkC,EAAE;CAEpD,AAAO,iBAAmC,EAAE;CAE5C,AAAgB;CAEhB,AAAU;CAEV,AAAU;CAIV,AAAO,YACL,QACA,AAAO,WACP,AAAmB,YACnB,AAAgB,UAChB,AAAmB,UAInB,AAAgB,aAChB,AAAgB,cAChB,AAAmB,KACnB,AAAmB,SACnB,AAAmB,SACnB;EAZO;EACY;EACH;EACG;EAIH;EACA;EACG;EACA;EACA;AAEnB,iBACE,QACA,CAAC,SAAS,EACV,iDACD;EAED,MAAM,EAAE,WAAW,SAAS,oBAC1B,KAAK,aACL,KAAK,KACL,KAAK,SACL,KAAK,QACN;AAED,OAAK,YAAY;AACjB,OAAK,OAAO;AAEZ,GAAC,GAAG,KAAK,WAAW;;CAetB,AAAO,cAAc,YAAuC;AAC1D,OAAK,UAAU,KACb,kCAAkC;GAChC,GAAG;GACH,KAAK,WAAW,OAAO,KAAK,UAAU,OAAO;GAC7C,OAAO,WAAW,SAAS,KAAK,UAAU,SAAS;GACpD,CAAC,CACH;;CAmBH,AAAO,aAAa,OAAgC;AAClD,SACE,OAAO,UAAU,cAAc,UAAU,MAAM,IAAI,YAAY,MAAM;;;CAKzE,AAAO,WAAmB;AACxB,SAAO,KAAK,eAAe;;CAG7B,AAAU,gBAAwB;AAChC,SAAO,iBAAiB,KAAK,OAAO","names":[],"sources":["../src/BaseProcessor.ts"],"version":3,"sourcesContent":["/* eslint-disable class-methods-use-this */\nimport type { Artifact, ExpressionValue } from '@wyw-in-js/shared';\nimport { hasEvalMeta } from '@wyw-in-js/shared';\n\nimport type {\n AstService,\n Expression,\n Identifier,\n MemberExpression,\n SourceLocation,\n} from './ast';\nimport { expressionToCode } from './ast';\nimport { createProcessorDiagnosticArtifact } from './diagnostics';\nimport type { ProcessorStaticContext, ProcessorStaticValue } from './static';\nimport type {\n IInterpolation,\n Params,\n ProcessorDiagnostic,\n Value,\n ValueCache,\n} from './types';\nimport getClassNameAndSlug from './utils/getClassNameAndSlug';\nimport { isCSSable } from './utils/toCSS';\nimport type { IFileContext, IOptions } from './utils/types';\nimport { validateParams } from './utils/validateParams';\n\nexport type { Expression };\n\nexport type ProcessorParams = ConstructorParameters<typeof BaseProcessor>;\nexport type TailProcessorParams = ProcessorParams extends [Params, ...infer T]\n ? T\n : never;\n\nexport type TagSource = {\n imported: string;\n source: string;\n};\n\nexport abstract class BaseProcessor {\n public static SKIP = Symbol('skip');\n\n public readonly artifacts: Artifact[] = [];\n\n public readonly className: string;\n\n public readonly dependencies: ExpressionValue[] = [];\n\n public interpolations: IInterpolation[] = [];\n\n public readonly slug: string;\n\n protected callee: Identifier | MemberExpression;\n\n protected evaluated:\n | Record<'dependencies' | 'expression', Value[]>\n | undefined;\n\n public constructor(\n params: Params,\n public tagSource: TagSource,\n protected readonly astService: AstService,\n public readonly location: SourceLocation | null,\n protected readonly replacer: (\n replacement: Expression | ((tagPath: unknown) => Expression),\n isPure: boolean\n ) => void,\n public readonly displayName: string,\n public readonly isReferenced: boolean,\n protected readonly idx: number,\n protected readonly options: IOptions,\n protected readonly context: IFileContext\n ) {\n validateParams(\n params,\n ['callee'],\n 'Unknown error: a callee param is not specified'\n );\n\n const { className, slug } = getClassNameAndSlug(\n this.displayName,\n this.idx,\n this.options,\n this.context\n );\n\n this.className = className;\n this.slug = slug;\n\n [[, this.callee]] = params;\n }\n\n /**\n * A replacement for tag referenced in a template literal.\n */\n public abstract get asSelector(): string;\n\n /**\n * A replacement for the tag in evaluation time.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with an object with metadata.\n */\n public abstract get value(): Expression;\n\n public addDiagnostic(diagnostic: ProcessorDiagnostic): void {\n this.artifacts.push(\n createProcessorDiagnosticArtifact({\n ...diagnostic,\n end: diagnostic.end ?? this.location?.end ?? null,\n start: diagnostic.start ?? this.location?.start ?? null,\n })\n );\n }\n\n /* eslint-disable @typescript-eslint/member-ordering */\n public getStaticValue?(\n context: ProcessorStaticContext\n ): ProcessorStaticValue | null | undefined;\n\n public resolveStaticInterpolation?(\n interpolation: IInterpolation,\n value: ProcessorStaticValue,\n context: ProcessorStaticContext\n ): ProcessorStaticValue | null | undefined;\n\n public resolveStaticTagTarget?(\n target: ProcessorStaticValue,\n context: ProcessorStaticContext\n ): ProcessorStaticValue | null | undefined;\n\n public isValidValue(value: unknown): value is Value {\n return (\n typeof value === 'function' || isCSSable(value) || hasEvalMeta(value)\n );\n }\n /* eslint-enable @typescript-eslint/member-ordering */\n\n public toString(): string {\n return this.tagSourceCode();\n }\n\n protected tagSourceCode(): string {\n return expressionToCode(this.callee);\n }\n\n public abstract build(values: ValueCache): void;\n\n /**\n * Perform a replacement for the tag in evaluation time.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with an object with metadata.\n */\n public abstract doEvaltimeReplacement(): void;\n\n /**\n * Perform a replacement for the tag with its runtime version.\n * For example, `css` tag will be replaced with its className,\n * whereas `styled` tag will be replaced with a component.\n * If some parts require evaluated data for render,\n * they will be replaced with placeholders.\n */\n public abstract doRuntimeReplacement(): void;\n}\n"],"file":"BaseProcessor.js"}
package/esm/ast.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"AA4HA,MAAM,qBACJ,OACA,QAA6B,aAClB;CACX,MAAM,OAAO,KAAK,UAAU,MAAM;AAClC,KAAI,UAAU,UAAU;AACtB,SAAO;;AAGT,QAAO,IAAI,KAAK,MAAM,GAAG,CAAC,EAAE,CAAC,QAAQ,MAAM,MAAM,CAAC;;AAGpD,MAAM,UAAU,UAA0B,KAAK,OAAO,MAAM;AAE5D,MAAM,+BACJ,YACA,YACW;AACX,KAAI,WAAW,SAAS,cAAc;AACpC,SAAQ,WAA0B;;AAGpC,KAAI,WAAW,SAAS,iBAAiB;AACvC,SAAO,kBAAmB,WAA6B,OAAO,QAAQ,MAAM;;AAG9E,KACE,WAAW,SAAS,oBACpB,WAAW,SAAS,kBACpB;AACA,SAAO,OAAQ,WAA+C,MAAM;;AAGtE,KAAI,WAAW,SAAS,eAAe;AACrC,SAAO;;AAGT,KAAI,WAAW,SAAS,oBAAoB;EAC1C,MAAM,mBAAmB;EACzB,MAAM,SAAS,4BAA4B,iBAAiB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,OAAO,CAAC;;AAGrD,QAAO,WAAW;;AAGpB,OAAO,MAAM,oBAAoB,eAC/B,4BAA4B,YAAY;CACtC,QAAQ;CACR,OAAO;CACR,CAAC","names":[],"sources":["../src/ast.ts"],"version":3,"sourcesContent":["import type { Location } from '@wyw-in-js/shared';\n\nexport type SourceLocation = {\n end: Location;\n filename?: string;\n identifierName?: string | null;\n start: Location;\n};\n\nexport type BaseAstNode = {\n end?: number | null;\n loc?: unknown;\n start?: number | null;\n type: string;\n};\n\nexport type Expression = BaseAstNode;\n\nexport type Identifier = Expression & {\n name: string;\n type: 'Identifier';\n};\n\nexport type StringLiteral = Expression & {\n type: 'StringLiteral';\n value: string;\n};\n\nexport type NumericLiteral = Expression & {\n type: 'NumericLiteral';\n value: number;\n};\n\nexport type BooleanLiteral = Expression & {\n type: 'BooleanLiteral';\n value: boolean;\n};\n\nexport type NullLiteral = Expression & {\n type: 'NullLiteral';\n};\n\nexport type BlockStatement = BaseAstNode & {\n body: BaseAstNode[];\n type: 'BlockStatement';\n};\n\nexport type TemplateElement = BaseAstNode & {\n loc?: SourceLocation | null;\n tail: boolean;\n type: 'TemplateElement';\n value: {\n cooked?: string | null;\n raw: string;\n };\n};\n\nexport type MemberExpression = Expression & {\n computed?: boolean;\n object: Expression;\n property: Expression;\n type: 'MemberExpression';\n};\n\nexport type ObjectProperty = BaseAstNode & {\n computed?: boolean;\n key: Expression;\n shorthand?: boolean;\n type: 'ObjectProperty';\n value: Expression;\n};\n\nexport type ObjectExpression = Expression & {\n properties: ObjectProperty[];\n type: 'ObjectExpression';\n};\n\nexport type ArrayExpression = Expression & {\n elements: (Expression | null)[];\n type: 'ArrayExpression';\n};\n\nexport type CallExpression = Expression & {\n arguments: Expression[];\n callee: Expression;\n type: 'CallExpression';\n};\n\nexport type ArrowFunctionExpression = Expression & {\n async?: boolean;\n body: BlockStatement | Expression;\n params: Identifier[];\n type: 'ArrowFunctionExpression';\n};\n\nexport type AstService = {\n addDefaultImport(source: string, nameHint?: string): Identifier;\n addNamedImport(name: string, source: string, nameHint?: string): Identifier;\n arrayExpression(elements: (Expression | null)[]): ArrayExpression;\n arrowFunctionExpression(\n params: Identifier[],\n body: BlockStatement | Expression\n ): ArrowFunctionExpression;\n blockStatement(body: BaseAstNode[]): BlockStatement;\n booleanLiteral(value: boolean): BooleanLiteral;\n callExpression(callee: Expression, args: Expression[]): CallExpression;\n identifier(name: string): Identifier;\n memberExpression(\n object: Expression,\n property: Expression,\n computed?: boolean\n ): MemberExpression;\n nullLiteral(): NullLiteral;\n numericLiteral(value: number): NumericLiteral;\n objectExpression(properties: ObjectProperty[]): ObjectExpression;\n objectProperty(key: Expression, value: Expression): ObjectProperty;\n stringLiteral(value: string): StringLiteral;\n};\n\ntype PrintContext = {\n indent: number;\n quote?: 'double' | 'single';\n};\n\nconst stringLiteralCode = (\n value: string,\n quote: 'double' | 'single' = 'double'\n): string => {\n const json = JSON.stringify(value);\n if (quote === 'double') {\n return json;\n }\n\n return `'${json.slice(1, -1).replace(/'/g, \"\\\\'\")}'`;\n};\n\nconst indent = (level: number): string => ' '.repeat(level);\n\nconst expressionToCodeWithContext = (\n expression: Expression,\n context: PrintContext\n): string => {\n if (expression.type === 'Identifier') {\n return (expression as Identifier).name;\n }\n\n if (expression.type === 'StringLiteral') {\n return stringLiteralCode((expression as StringLiteral).value, context.quote);\n }\n\n if (\n expression.type === 'NumericLiteral' ||\n expression.type === 'BooleanLiteral'\n ) {\n return String((expression as NumericLiteral | BooleanLiteral).value);\n }\n\n if (expression.type === 'NullLiteral') {\n return 'null';\n }\n\n if (expression.type === 'MemberExpression') {\n const memberExpression = expression as MemberExpression;\n const object = expressionToCodeWithContext(memberExpression.object, context);\n const property = expressionToCodeWithContext(\n memberExpression.property,\n context\n );\n return memberExpression.computed\n ? `${object}[${property}]`\n : `${object}.${property}`;\n }\n\n if (expression.type === 'CallExpression') {\n const callExpression = expression as CallExpression;\n const callee = expressionToCodeWithContext(callExpression.callee, context);\n const args = callExpression.arguments.map((arg) =>\n expressionToCodeWithContext(arg, {\n ...context,\n quote: arg.type === 'StringLiteral' ? 'single' : context.quote,\n })\n );\n\n return `${callee}(${args.join(', ')})`;\n }\n\n if (expression.type === 'ArrowFunctionExpression') {\n const arrow = expression as ArrowFunctionExpression;\n return `(${arrow.params\n .map((param) => expressionToCodeWithContext(param, context))\n .join(', ')}) => ${expressionToCodeWithContext(arrow.body, context)}`;\n }\n\n if (expression.type === 'ArrayExpression') {\n return `[${(expression as ArrayExpression).elements\n .map((item) =>\n item ? expressionToCodeWithContext(item, context) : ''\n )\n .join(', ')}]`;\n }\n\n if (expression.type === 'BlockStatement') {\n return '{ }';\n }\n\n if (expression.type === 'ObjectExpression') {\n const objectExpression = expression as ObjectExpression;\n if (objectExpression.properties.length === 0) {\n return '{}';\n }\n\n const nextIndent = context.indent + 1;\n const properties = objectExpression.properties\n .map((property) => {\n const key = expressionToCodeWithContext(property.key, {\n ...context,\n quote: 'double',\n });\n const value = expressionToCodeWithContext(property.value, {\n indent: nextIndent,\n quote: 'double',\n });\n return `${indent(nextIndent)}${key}: ${value}`;\n })\n .join(',\\n');\n\n return `{\\n${properties}\\n${indent(context.indent)}}`;\n }\n\n return expression.type;\n};\n\nexport const expressionToCode = (expression: Expression): string =>\n expressionToCodeWithContext(expression, {\n indent: 0,\n quote: 'double',\n });\n"],"file":"ast.js"}
1
+ {"mappings":"AA4HA,MAAM,qBACJ,OACA,QAA6B,aAClB;CACX,MAAM,OAAO,KAAK,UAAU,MAAM;AAClC,KAAI,UAAU,UAAU;AACtB,SAAO;;AAGT,QAAO,IAAI,KAAK,MAAM,GAAG,CAAC,EAAE,CAAC,QAAQ,MAAM,MAAM,CAAC;;AAGpD,MAAM,UAAU,UAA0B,KAAK,OAAO,MAAM;AAE5D,MAAM,+BACJ,YACA,YACW;AACX,KAAI,WAAW,SAAS,cAAc;AACpC,SAAQ,WAA0B;;AAGpC,KAAI,WAAW,SAAS,iBAAiB;AACvC,SAAO,kBACJ,WAA6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AAS,KAAK;IACpD,GAAG;IACH,OAAO;IACR,CAAC;GACF,MAAM,QAAQ,4BAA4B,SAAS,OAAO;IACxD,QAAQ;IACR,OAAO;IACR,CAAC;AACF,UAAO,GAAG,OAAO,WAAW,GAAG,IAAI,IAAI;IACvC,CACD,KAAK,MAAM;AAEd,SAAO,MAAM,WAAW,IAAI,OAAO,QAAQ,OAAO,CAAC;;AAGrD,QAAO,WAAW;;AAGpB,OAAO,MAAM,oBAAoB,eAC/B,4BAA4B,YAAY;CACtC,QAAQ;CACR,OAAO;CACR,CAAC","names":[],"sources":["../src/ast.ts"],"version":3,"sourcesContent":["import type { Location } from '@wyw-in-js/shared';\n\nexport type SourceLocation = {\n end: Location;\n filename?: string;\n identifierName?: string | null;\n start: Location;\n};\n\nexport type BaseAstNode = {\n end?: number | null;\n loc?: unknown;\n start?: number | null;\n type: string;\n};\n\nexport type Expression = BaseAstNode;\n\nexport type Identifier = Expression & {\n name: string;\n type: 'Identifier';\n};\n\nexport type StringLiteral = Expression & {\n type: 'StringLiteral';\n value: string;\n};\n\nexport type NumericLiteral = Expression & {\n type: 'NumericLiteral';\n value: number;\n};\n\nexport type BooleanLiteral = Expression & {\n type: 'BooleanLiteral';\n value: boolean;\n};\n\nexport type NullLiteral = Expression & {\n type: 'NullLiteral';\n};\n\nexport type BlockStatement = BaseAstNode & {\n body: BaseAstNode[];\n type: 'BlockStatement';\n};\n\nexport type TemplateElement = BaseAstNode & {\n loc?: SourceLocation | null;\n tail: boolean;\n type: 'TemplateElement';\n value: {\n cooked?: string | null;\n raw: string;\n };\n};\n\nexport type MemberExpression = Expression & {\n computed?: boolean;\n object: Expression;\n property: Expression;\n type: 'MemberExpression';\n};\n\nexport type ObjectProperty = BaseAstNode & {\n computed?: boolean;\n key: Expression;\n shorthand?: boolean;\n type: 'ObjectProperty';\n value: Expression;\n};\n\nexport type ObjectExpression = Expression & {\n properties: ObjectProperty[];\n type: 'ObjectExpression';\n};\n\nexport type ArrayExpression = Expression & {\n elements: (Expression | null)[];\n type: 'ArrayExpression';\n};\n\nexport type CallExpression = Expression & {\n arguments: Expression[];\n callee: Expression;\n type: 'CallExpression';\n};\n\nexport type ArrowFunctionExpression = Expression & {\n async?: boolean;\n body: BlockStatement | Expression;\n params: Identifier[];\n type: 'ArrowFunctionExpression';\n};\n\nexport type AstService = {\n addDefaultImport(source: string, nameHint?: string): Identifier;\n addNamedImport(name: string, source: string, nameHint?: string): Identifier;\n arrayExpression(elements: (Expression | null)[]): ArrayExpression;\n arrowFunctionExpression(\n params: Identifier[],\n body: BlockStatement | Expression\n ): ArrowFunctionExpression;\n blockStatement(body: BaseAstNode[]): BlockStatement;\n booleanLiteral(value: boolean): BooleanLiteral;\n callExpression(callee: Expression, args: Expression[]): CallExpression;\n identifier(name: string): Identifier;\n memberExpression(\n object: Expression,\n property: Expression,\n computed?: boolean\n ): MemberExpression;\n nullLiteral(): NullLiteral;\n numericLiteral(value: number): NumericLiteral;\n objectExpression(properties: ObjectProperty[]): ObjectExpression;\n objectProperty(key: Expression, value: Expression): ObjectProperty;\n stringLiteral(value: string): StringLiteral;\n};\n\ntype PrintContext = {\n indent: number;\n quote?: 'double' | 'single';\n};\n\nconst stringLiteralCode = (\n value: string,\n quote: 'double' | 'single' = 'double'\n): string => {\n const json = JSON.stringify(value);\n if (quote === 'double') {\n return json;\n }\n\n return `'${json.slice(1, -1).replace(/'/g, \"\\\\'\")}'`;\n};\n\nconst indent = (level: number): string => ' '.repeat(level);\n\nconst expressionToCodeWithContext = (\n expression: Expression,\n context: PrintContext\n): string => {\n if (expression.type === 'Identifier') {\n return (expression as Identifier).name;\n }\n\n if (expression.type === 'StringLiteral') {\n return stringLiteralCode(\n (expression as StringLiteral).value,\n context.quote\n );\n }\n\n if (\n expression.type === 'NumericLiteral' ||\n expression.type === 'BooleanLiteral'\n ) {\n return String((expression as NumericLiteral | BooleanLiteral).value);\n }\n\n if (expression.type === 'NullLiteral') {\n return 'null';\n }\n\n if (expression.type === 'MemberExpression') {\n const memberExpression = expression as MemberExpression;\n const object = expressionToCodeWithContext(\n memberExpression.object,\n context\n );\n const property = expressionToCodeWithContext(\n memberExpression.property,\n context\n );\n return memberExpression.computed\n ? `${object}[${property}]`\n : `${object}.${property}`;\n }\n\n if (expression.type === 'CallExpression') {\n const callExpression = expression as CallExpression;\n const callee = expressionToCodeWithContext(callExpression.callee, context);\n const args = callExpression.arguments.map((arg) =>\n expressionToCodeWithContext(arg, {\n ...context,\n quote: arg.type === 'StringLiteral' ? 'single' : context.quote,\n })\n );\n\n return `${callee}(${args.join(', ')})`;\n }\n\n if (expression.type === 'ArrowFunctionExpression') {\n const arrow = expression as ArrowFunctionExpression;\n return `(${arrow.params\n .map((param) => expressionToCodeWithContext(param, context))\n .join(', ')}) => ${expressionToCodeWithContext(arrow.body, context)}`;\n }\n\n if (expression.type === 'ArrayExpression') {\n return `[${(expression as ArrayExpression).elements\n .map((item) => (item ? expressionToCodeWithContext(item, context) : ''))\n .join(', ')}]`;\n }\n\n if (expression.type === 'BlockStatement') {\n return '{ }';\n }\n\n if (expression.type === 'ObjectExpression') {\n const objectExpression = expression as ObjectExpression;\n if (objectExpression.properties.length === 0) {\n return '{}';\n }\n\n const nextIndent = context.indent + 1;\n const properties = objectExpression.properties\n .map((property) => {\n const key = expressionToCodeWithContext(property.key, {\n ...context,\n quote: 'double',\n });\n const value = expressionToCodeWithContext(property.value, {\n indent: nextIndent,\n quote: 'double',\n });\n return `${indent(nextIndent)}${key}: ${value}`;\n })\n .join(',\\n');\n\n return `{\\n${properties}\\n${indent(context.indent)}}`;\n }\n\n return expression.type;\n};\n\nexport const expressionToCode = (expression: Expression): string =>\n expressionToCodeWithContext(expression, {\n indent: 0,\n quote: 'double',\n });\n"],"file":"ast.js"}
package/esm/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"AAAA,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SACE,mCACA,+BACA,qCACK;AA0BP,cAAc;AACd,SAAS,iBAAiB;AAE1B,SAAS,eAAe,sBAAsB;AAE9C,SAAS,+BAA+B;AACxC,SAAS,4BAA4B","names":[],"sources":["../src/index.ts"],"version":3,"sourcesContent":["export { BaseProcessor } from './BaseProcessor';\nexport { expressionToCode } from './ast';\nexport {\n createProcessorDiagnosticArtifact,\n isProcessorDiagnosticArtifact,\n PROCESSOR_DIAGNOSTIC_ARTIFACT,\n} from './diagnostics';\nexport type { ProcessorDiagnosticArtifact } from './diagnostics';\nexport type {\n ArrayExpression,\n ArrowFunctionExpression,\n AstService,\n BaseAstNode,\n BlockStatement,\n BooleanLiteral,\n CallExpression,\n Expression,\n Identifier,\n MemberExpression,\n NullLiteral,\n NumericLiteral,\n ObjectExpression,\n ObjectProperty,\n SourceLocation,\n StringLiteral,\n TemplateElement,\n} from './ast';\nexport type {\n ProcessorParams,\n TagSource,\n TailProcessorParams,\n} from './BaseProcessor';\nexport * from './types';\nexport { buildSlug } from './utils/buildSlug';\nexport type { IOptions, IFileContext } from './utils/types';\nexport { isValidParams, validateParams } from './utils/validateParams';\nexport type { MapParams, ParamConstraints } from './utils/validateParams';\nexport { TaggedTemplateProcessor } from './TaggedTemplateProcessor';\nexport { toValidCSSIdentifier } from './utils/toValidCSSIdentifier';\n"],"file":"index.js"}
1
+ {"mappings":"AAAA,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SACE,mCACA,+BACA,qCACK;AAyCP,cAAc;AACd,SAAS,iBAAiB;AAE1B,SAAS,eAAe,sBAAsB;AAE9C,SAAS,+BAA+B;AACxC,SAAS,4BAA4B","names":[],"sources":["../src/index.ts"],"version":3,"sourcesContent":["export { BaseProcessor } from './BaseProcessor';\nexport { expressionToCode } from './ast';\nexport {\n createProcessorDiagnosticArtifact,\n isProcessorDiagnosticArtifact,\n PROCESSOR_DIAGNOSTIC_ARTIFACT,\n} from './diagnostics';\nexport type { ProcessorDiagnosticArtifact } from './diagnostics';\nexport type {\n ArrayExpression,\n ArrowFunctionExpression,\n AstService,\n BaseAstNode,\n BlockStatement,\n BooleanLiteral,\n CallExpression,\n Expression,\n Identifier,\n MemberExpression,\n NullLiteral,\n NumericLiteral,\n ObjectExpression,\n ObjectProperty,\n SourceLocation,\n StringLiteral,\n TemplateElement,\n} from './ast';\nexport type {\n ProcessorParams,\n TagSource,\n TailProcessorParams,\n} from './BaseProcessor';\nexport type {\n ProcessorStaticClassNameValue,\n ProcessorStaticContext,\n ProcessorStaticDebugReason,\n ProcessorStaticDependency,\n ProcessorStaticInterpolationResolver,\n ProcessorStaticMetadata,\n ProcessorStaticOpaqueComponentValue,\n ProcessorStaticRuntimeCallbackValue,\n ProcessorStaticSelectorChainValue,\n ProcessorStaticSerializableValue,\n ProcessorStaticTagTargetResolver,\n ProcessorStaticUnresolvedValue,\n ProcessorStaticValue,\n} from './static';\nexport * from './types';\nexport { buildSlug } from './utils/buildSlug';\nexport type { IOptions, IFileContext } from './utils/types';\nexport { isValidParams, validateParams } from './utils/validateParams';\nexport type { MapParams, ParamConstraints } from './utils/validateParams';\nexport { TaggedTemplateProcessor } from './TaggedTemplateProcessor';\nexport { toValidCSSIdentifier } from './utils/toValidCSSIdentifier';\n"],"file":"index.js"}
package/esm/static.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=static.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"","names":[],"sources":["../src/static.ts"],"version":3,"sourcesContent":["import type { SourceLocation } from './ast';\nimport type { IFileContext, IOptions } from './utils/types';\nimport type { IInterpolation } from './types';\nimport type { TagSource } from './BaseProcessor';\n\nexport type ProcessorStaticSerializableValue = {\n kind: 'serializable';\n value: unknown;\n};\n\nexport type ProcessorStaticClassNameValue = {\n className: string;\n kind: 'class-name';\n value?: unknown;\n};\n\nexport type ProcessorStaticSelectorChainValue = {\n className: string;\n kind: 'selector-chain';\n selectors: string[];\n value?: unknown;\n};\n\nexport type ProcessorStaticRuntimeCallbackValue = {\n kind: 'runtime-callback';\n source?: string;\n value?: unknown;\n};\n\nexport type ProcessorStaticOpaqueComponentValue = {\n className?: string;\n kind: 'opaque-component';\n value?: unknown;\n};\n\nexport type ProcessorStaticUnresolvedValue = {\n details?: Readonly<Record<string, unknown>>;\n kind: 'unresolved';\n reason: string;\n};\n\nexport type ProcessorStaticValue =\n | ProcessorStaticClassNameValue\n | ProcessorStaticOpaqueComponentValue\n | ProcessorStaticRuntimeCallbackValue\n | ProcessorStaticSelectorChainValue\n | ProcessorStaticSerializableValue\n | ProcessorStaticUnresolvedValue;\n\nexport type ProcessorStaticDependency = Readonly<{\n imported?: string;\n local?: string;\n reason?: string;\n source?: string;\n}>;\n\nexport type ProcessorStaticDebugReason = Readonly<{\n details?: Readonly<Record<string, unknown>>;\n reason: string;\n}>;\n\nexport type ProcessorStaticMetadata = Readonly<{\n className: string;\n displayName: string;\n isReferenced: boolean;\n location: SourceLocation | null;\n slug: string;\n tagSource: TagSource;\n}>;\n\nexport type ProcessorStaticContext = Readonly<{\n fileContext: Readonly<IFileContext>;\n metadata: ProcessorStaticMetadata;\n options: Readonly<IOptions>;\n addDependency(dependency: ProcessorStaticDependency): void;\n debug(reason: ProcessorStaticDebugReason): void;\n unresolved(\n reason: string,\n details?: Readonly<Record<string, unknown>>\n ): ProcessorStaticUnresolvedValue;\n}>;\n\nexport type ProcessorStaticInterpolationResolver = (\n interpolation: IInterpolation,\n value: ProcessorStaticValue,\n context: ProcessorStaticContext\n) => ProcessorStaticValue | null | undefined;\n\nexport type ProcessorStaticTagTargetResolver = (\n target: ProcessorStaticValue,\n context: ProcessorStaticContext\n) => ProcessorStaticValue | null | undefined;\n"],"file":"static.js"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@wyw-in-js/processor-utils",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.1",
4
4
  "type": "module",
5
5
  "dependencies": {
6
- "@wyw-in-js/shared": "2.0.0-alpha.0"
6
+ "@wyw-in-js/shared": "2.0.0-alpha.1"
7
7
  },
8
8
  "devDependencies": {
9
9
  "@types/node": "^22.0.0",
@@ -1,5 +1,6 @@
1
1
  import type { Artifact, ExpressionValue } from '@wyw-in-js/shared';
2
2
  import type { AstService, Expression, Identifier, MemberExpression, SourceLocation } from './ast';
3
+ import type { ProcessorStaticContext, ProcessorStaticValue } from './static';
3
4
  import type { IInterpolation, Params, ProcessorDiagnostic, Value, ValueCache } from './types';
4
5
  import type { IFileContext, IOptions } from './utils/types';
5
6
  export type { Expression };
@@ -39,6 +40,9 @@ export declare abstract class BaseProcessor {
39
40
  */
40
41
  abstract get value(): Expression;
41
42
  addDiagnostic(diagnostic: ProcessorDiagnostic): void;
43
+ getStaticValue?(context: ProcessorStaticContext): ProcessorStaticValue | null | undefined;
44
+ resolveStaticInterpolation?(interpolation: IInterpolation, value: ProcessorStaticValue, context: ProcessorStaticContext): ProcessorStaticValue | null | undefined;
45
+ resolveStaticTagTarget?(target: ProcessorStaticValue, context: ProcessorStaticContext): ProcessorStaticValue | null | undefined;
42
46
  isValidValue(value: unknown): value is Value;
43
47
  toString(): string;
44
48
  protected tagSourceCode(): string;
@@ -48,6 +48,7 @@ export class BaseProcessor {
48
48
  isValidValue(value) {
49
49
  return (typeof value === 'function' || isCSSable(value) || hasEvalMeta(value));
50
50
  }
51
+ /* eslint-enable @typescript-eslint/member-ordering */
51
52
  toString() {
52
53
  return this.tagSourceCode();
53
54
  }
package/types/ast.js CHANGED
@@ -45,7 +45,7 @@ const expressionToCodeWithContext = (expression, context) => {
45
45
  }
46
46
  if (expression.type === 'ArrayExpression') {
47
47
  return `[${expression.elements
48
- .map((item) => item ? expressionToCodeWithContext(item, context) : '')
48
+ .map((item) => (item ? expressionToCodeWithContext(item, context) : ''))
49
49
  .join(', ')}]`;
50
50
  }
51
51
  if (expression.type === 'BlockStatement') {
package/types/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { createProcessorDiagnosticArtifact, isProcessorDiagnosticArtifact, PROCE
4
4
  export type { ProcessorDiagnosticArtifact } from './diagnostics';
5
5
  export type { ArrayExpression, ArrowFunctionExpression, AstService, BaseAstNode, BlockStatement, BooleanLiteral, CallExpression, Expression, Identifier, MemberExpression, NullLiteral, NumericLiteral, ObjectExpression, ObjectProperty, SourceLocation, StringLiteral, TemplateElement, } from './ast';
6
6
  export type { ProcessorParams, TagSource, TailProcessorParams, } from './BaseProcessor';
7
+ export type { ProcessorStaticClassNameValue, ProcessorStaticContext, ProcessorStaticDebugReason, ProcessorStaticDependency, ProcessorStaticInterpolationResolver, ProcessorStaticMetadata, ProcessorStaticOpaqueComponentValue, ProcessorStaticRuntimeCallbackValue, ProcessorStaticSelectorChainValue, ProcessorStaticSerializableValue, ProcessorStaticTagTargetResolver, ProcessorStaticUnresolvedValue, ProcessorStaticValue, } from './static';
7
8
  export * from './types';
8
9
  export { buildSlug } from './utils/buildSlug';
9
10
  export type { IOptions, IFileContext } from './utils/types';
@@ -0,0 +1,63 @@
1
+ import type { SourceLocation } from './ast';
2
+ import type { IFileContext, IOptions } from './utils/types';
3
+ import type { IInterpolation } from './types';
4
+ import type { TagSource } from './BaseProcessor';
5
+ export type ProcessorStaticSerializableValue = {
6
+ kind: 'serializable';
7
+ value: unknown;
8
+ };
9
+ export type ProcessorStaticClassNameValue = {
10
+ className: string;
11
+ kind: 'class-name';
12
+ value?: unknown;
13
+ };
14
+ export type ProcessorStaticSelectorChainValue = {
15
+ className: string;
16
+ kind: 'selector-chain';
17
+ selectors: string[];
18
+ value?: unknown;
19
+ };
20
+ export type ProcessorStaticRuntimeCallbackValue = {
21
+ kind: 'runtime-callback';
22
+ source?: string;
23
+ value?: unknown;
24
+ };
25
+ export type ProcessorStaticOpaqueComponentValue = {
26
+ className?: string;
27
+ kind: 'opaque-component';
28
+ value?: unknown;
29
+ };
30
+ export type ProcessorStaticUnresolvedValue = {
31
+ details?: Readonly<Record<string, unknown>>;
32
+ kind: 'unresolved';
33
+ reason: string;
34
+ };
35
+ export type ProcessorStaticValue = ProcessorStaticClassNameValue | ProcessorStaticOpaqueComponentValue | ProcessorStaticRuntimeCallbackValue | ProcessorStaticSelectorChainValue | ProcessorStaticSerializableValue | ProcessorStaticUnresolvedValue;
36
+ export type ProcessorStaticDependency = Readonly<{
37
+ imported?: string;
38
+ local?: string;
39
+ reason?: string;
40
+ source?: string;
41
+ }>;
42
+ export type ProcessorStaticDebugReason = Readonly<{
43
+ details?: Readonly<Record<string, unknown>>;
44
+ reason: string;
45
+ }>;
46
+ export type ProcessorStaticMetadata = Readonly<{
47
+ className: string;
48
+ displayName: string;
49
+ isReferenced: boolean;
50
+ location: SourceLocation | null;
51
+ slug: string;
52
+ tagSource: TagSource;
53
+ }>;
54
+ export type ProcessorStaticContext = Readonly<{
55
+ fileContext: Readonly<IFileContext>;
56
+ metadata: ProcessorStaticMetadata;
57
+ options: Readonly<IOptions>;
58
+ addDependency(dependency: ProcessorStaticDependency): void;
59
+ debug(reason: ProcessorStaticDebugReason): void;
60
+ unresolved(reason: string, details?: Readonly<Record<string, unknown>>): ProcessorStaticUnresolvedValue;
61
+ }>;
62
+ export type ProcessorStaticInterpolationResolver = (interpolation: IInterpolation, value: ProcessorStaticValue, context: ProcessorStaticContext) => ProcessorStaticValue | null | undefined;
63
+ export type ProcessorStaticTagTargetResolver = (target: ProcessorStaticValue, context: ProcessorStaticContext) => ProcessorStaticValue | null | undefined;
@@ -0,0 +1 @@
1
+ export {};