@wuchale/astro 0.2.2 → 0.2.3

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.
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import { AstroTransformer } from './transformer.js';
8
8
  */
9
9
  export function createAstroHeuristic(opts) {
10
10
  const defaultHeuristic = createHeuristic(opts);
11
- return (msg) => {
11
+ return msg => {
12
12
  const defRes = defaultHeuristic(msg);
13
13
  if (!defRes) {
14
14
  return false;
@@ -30,12 +30,12 @@ const defaultRuntime = {
30
30
  // Astro is SSR - always use non-reactive
31
31
  useReactive: () => false,
32
32
  reactive: {
33
- wrapInit: (expr) => expr,
34
- wrapUse: (expr) => expr,
33
+ wrapInit: expr => expr,
34
+ wrapUse: expr => expr,
35
35
  },
36
36
  plain: {
37
- wrapInit: (expr) => expr,
38
- wrapUse: (expr) => expr,
37
+ wrapInit: expr => expr,
38
+ wrapUse: expr => expr,
39
39
  },
40
40
  };
41
41
  const defaultArgs = {
@@ -3,6 +3,7 @@ import type * as Estree from 'acorn';
3
3
  import type { CatalogExpr, CodePattern, HeuristicFunc, IndexTracker, Message, RuntimeConf, TransformOutput, UrlMatcher } from 'wuchale';
4
4
  import { MixedVisitor } from 'wuchale/adapter-utils';
5
5
  import { Transformer } from 'wuchale/adapter-vanilla';
6
+ export declare function parseExpr(content: string): [Estree.Expression, Estree.Comment[][]];
6
7
  type MixedAstroNodes = Node;
7
8
  export declare class AstroTransformer extends Transformer {
8
9
  currentElement?: string;
@@ -20,7 +21,7 @@ export declare class AstroTransformer extends Transformer {
20
21
  end: number;
21
22
  };
22
23
  initMixedVisitor: () => MixedVisitor<Node>;
23
- _parseAndVisitExpr: (expr: string, startOffset: number, startFromProgram?: boolean) => Message[];
24
+ _parseAndVisitExpr: (expr: string, startOffset: number, asScript?: boolean) => Message[];
24
25
  visitexpression: (node: ExpressionNode) => Message[];
25
26
  _visitChildren: (nodes: Node[]) => Message[];
26
27
  visitFragmentNode: (node: FragmentNode) => Message[];
@@ -1,7 +1,14 @@
1
1
  import { parse } from '@astrojs/compiler';
2
+ import { tsPlugin } from '@sveltejs/acorn-typescript';
3
+ import { Parser } from 'acorn';
2
4
  import MagicString from 'magic-string';
3
5
  import { MixedVisitor, nonWhitespaceText } from 'wuchale/adapter-utils';
4
- import { parseScript, Transformer } from 'wuchale/adapter-vanilla';
6
+ import { parseScript, scriptParseOptionsWithComments, Transformer } from 'wuchale/adapter-vanilla';
7
+ const ExprParser = Parser.extend(tsPlugin());
8
+ export function parseExpr(content) {
9
+ const [opts, comments] = scriptParseOptionsWithComments();
10
+ return [ExprParser.parseExpressionAt(content, 0, opts), comments];
11
+ }
5
12
  // Astro nodes that can have children
6
13
  const nodesWithChildren = ['element', 'component', 'custom-element', 'fragment'];
7
14
  const rtRenderFunc = '_w_Tx_';
@@ -54,13 +61,13 @@ export class AstroTransformer extends Transformer {
54
61
  mstr: this.mstr,
55
62
  vars: this.vars,
56
63
  getRange: this.getRange,
57
- isText: (node) => node.type === 'text',
58
- isComment: (node) => node.type === 'comment',
59
- leaveInPlace: (node) => [''].includes(node.type),
60
- isExpression: (node) => node.type === 'expression',
64
+ isText: node => node.type === 'text',
65
+ isComment: node => node.type === 'comment',
66
+ leaveInPlace: node => [''].includes(node.type),
67
+ isExpression: node => node.type === 'expression',
61
68
  getTextContent: (node) => node.value,
62
69
  getCommentData: (node) => node.value.trim(),
63
- canHaveChildren: (node) => nodesWithChildren.includes(node.type),
70
+ canHaveChildren: node => nodesWithChildren.includes(node.type),
64
71
  visitFunc: (child, inCompoundText) => {
65
72
  const inCompoundTextPrev = this.inCompoundText;
66
73
  this.inCompoundText = inCompoundText;
@@ -103,18 +110,11 @@ export class AstroTransformer extends Transformer {
103
110
  this.mstr.appendRight(lastChildEnd, end);
104
111
  },
105
112
  });
106
- _parseAndVisitExpr = (expr, startOffset, startFromProgram = false) => {
107
- const [ast, comments] = parseScript(expr);
113
+ _parseAndVisitExpr = (expr, startOffset, asScript = false) => {
114
+ const [ast, comments] = (asScript ? parseScript : parseExpr)(expr);
108
115
  this.comments = comments;
109
116
  this.mstr.offset = startOffset;
110
- // not just visit Program because visitProgram sets insideProgram to true
111
- let msgs;
112
- if (startFromProgram) {
113
- msgs = this.visit(ast);
114
- }
115
- else {
116
- msgs = ast.body.flatMap(this.visit);
117
- }
117
+ const msgs = this.visit(ast);
118
118
  this.mstr.offset = 0; // restore
119
119
  return msgs;
120
120
  };
@@ -164,6 +164,9 @@ export class AstroTransformer extends Transformer {
164
164
  attribute: node.name,
165
165
  };
166
166
  let { start } = this.getRange(node);
167
+ if (node.kind === 'spread') {
168
+ return this._parseAndVisitExpr(node.name, start);
169
+ }
167
170
  if (node.kind !== 'empty') {
168
171
  start = this.content.indexOf('=', start) + 1;
169
172
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/astro",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Wuchale i18n adapter for Astro files",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",