@vue/compiler-sfc 3.2.7 → 3.2.11

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.
@@ -1438,6 +1438,10 @@ function compileScript(sfc, options) {
1438
1438
  `${DEFINE_PROPS} declaration.`, node);
1439
1439
  }
1440
1440
  propsRuntimeDefaults = node.arguments[1];
1441
+ if (!propsRuntimeDefaults ||
1442
+ propsRuntimeDefaults.type !== 'ObjectExpression') {
1443
+ error(`The 2nd argument of ${WITH_DEFAULTS} must be an object literal.`, propsRuntimeDefaults || node);
1444
+ }
1441
1445
  }
1442
1446
  else {
1443
1447
  error(`${WITH_DEFAULTS}' first argument must be a ${DEFINE_PROPS} call.`, node.arguments[0] || node);
@@ -1488,7 +1492,10 @@ function compileScript(sfc, options) {
1488
1492
  return isQualifiedType(node.declaration);
1489
1493
  }
1490
1494
  };
1491
- for (const node of scriptSetupAst.body) {
1495
+ const body = scriptAst
1496
+ ? [...scriptSetupAst.body, ...scriptAst.body]
1497
+ : scriptSetupAst.body;
1498
+ for (const node of body) {
1492
1499
  const qualified = isQualifiedType(node);
1493
1500
  if (qualified) {
1494
1501
  return qualified;
@@ -1528,17 +1535,24 @@ function compileScript(sfc, options) {
1528
1535
  s.overwrite(node.start + startOffset, node.argument.start + startOffset, `${isStatement ? `;` : ``}(([__temp,__restore]=${helper(`withAsyncContext`)}(()=>(`);
1529
1536
  s.appendLeft(node.end + startOffset, `))),__temp=await __temp,__restore()${isStatement ? `` : `,__temp`})`);
1530
1537
  }
1538
+ /**
1539
+ * check defaults. If the default object is an object literal with only
1540
+ * static properties, we can directly generate more optimzied default
1541
+ * declarations. Otherwise we will have to fallback to runtime merging.
1542
+ */
1543
+ function checkStaticDefaults() {
1544
+ return (propsRuntimeDefaults &&
1545
+ propsRuntimeDefaults.type === 'ObjectExpression' &&
1546
+ propsRuntimeDefaults.properties.every(node => (node.type === 'ObjectProperty' && !node.computed) ||
1547
+ node.type === 'ObjectMethod'));
1548
+ }
1531
1549
  function genRuntimeProps(props) {
1532
1550
  const keys = Object.keys(props);
1533
1551
  if (!keys.length) {
1534
1552
  return ``;
1535
1553
  }
1536
- // check defaults. If the default object is an object literal with only
1537
- // static properties, we can directly generate more optimzied default
1538
- // decalrations. Otherwise we will have to fallback to runtime merging.
1539
- const hasStaticDefaults = propsRuntimeDefaults &&
1540
- propsRuntimeDefaults.type === 'ObjectExpression' &&
1541
- propsRuntimeDefaults.properties.every(node => node.type === 'ObjectProperty' && !node.computed);
1554
+ const hasStaticDefaults = checkStaticDefaults();
1555
+ const scriptSetupSource = scriptSetup.content;
1542
1556
  let propsDecls = `{
1543
1557
  ${keys
1544
1558
  .map(key => {
@@ -1546,8 +1560,13 @@ function compileScript(sfc, options) {
1546
1560
  if (hasStaticDefaults) {
1547
1561
  const prop = propsRuntimeDefaults.properties.find((node) => node.key.name === key);
1548
1562
  if (prop) {
1549
- // prop has corresponding static default value
1550
- defaultString = `default: ${source.slice(prop.value.start + startOffset, prop.value.end + startOffset)}`;
1563
+ if (prop.type === 'ObjectProperty') {
1564
+ // prop has corresponding static default value
1565
+ defaultString = `default: ${scriptSetupSource.slice(prop.value.start, prop.value.end)}`;
1566
+ }
1567
+ else {
1568
+ defaultString = `default() ${scriptSetupSource.slice(prop.body.start, prop.body.end)}`;
1569
+ }
1551
1570
  }
1552
1571
  }
1553
1572
  {
@@ -1559,7 +1578,38 @@ function compileScript(sfc, options) {
1559
1578
  if (propsRuntimeDefaults && !hasStaticDefaults) {
1560
1579
  propsDecls = `${helper('mergeDefaults')}(${propsDecls}, ${source.slice(propsRuntimeDefaults.start + startOffset, propsRuntimeDefaults.end + startOffset)})`;
1561
1580
  }
1562
- return `\n props: ${propsDecls} as unknown as undefined,`;
1581
+ return `\n props: ${propsDecls},`;
1582
+ }
1583
+ function genSetupPropsType(node) {
1584
+ const scriptSetupSource = scriptSetup.content;
1585
+ if (checkStaticDefaults()) {
1586
+ // if withDefaults() is used, we need to remove the optional flags
1587
+ // on props that have default values
1588
+ let res = `{ `;
1589
+ const members = node.type === 'TSTypeLiteral' ? node.members : node.body;
1590
+ for (const m of members) {
1591
+ if ((m.type === 'TSPropertySignature' ||
1592
+ m.type === 'TSMethodSignature') &&
1593
+ m.typeAnnotation &&
1594
+ m.key.type === 'Identifier') {
1595
+ if (propsRuntimeDefaults.properties.some((p) => p.key.name === m.key.name)) {
1596
+ res +=
1597
+ m.key.name +
1598
+ (m.type === 'TSMethodSignature' ? '()' : '') +
1599
+ scriptSetupSource.slice(m.typeAnnotation.start, m.typeAnnotation.end) +
1600
+ ', ';
1601
+ }
1602
+ else {
1603
+ res +=
1604
+ scriptSetupSource.slice(m.start, m.typeAnnotation.end) + `, `;
1605
+ }
1606
+ }
1607
+ }
1608
+ return (res.length ? res.slice(0, -2) : res) + ` }`;
1609
+ }
1610
+ else {
1611
+ return scriptSetupSource.slice(node.start, node.end);
1612
+ }
1563
1613
  }
1564
1614
  // 1. process normal <script> first if it exists
1565
1615
  let scriptAst;
@@ -1756,7 +1806,7 @@ function compileScript(sfc, options) {
1756
1806
  }
1757
1807
  }
1758
1808
  }
1759
- // walk decalrations to record declared bindings
1809
+ // walk declarations to record declared bindings
1760
1810
  if ((node.type === 'VariableDeclaration' ||
1761
1811
  node.type === 'FunctionDeclaration' ||
1762
1812
  node.type === 'ClassDeclaration') &&
@@ -1879,17 +1929,20 @@ function compileScript(sfc, options) {
1879
1929
  // 9. finalize setup() argument signature
1880
1930
  let args = `__props`;
1881
1931
  if (propsTypeDecl) {
1882
- args += `: ${scriptSetup.content.slice(propsTypeDecl.start, propsTypeDecl.end)}`;
1932
+ // mark as any and only cast on assignment
1933
+ // since the user defined complex types may be incompatible with the
1934
+ // inferred type from generated runtime declarations
1935
+ args += `: any`;
1883
1936
  }
1884
1937
  // inject user assignment of props
1885
1938
  // we use a default __props so that template expressions referencing props
1886
1939
  // can use it directly
1887
1940
  if (propsIdentifier) {
1888
- s.prependRight(startOffset, `\nconst ${propsIdentifier} = __props`);
1941
+ s.prependRight(startOffset, `\nconst ${propsIdentifier} = __props${propsTypeDecl ? ` as ${genSetupPropsType(propsTypeDecl)}` : ``}`);
1889
1942
  }
1890
1943
  // inject temp variables for async context preservation
1891
1944
  if (hasAwait) {
1892
- const any = isTS ? `:any` : ``;
1945
+ const any = isTS ? `: any` : ``;
1893
1946
  s.prependRight(startOffset, `\nlet __temp${any}, __restore${any}\n`);
1894
1947
  }
1895
1948
  const destructureElements = hasDefineExposeCall || !options.inlineTemplate ? [`expose`] : [];
@@ -2230,6 +2283,7 @@ function inferRuntimeType(node, declaredTypes) {
2230
2283
  case 'Map':
2231
2284
  case 'WeakSet':
2232
2285
  case 'WeakMap':
2286
+ case 'Date':
2233
2287
  return [node.typeName.name];
2234
2288
  case 'Record':
2235
2289
  case 'Partial':
@@ -2297,7 +2351,7 @@ function genRuntimeEmits(emits) {
2297
2351
  return emits.size
2298
2352
  ? `\n emits: [${Array.from(emits)
2299
2353
  .map(p => JSON.stringify(p))
2300
- .join(', ')}] as unknown as undefined,`
2354
+ .join(', ')}],`
2301
2355
  : ``;
2302
2356
  }
2303
2357
  function isCallOf(node, test) {