@lwc/template-compiler 6.2.0 → 6.2.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.
package/dist/index.d.ts CHANGED
@@ -3,6 +3,18 @@ import { TemplateCompileResult, TemplateParseResult } from './shared/types';
3
3
  export * from './shared/types';
4
4
  export { CustomRendererConfig, CustomRendererElementConfig } from './shared/renderer-hooks';
5
5
  export { Config } from './config';
6
+ /**
7
+ * Parses HTML markup into an AST
8
+ * @param source HTML markup to parse
9
+ * @param config HTML template compilation config
10
+ * @returns Object containing the AST
11
+ */
6
12
  export declare function parse(source: string, config?: Config): TemplateParseResult;
7
13
  export { compile };
14
+ /**
15
+ * Compiles a LWC template to JavaScript source code consumable by the engine.
16
+ * @param source HTML markup to compile
17
+ * @param config HTML template compilation config
18
+ * @returns Object containing the compiled code and any warnings that occurred.
19
+ */
8
20
  export default function compile(source: string, config: Config): TemplateCompileResult;
package/dist/index.js CHANGED
@@ -54,7 +54,7 @@ const DASHED_TAGNAME_ELEMENT_SET = new Set([
54
54
  const STATIC_SAFE_DIRECTIVES = new Set(['Ref']);
55
55
 
56
56
  /*
57
- * Copyright (c) 2018, salesforce.com, inc.
57
+ * Copyright (c) 2024, Salesforce, Inc.
58
58
  * All rights reserved.
59
59
  * SPDX-License-Identifier: MIT
60
60
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
@@ -78,6 +78,7 @@ function toPropertyName(attr) {
78
78
  * Test if given tag name is a custom element.
79
79
  * @param tagName element tag name to test
80
80
  * @returns true if given tag name represents a custom element, false otherwise.
81
+ * @example isCustomElementTag("my-component") // true
81
82
  */
82
83
  function isCustomElementTag(tagName) {
83
84
  return tagName.includes('-') && !DASHED_TAGNAME_ELEMENT_SET.has(tagName);
@@ -86,6 +87,7 @@ function isCustomElementTag(tagName) {
86
87
  * Test if given tag name is a custom LWC tag denoted lwc:*.
87
88
  * @param tagName element tag name to test
88
89
  * @returns true if given tag name represents a custom LWC tag, false otherwise.
90
+ * @example isLwcElementTag("my-component") // false
89
91
  */
90
92
  function isLwcElementTag(tagName) {
91
93
  return tagName.startsWith('lwc:');
@@ -10132,6 +10134,8 @@ class ParserCtx {
10132
10134
  }
10133
10135
  /**
10134
10136
  * This method flattens the scopes into a single array for traversal.
10137
+ * @param element
10138
+ * @yields Each node in the scope and its parent.
10135
10139
  */
10136
10140
  *ancestors(element) {
10137
10141
  const ancestors = this.elementScopes.flat();
@@ -10144,11 +10148,10 @@ class ParserCtx {
10144
10148
  * This method returns an iterator over ancestor nodes, starting at the parent and ending at the root node.
10145
10149
  *
10146
10150
  * Note: There are instances when we want to terminate the traversal early, such as searching for a ForBlock parent.
10147
- *
10148
- * @param {ParentNode} startNode - Starting node to begin search, defaults to the tail of the current scope.
10149
- * @param {function} predicate - This callback is called once for each ancestor until it finds one where predicate returns true.
10150
- * @param {function} traversalCond - This callback is called after predicate and will terminate the traversal if it returns false.
10151
+ * @param predicate This callback is called once for each ancestor until it finds one where predicate returns true.
10152
+ * @param traversalCond This callback is called after predicate and will terminate the traversal if it returns false.
10151
10153
  * traversalCond is ignored if no value is provided.
10154
+ * @param startNode Starting node to begin search, defaults to the tail of the current scope.
10152
10155
  */
10153
10156
  findAncestor(predicate, traversalCond = () => true, startNode) {
10154
10157
  for (const { current, parent } of this.ancestors(startNode)) {
@@ -10163,8 +10166,7 @@ class ParserCtx {
10163
10166
  }
10164
10167
  /**
10165
10168
  * This method searchs the current scope and returns the value that satisfies the predicate.
10166
- *
10167
- * @param {function} predicate - This callback is called once for each sibling in the current scope
10169
+ * @param predicate This callback is called once for each sibling in the current scope
10168
10170
  * until it finds one where predicate returns true.
10169
10171
  */
10170
10172
  findInCurrentElementScope(predicate) {
@@ -10275,8 +10277,7 @@ class ParserCtx {
10275
10277
  /**
10276
10278
  * This method recovers from diagnostic errors that are encountered when fn is invoked.
10277
10279
  * All other errors are considered compiler errors and can not be recovered from.
10278
- *
10279
- * @param fn - method to be invoked.
10280
+ * @param fn method to be invoked.
10280
10281
  */
10281
10282
  withErrorRecovery(fn) {
10282
10283
  try {
@@ -10313,18 +10314,28 @@ class ParserCtx {
10313
10314
  }
10314
10315
  /**
10315
10316
  * This method throws a diagnostic error with the node's location.
10317
+ * @param errorInfo
10318
+ * @param node
10319
+ * @param messageArgs
10316
10320
  */
10317
10321
  throwOnNode(errorInfo, node, messageArgs) {
10318
10322
  this.throw(errorInfo, messageArgs, node.location);
10319
10323
  }
10320
10324
  /**
10321
10325
  * This method throws a diagnostic error with location information.
10326
+ * @param errorInfo
10327
+ * @param location
10328
+ * @param messageArgs
10322
10329
  */
10323
10330
  throwAtLocation(errorInfo, location, messageArgs) {
10324
10331
  this.throw(errorInfo, messageArgs, location);
10325
10332
  }
10326
10333
  /**
10327
10334
  * This method throws a diagnostic error and will immediately exit the current routine.
10335
+ * @param errorInfo
10336
+ * @param messageArgs
10337
+ * @param location
10338
+ * @throws
10328
10339
  */
10329
10340
  throw(errorInfo, messageArgs, location) {
10330
10341
  throw generateCompilerError(errorInfo, {
@@ -10336,18 +10347,27 @@ class ParserCtx {
10336
10347
  }
10337
10348
  /**
10338
10349
  * This method logs a diagnostic warning with the node's location.
10350
+ * @param errorInfo
10351
+ * @param node
10352
+ * @param messageArgs
10339
10353
  */
10340
10354
  warnOnNode(errorInfo, node, messageArgs) {
10341
10355
  this.warn(errorInfo, messageArgs, node.location);
10342
10356
  }
10343
10357
  /**
10344
10358
  * This method logs a diagnostic warning with location information.
10359
+ * @param errorInfo
10360
+ * @param location
10361
+ * @param messageArgs
10345
10362
  */
10346
10363
  warnAtLocation(errorInfo, location, messageArgs) {
10347
10364
  this.warn(errorInfo, messageArgs, location);
10348
10365
  }
10349
10366
  /**
10350
10367
  * This method logs a diagnostic warning and will continue execution of the current routine.
10368
+ * @param errorInfo
10369
+ * @param messageArgs
10370
+ * @param location
10351
10371
  */
10352
10372
  warn(errorInfo, messageArgs, location) {
10353
10373
  this.addDiagnostic(generateCompilerDiagnostic(errorInfo, {
@@ -10568,22 +10588,24 @@ function getTrailingChars(str) {
10568
10588
  * This function checks for "unbalanced" extraneous parentheses surrounding the expression.
10569
10589
  *
10570
10590
  * Examples of balanced extraneous parentheses (validation passes):
10571
- * {(foo.bar)} <-- the MemberExpressions does not account for the surrounding parens
10572
- * {(foo())} <-- the CallExpression does not account for the surrounding parens
10573
- * {((foo ?? bar)())} <-- the CallExpression does not account for the surrounding parens
10591
+ * - `{(foo.bar)}` <-- the MemberExpressions does not account for the surrounding parens
10592
+ * - `{(foo())}` <-- the CallExpression does not account for the surrounding parens
10593
+ * - `{((foo ?? bar)())}` <-- the CallExpression does not account for the surrounding parens
10574
10594
  *
10575
10595
  * Examples of unbalanced extraneous parentheses (validation fails):
10576
- * {(foo.bar))} <-- there is an extraneous trailing paren
10577
- * {foo())} <-- there is an extraneous trailing paren
10596
+ * - `{(foo.bar))}` <-- there is an extraneous trailing paren
10597
+ * - `{foo())}` <-- there is an extraneous trailing paren
10578
10598
  *
10579
10599
  * Examples of no extraneous parentheses (validation passes):
10580
- * {foo()} <-- the CallExpression accounts for the trailing paren
10581
- * {(foo ?? bar).baz} <-- the outer MemberExpression accounts for the leading paren
10582
- * {(foo).bar} <-- the outer MemberExpression accounts for the leading paren
10600
+ * - `{foo()}` <-- the CallExpression accounts for the trailing paren
10601
+ * - `{(foo ?? bar).baz}` <-- the outer MemberExpression accounts for the leading paren
10602
+ * - `{(foo).bar}` <-- the outer MemberExpression accounts for the leading paren
10583
10603
  *
10584
10604
  * Notably, no examples of extraneous leading parens could be found - these result in a
10585
10605
  * parsing error in Acorn. However, this function still checks, in case there is an
10586
10606
  * unknown expression that would parse with an extraneous leading paren.
10607
+ * @param leadingChars
10608
+ * @param trailingChars
10587
10609
  */
10588
10610
  function validateMatchingExtraParens(leadingChars, trailingChars) {
10589
10611
  const numLeadingParens = leadingChars.split('(').length - 1;
@@ -10595,8 +10617,8 @@ function validateMatchingExtraParens(leadingChars, trailingChars) {
10595
10617
  *
10596
10618
  * Its behavior diverges from that specified in the WHATWG HTML spec
10597
10619
  * in two places:
10598
- * - 13.2.5.38 - unquoted attribute values
10599
- * - 13.2.5.1 - the "data" state, which corresponds to parsing outside of tags
10620
+ * - 13.2.5.38 - unquoted attribute values
10621
+ * - 13.2.5.1 - the "data" state, which corresponds to parsing outside of tags
10600
10622
  *
10601
10623
  * Specifically, this tokenizer defers to Acorn's JavaScript parser when
10602
10624
  * encountering a `{` character for an attribute value or within a text
@@ -10747,11 +10769,9 @@ class TemplateHtmlParser extends Parser {
10747
10769
  /**
10748
10770
  * Parse the LWC template using a customized parser & lexer that allow
10749
10771
  * for template expressions to be parsed correctly.
10750
- *
10751
- * @param {string} source raw template markup
10752
- * @param {ParseFragmentConfig} config
10753
- *
10754
- * @return {DocumentFragment} the parsed document
10772
+ * @param source raw template markup
10773
+ * @param config
10774
+ * @returns the parsed document
10755
10775
  */
10756
10776
  function parseFragment(source, config) {
10757
10777
  const { ctx, sourceCodeLocationInfo = true, onParseError } = config;
@@ -11131,6 +11151,7 @@ function isTemplateDirective(attrName) {
11131
11151
  }
11132
11152
  /**
11133
11153
  * Convert attribute name from kebab case to camel case property name
11154
+ * @param attrName
11134
11155
  */
11135
11156
  function attributeToPropertyName(attrName) {
11136
11157
  return ATTRS_PROPS_TRANFORMS[attrName] || toPropertyName(attrName);
@@ -11259,6 +11280,10 @@ function parseRoot(ctx, parse5Elm) {
11259
11280
  *
11260
11281
  * Note: Not every node in the hierarchy is guaranteed to be created, for example,
11261
11282
  * <div></div> will only create an Element node.
11283
+ * @param ctx
11284
+ * @param parse5Elm
11285
+ * @param parentNode
11286
+ * @param parse5ParentLocation
11262
11287
  */
11263
11288
  function parseElement(ctx, parse5Elm, parentNode, parse5ParentLocation) {
11264
11289
  const parse5ElmLocation = parseElementLocation(ctx, parse5Elm, parse5ParentLocation);
@@ -12410,6 +12435,8 @@ function objectToAST(obj, valueMapper) {
12410
12435
  *
12411
12436
  * This function searches through the children to determine if flattening needs to occur in the runtime.
12412
12437
  * Children should be flattened if they contain an iterator, a dynamic directive or a slot inside a light dom element.
12438
+ * @param codeGen
12439
+ * @param children
12413
12440
  */
12414
12441
  function shouldFlatten(codeGen, children) {
12415
12442
  return children.some((child) => {
@@ -12427,6 +12454,7 @@ function shouldFlatten(codeGen, children) {
12427
12454
  }
12428
12455
  /**
12429
12456
  * Returns true if the AST element or any of its descendants use an id attribute.
12457
+ * @param node
12430
12458
  */
12431
12459
  function hasIdAttribute(node) {
12432
12460
  if (isBaseElement(node)) {
@@ -12601,6 +12629,7 @@ const rawContentElements = new Set([
12601
12629
  /**
12602
12630
  * Escape all the characters that could break a JavaScript template string literal: "`" (backtick),
12603
12631
  * "${" (dollar + open curly) and "\" (backslash).
12632
+ * @param str
12604
12633
  */
12605
12634
  function templateStringEscape(str) {
12606
12635
  return str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\${');
@@ -12706,21 +12735,23 @@ function serializeStaticElement(element, preserveComments) {
12706
12735
  /**
12707
12736
  * Bind the passed expression to the component instance. It applies the following
12708
12737
  * transformation to the expression:
12709
- * {value} --> {$cmp.value}
12710
- * {value[index]} --> {$cmp.value[$cmp.index]}
12711
- * {foo ?? bar} --> {$cmp.foo ?? $cmp.bar}
12712
- * {foo?.bar} --> {$cmp.foo?.bar}
12738
+ * - `{value}` --> `{$cmp.value}`
12739
+ * - `{value[index]}` --> `{$cmp.value[$cmp.index]}`
12740
+ * - `{foo ?? bar}` --> `{$cmp.foo ?? $cmp.bar}`
12741
+ * - `{foo?.bar}` --> `{$cmp.foo?.bar}`
12713
12742
  *
12714
12743
  * However, parameter variables are not be transformed in this way. For example,
12715
12744
  * the following transformations do not happen:
12716
- * {(foo) => foo && bar} -> {(foo) => $cmp.foo && $cmp.bar}
12717
- * {(foo) => foo && bar} -> {($cmp.foo) => foo && $cmp.bar}
12718
- * {(foo) => foo && bar} -> {($cmp.foo) => $cmp.foo && $cmp.bar}
12745
+ * - `{(foo) => foo && bar}` --> `{(foo) => $cmp.foo && $cmp.bar}`
12746
+ * - `{(foo) => foo && bar}` --> `{($cmp.foo) => foo && $cmp.bar}`
12747
+ * - `{(foo) => foo && bar}` --> `{($cmp.foo) => $cmp.foo && $cmp.bar}`
12719
12748
  *
12720
12749
  * Instead, the scopes are respected:
12721
- * {(foo) => foo && $cmp.bar}
12750
+ * - `{(foo) => foo && $cmp.bar}`
12722
12751
  *
12723
12752
  * Similar checks occur for local identifiers introduced via for:each or similar.
12753
+ * @param expression
12754
+ * @param codeGen
12724
12755
  */
12725
12756
  function bindComplexExpression(expression, codeGen) {
12726
12757
  const expressionScopes = new ExpressionScopes();
@@ -12984,6 +13015,9 @@ class CodeGen {
12984
13015
  }
12985
13016
  /**
12986
13017
  * Generates childs vnodes when slot content is static.
13018
+ * @param slotName
13019
+ * @param data
13020
+ * @param children
12987
13021
  */
12988
13022
  getSlot(slotName, data, children) {
12989
13023
  this.slotNames.add(slotName);
@@ -12996,6 +13030,8 @@ class CodeGen {
12996
13030
  }
12997
13031
  /**
12998
13032
  * Generates a factory function that inturn generates child vnodes for scoped slot content.
13033
+ * @param callback
13034
+ * @param slotName
12999
13035
  */
13000
13036
  getScopedSlotFactory(callback, slotName) {
13001
13037
  return this._renderApiCall(RENDER_APIS.scopedSlotFactory, [slotName, callback]);
@@ -13029,9 +13065,8 @@ class CodeGen {
13029
13065
  * This routine generates an expression that avoids
13030
13066
  * computing the sanitized html of a raw html if it does not change
13031
13067
  * between renders.
13032
- *
13033
13068
  * @param expr
13034
- * @returns sanitizedHtmlExpr
13069
+ * @returns The generated expression
13035
13070
  */
13036
13071
  genSanitizedHtmlExpr(expr) {
13037
13072
  const instance = this.innerHtmlInstances++;
@@ -13081,6 +13116,7 @@ class CodeGen {
13081
13116
  }
13082
13117
  /**
13083
13118
  * Searches the scopes to find an identifier with a matching name.
13119
+ * @param identifier
13084
13120
  */
13085
13121
  isLocalIdentifier(identifier) {
13086
13122
  let scope = this.scope;
@@ -13096,6 +13132,7 @@ class CodeGen {
13096
13132
  * Bind the passed expression to the component instance. It applies the following transformation to the expression:
13097
13133
  * - {value} --> {$cmp.value}
13098
13134
  * - {value[index]} --> {$cmp.value[$cmp.index]}
13135
+ * @param expression
13099
13136
  */
13100
13137
  bindExpression(expression) {
13101
13138
  if (isIdentifier(expression)) {
@@ -13277,6 +13314,7 @@ function kebabcaseToCamelcase(name) {
13277
13314
  * };
13278
13315
  * }
13279
13316
  * ```
13317
+ * @param templateFn
13280
13318
  */
13281
13319
  function optimizeStaticExpressions(templateFn) {
13282
13320
  const result = [];
@@ -13359,7 +13397,8 @@ function generateHoistedNodes(codegen) {
13359
13397
  * Generate an ES module AST from a template ESTree AST. The generated module imports the dependent
13360
13398
  * LWC components via import statements and expose the template function via a default export
13361
13399
  * statement.
13362
- *
13400
+ * @param templateFn
13401
+ * @param codeGen
13363
13402
  * @example
13364
13403
  * ```js
13365
13404
  * import { registerTemplate } from 'lwc';
@@ -13538,7 +13577,6 @@ function transform(codeGen) {
13538
13577
  }
13539
13578
  /**
13540
13579
  * Transforms an IfBlock or ElseifBlock along with both its direct descendants and its 'else' descendants.
13541
- *
13542
13580
  * @param conditionalParentBlock The IfBlock or ElseifBlock to transform into a conditional expression
13543
13581
  * @param key The key to use for this chain of IfBlock/ElseifBlock branches, if applicable
13544
13582
  * @returns A conditional expression representing the full conditional tree with conditionalParentBlock as the root node
@@ -13906,16 +13944,28 @@ function generate (root, state) {
13906
13944
  }
13907
13945
 
13908
13946
  /*
13909
- * Copyright (c) 2018, salesforce.com, inc.
13947
+ * Copyright (c) 2024, Salesforce, Inc.
13910
13948
  * All rights reserved.
13911
13949
  * SPDX-License-Identifier: MIT
13912
13950
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
13913
13951
  */
13952
+ /**
13953
+ * Parses HTML markup into an AST
13954
+ * @param source HTML markup to parse
13955
+ * @param config HTML template compilation config
13956
+ * @returns Object containing the AST
13957
+ */
13914
13958
  function parse(source, config = {}) {
13915
13959
  const options = normalizeConfig(config);
13916
13960
  const state = new State$1(options);
13917
13961
  return parse$1(source, state);
13918
13962
  }
13963
+ /**
13964
+ * Compiles a LWC template to JavaScript source code consumable by the engine.
13965
+ * @param source HTML markup to compile
13966
+ * @param config HTML template compilation config
13967
+ * @returns Object containing the compiled code and any warnings that occurred.
13968
+ */
13919
13969
  function compile(source, config) {
13920
13970
  const options = normalizeConfig(config);
13921
13971
  const state = new State$1(options);
@@ -13944,5 +13994,5 @@ function compile(source, config) {
13944
13994
  }
13945
13995
 
13946
13996
  export { ElementDirectiveName, LWCDirectiveDomMode, LWCDirectiveRenderMode, LwcTagName, RootDirectiveName, TemplateDirectiveName, compile, compile as default, parse };
13947
- /** version: 6.2.0 */
13997
+ /** version: 6.2.1 */
13948
13998
  //# sourceMappingURL=index.js.map