@lwc/template-compiler 2.7.2 → 2.9.0

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.
Files changed (58) hide show
  1. package/dist/commonjs/codegen/codegen.js +89 -16
  2. package/dist/commonjs/codegen/codegen.js.map +1 -1
  3. package/dist/commonjs/codegen/formatters/function.js +3 -1
  4. package/dist/commonjs/codegen/formatters/function.js.map +1 -1
  5. package/dist/commonjs/codegen/formatters/module.js +3 -1
  6. package/dist/commonjs/codegen/formatters/module.js.map +1 -1
  7. package/dist/commonjs/codegen/helpers.js +34 -20
  8. package/dist/commonjs/codegen/helpers.js.map +1 -1
  9. package/dist/commonjs/codegen/index.js +184 -194
  10. package/dist/commonjs/codegen/index.js.map +1 -1
  11. package/dist/commonjs/codegen/optimize.js +115 -0
  12. package/dist/commonjs/codegen/optimize.js.map +1 -0
  13. package/dist/commonjs/index.js +12 -5
  14. package/dist/commonjs/index.js.map +1 -1
  15. package/dist/commonjs/parser/attribute.js +5 -7
  16. package/dist/commonjs/parser/attribute.js.map +1 -1
  17. package/dist/commonjs/parser/constants.js +1 -10
  18. package/dist/commonjs/parser/constants.js.map +1 -1
  19. package/dist/commonjs/parser/expression.js +5 -2
  20. package/dist/commonjs/parser/expression.js.map +1 -1
  21. package/dist/commonjs/parser/html.js +18 -1
  22. package/dist/commonjs/parser/html.js.map +1 -1
  23. package/dist/commonjs/parser/index.js +371 -342
  24. package/dist/commonjs/parser/index.js.map +1 -1
  25. package/dist/commonjs/parser/parse5Errors.js +77 -0
  26. package/dist/commonjs/parser/parse5Errors.js.map +1 -0
  27. package/dist/commonjs/parser/parser.js +85 -30
  28. package/dist/commonjs/parser/parser.js.map +1 -1
  29. package/dist/commonjs/shared/ast.js +303 -0
  30. package/dist/commonjs/shared/ast.js.map +1 -0
  31. package/dist/commonjs/shared/estree.js +9 -1
  32. package/dist/commonjs/shared/estree.js.map +1 -1
  33. package/dist/commonjs/shared/parse5.js +1 -15
  34. package/dist/commonjs/shared/parse5.js.map +1 -1
  35. package/dist/commonjs/shared/types.js +1 -7
  36. package/dist/commonjs/shared/types.js.map +1 -1
  37. package/dist/types/codegen/codegen.d.ts +25 -3
  38. package/dist/types/codegen/helpers.d.ts +11 -5
  39. package/dist/types/codegen/index.d.ts +2 -2
  40. package/dist/types/codegen/optimize.d.ts +33 -0
  41. package/dist/types/index.d.ts +1 -2
  42. package/dist/types/parser/attribute.d.ts +7 -7
  43. package/dist/types/parser/constants.d.ts +0 -9
  44. package/dist/types/parser/expression.d.ts +3 -4
  45. package/dist/types/parser/parse5Errors.d.ts +2 -0
  46. package/dist/types/parser/parser.d.ts +57 -28
  47. package/dist/types/shared/ast.d.ts +45 -0
  48. package/dist/types/shared/estree.d.ts +2 -2
  49. package/dist/types/shared/parse5.d.ts +0 -1
  50. package/dist/types/shared/types.d.ts +129 -86
  51. package/package.json +4 -4
  52. package/LICENSE +0 -10
  53. package/dist/commonjs/codegen/scope.js +0 -61
  54. package/dist/commonjs/codegen/scope.js.map +0 -1
  55. package/dist/commonjs/shared/ir.js +0 -90
  56. package/dist/commonjs/shared/ir.js.map +0 -1
  57. package/dist/types/codegen/scope.d.ts +0 -8
  58. package/dist/types/shared/ir.d.ts +0 -15
@@ -1,28 +1,59 @@
1
- import * as parse5 from 'parse5';
2
1
  import { CompilerDiagnostic, LWCErrorInfo } from '@lwc/errors';
3
- import { IRElement, LWCDirectiveRenderMode, IRBaseAttribute, IRNode } from '../shared/types';
4
2
  import { ResolvedConfig } from '../config';
3
+ import { Root, SourceLocation, ParentNode, BaseNode, LWCDirectiveRenderMode } from '../shared/types';
4
+ interface ParentWrapper {
5
+ parent: ParentNode | null;
6
+ current: ParentNode;
7
+ }
5
8
  export default class ParserCtx {
6
9
  private readonly source;
7
10
  readonly config: ResolvedConfig;
8
11
  readonly warnings: CompilerDiagnostic[];
9
12
  readonly seenIds: Set<string>;
10
13
  readonly seenSlots: Set<string>;
11
- readonly parentStack: IRElement[];
14
+ /**
15
+ * Scopes keep track of the hierarchy of ParentNodes as the parser traverses the parse5 AST.
16
+ * Each scope is represented by an array where each node in the array correspond to either
17
+ * a ForEach, ForOf, If, Element, Component, or Slot.
18
+ *
19
+ * Currently, each scope has a hierarchy of ForBlock > IfBlock > Element | Component | Slot.
20
+ * Note: Not all scopes will have all three, but when they do, they will appear in this order.
21
+ * We do not keep track of template nodes.
22
+ *
23
+ * Each scope corresponds to the original parse5.Element node.
24
+ */
25
+ private readonly scopes;
26
+ renderMode: LWCDirectiveRenderMode;
27
+ preserveComments: boolean;
12
28
  constructor(source: String, config: ResolvedConfig);
13
29
  getSource(start: number, end: number): string;
14
- ancestors(element?: IRElement): Generator<{
15
- current: IRElement;
16
- index: number;
17
- }, void, unknown>;
18
- findAncestor(args: {
19
- element?: IRElement;
20
- predicate: (elm: IRElement) => unknown;
21
- traversalCond?: (nodes: {
22
- current: IRElement;
23
- parent: IRElement | null;
24
- }) => unknown;
25
- }): IRElement | null;
30
+ setRootDirective(root: Root): void;
31
+ /**
32
+ * This method flattens the scopes into a single array for traversal.
33
+ */
34
+ ancestors(element?: ParentNode): IterableIterator<ParentWrapper>;
35
+ /**
36
+ * This method returns an iterator over ancestor nodes, starting at the parent and ending at the root node.
37
+ *
38
+ * Note: There are instances when we want to terminate the traversal early, such as searching for a ForBlock parent.
39
+ *
40
+ * @param {ParentNode} startNode - Starting node to begin search, defaults to the tail of the current scope.
41
+ * @param {function} predicate - This callback is called once for each ancestor until it finds one where predicate returns true.
42
+ * @param {function} traversalCond - This callback is called after predicate and will terminate the traversal if it returns false.
43
+ * traversalCond is ignored if no value is provided.
44
+ */
45
+ findAncestor<A extends ParentNode>(predicate: (node: ParentNode) => node is A, traversalCond?: (nodes: ParentWrapper) => unknown, startNode?: ParentNode): A | null;
46
+ /**
47
+ * This method searchs the current scope and returns the value that satisfies the predicate.
48
+ *
49
+ * @param {function} predicate - This callback is called once for each sibling in the current scope
50
+ * until it finds one where predicate returns true.
51
+ */
52
+ findSibling<A extends ParentNode>(predicate: (node: ParentNode) => node is A): A | null;
53
+ beginScope(): void;
54
+ endScope(): void;
55
+ addNodeCurrentScope(node: ParentNode): void;
56
+ private currentScope;
26
57
  /**
27
58
  * This method recovers from diagnostic errors that are encountered when fn is invoked.
28
59
  * All other errors are considered compiler errors and can not be recovered from.
@@ -30,34 +61,32 @@ export default class ParserCtx {
30
61
  * @param fn - method to be invoked.
31
62
  */
32
63
  withErrorRecovery<T>(fn: () => T): T | undefined;
33
- withErrorWrapping<T>(fn: () => T, errorInfo: LWCErrorInfo, location: parse5.Location, msgFormatter?: (error: any) => string): T;
34
- throwOnError(errorInfo: LWCErrorInfo, error: any, location?: parse5.Location): never;
64
+ withErrorWrapping<T>(fn: () => T, errorInfo: LWCErrorInfo, location: SourceLocation, msgFormatter?: (error: any) => string): T;
65
+ throwOnError(errorInfo: LWCErrorInfo, error: any, location?: SourceLocation): never;
35
66
  /**
36
- * This method throws a diagnostic error with the IRNode's location.
67
+ * This method throws a diagnostic error with the node's location.
37
68
  */
38
- throwOnIRNode(errorInfo: LWCErrorInfo, irNode: IRNode | IRBaseAttribute, messageArgs?: any[]): never;
69
+ throwOnNode(errorInfo: LWCErrorInfo, node: BaseNode, messageArgs?: any[]): never;
39
70
  /**
40
71
  * This method throws a diagnostic error with location information.
41
72
  */
42
- throwAtLocation(errorInfo: LWCErrorInfo, location: parse5.Location, messageArgs?: any[]): never;
73
+ throwAtLocation(errorInfo: LWCErrorInfo, location: SourceLocation, messageArgs?: any[]): never;
43
74
  /**
44
75
  * This method throws a diagnostic error and will immediately exit the current routine.
45
76
  */
46
- throw(errorInfo: LWCErrorInfo, messageArgs?: any[], location?: parse5.Location): never;
77
+ throw(errorInfo: LWCErrorInfo, messageArgs?: any[], location?: SourceLocation): never;
47
78
  /**
48
- * This method logs a diagnostic warning with the IRNode's location.
79
+ * This method logs a diagnostic warning with the node's location.
49
80
  */
50
- warnOnIRNode(errorInfo: LWCErrorInfo, irNode: IRNode | IRBaseAttribute, messageArgs?: any[]): void;
81
+ warnOnNode(errorInfo: LWCErrorInfo, node: BaseNode, messageArgs?: any[]): void;
51
82
  /**
52
83
  * This method logs a diagnostic warning with location information.
53
84
  */
54
- warnAtLocation(errorInfo: LWCErrorInfo, location: parse5.Location, messageArgs?: any[]): void;
85
+ warnAtLocation(errorInfo: LWCErrorInfo, location: SourceLocation, messageArgs?: any[]): void;
55
86
  /**
56
87
  * This method logs a diagnostic warning and will continue execution of the current routine.
57
88
  */
58
- warn(errorInfo: LWCErrorInfo, messageArgs?: any[], location?: parse5.Location): void;
89
+ warn(errorInfo: LWCErrorInfo, messageArgs?: any[], location?: SourceLocation): void;
59
90
  private addDiagnostic;
60
- getRoot(element: IRElement): IRElement;
61
- getRenderMode(element: IRElement): LWCDirectiveRenderMode;
62
- getPreserveComments(element: IRElement): boolean;
63
91
  }
92
+ export {};
@@ -0,0 +1,45 @@
1
+ import * as parse5 from 'parse5';
2
+ import { Literal, SourceLocation, Element, Component, Expression, Comment, Text, ForEach, ForBlock, Slot, Identifier, Root, EventListener, KeyDirective, DynamicDirective, DomDirective, PreserveCommentsDirective, RenderModeDirective, Attribute, Property, ParentNode, BaseNode, ForOf, LWCDirectiveRenderMode, If, ElementSourceLocation, InnerHTMLDirective, Directive, BaseElement, LWCDirectiveDomMode } from './types';
3
+ export declare function root(parse5ElmLocation: parse5.ElementLocation): Root;
4
+ export declare function element(parse5Elm: parse5.Element, parse5ElmLocation: parse5.ElementLocation): Element;
5
+ export declare function component(parse5Elm: parse5.Element, parse5ElmLocation: parse5.ElementLocation): Component;
6
+ export declare function slot(slotName: string, parse5ElmLocation: parse5.ElementLocation): Slot;
7
+ export declare function text(value: Literal | Expression, parse5Location: parse5.Location): Text;
8
+ export declare function comment(value: string, parse5Location: parse5.Location): Comment;
9
+ export declare function elementSourceLocation(parse5ElmLocation: parse5.ElementLocation): ElementSourceLocation;
10
+ export declare function sourceLocation(location: parse5.Location): SourceLocation;
11
+ export declare function literal<T extends string | boolean>(value: T): Literal<T>;
12
+ export declare function forEach(expression: Expression, elementLocation: SourceLocation, directiveLocation: SourceLocation, item: Identifier, index?: Identifier): ForEach;
13
+ export declare function forOf(expression: Expression, iterator: Identifier, elementLocation: SourceLocation, directiveLocation: SourceLocation): ForOf;
14
+ export declare function ifNode(modifier: string, condition: Expression, elementLocation: SourceLocation, directiveLocation: SourceLocation): If;
15
+ export declare function eventListener(name: string, handler: Expression, location: SourceLocation): EventListener;
16
+ export declare function keyDirective(value: Expression, location: SourceLocation): KeyDirective;
17
+ export declare function dynamicDirective(value: Expression, location: SourceLocation): DynamicDirective;
18
+ export declare function domDirective(lwcDomAttr: LWCDirectiveDomMode, location: SourceLocation): DomDirective;
19
+ export declare function innerHTMLDirective(value: Expression | Literal<string>, location: SourceLocation): InnerHTMLDirective;
20
+ export declare function preserveCommentsDirective(preserveComments: boolean, location: SourceLocation): PreserveCommentsDirective;
21
+ export declare function renderModeDirective(renderMode: LWCDirectiveRenderMode, location: SourceLocation): RenderModeDirective;
22
+ export declare function attribute(name: string, value: Expression | Literal, location: SourceLocation): Attribute;
23
+ export declare function property(name: string, attributeName: string, value: Expression | Literal, location: SourceLocation): Property;
24
+ export declare function isElement(node: BaseNode): node is Element;
25
+ export declare function isRoot(node: BaseNode): node is Root;
26
+ export declare function isComponent(node: BaseNode): node is Component;
27
+ export declare function isSlot(node: BaseNode): node is Slot;
28
+ export declare function isBaseElement(node: BaseNode): node is BaseElement;
29
+ export declare function isText(node: BaseNode): node is Text;
30
+ export declare function isComment(node: BaseNode): node is Comment;
31
+ export declare function isExpression(node: Expression | Literal): node is Expression;
32
+ export declare function isStringLiteral(node: Expression | Literal): node is Literal<string>;
33
+ export declare function isBooleanLiteral(node: Expression | Literal): node is Literal<boolean>;
34
+ export declare function isForOf(node: BaseNode): node is ForOf;
35
+ export declare function isForEach(node: BaseNode): node is ForEach;
36
+ export declare function isForBlock(node: BaseNode): node is ForBlock;
37
+ export declare function isIf(node: BaseNode): node is If;
38
+ export declare function isParentNode(node: BaseNode): node is ParentNode;
39
+ export declare function isDynamicDirective(directive: Directive): directive is DynamicDirective;
40
+ export declare function isDomDirective(directive: Directive): directive is DomDirective;
41
+ export declare function isInnerHTMLDirective(directive: Directive): directive is InnerHTMLDirective;
42
+ export declare function isKeyDirective(directive: Directive): directive is KeyDirective;
43
+ export declare function isRenderModeDirective(directive: Directive): directive is RenderModeDirective;
44
+ export declare function isPreserveCommentsDirective(directive: Directive): directive is PreserveCommentsDirective;
45
+ export declare function isProperty(node: BaseNode): node is Property;
@@ -2,6 +2,8 @@ import type * as t from 'estree';
2
2
  export declare function isIdentifier(node: t.BaseNode): node is t.Identifier;
3
3
  export declare function isMemberExpression(node: t.BaseNode): node is t.MemberExpression;
4
4
  export declare function isArrayExpression(node: t.BaseNode): node is t.ArrayExpression;
5
+ export declare function isObjectExpression(node: t.BaseNode): node is t.ObjectExpression;
6
+ export declare function isProperty(node: t.BaseNode): node is t.Property;
5
7
  export declare function identifier(name: string, config?: Partial<t.Identifier>): t.Identifier;
6
8
  export declare function isLiteral(node: t.BaseNode): node is t.Literal;
7
9
  export declare function memberExpression(object: t.MemberExpression['object'], property: t.MemberExpression['property'], config?: Partial<t.MemberExpression>): t.MemberExpression;
@@ -52,8 +54,6 @@ export declare type BlockStatement = t.BlockStatement;
52
54
  export declare type ReturnStatement = t.ReturnStatement;
53
55
  export declare type VariableDeclarator = t.VariableDeclarator;
54
56
  export declare type VariableDeclaration = t.VariableDeclaration;
55
- export declare type TemplateLiteral = t.TemplateLiteral;
56
- export declare type TemplateElement = t.TemplateElement;
57
57
  export declare type ImportDeclaration = t.ImportDeclaration;
58
58
  export declare type ImportDefaultSpecifier = t.ImportDefaultSpecifier;
59
59
  export declare type ImportSpecifier = t.ImportSpecifier;
@@ -3,4 +3,3 @@ export declare function isElementNode(node: parse5.Node): node is parse5.Element
3
3
  export declare function isCommentNode(node: parse5.Node): node is parse5.CommentNode;
4
4
  export declare function isTextNode(node: parse5.Node): node is parse5.TextNode;
5
5
  export declare function getTemplateContent(templateElement: parse5.Element): parse5.DocumentFragment | undefined;
6
- export declare function createEmptyElementLocation(): parse5.ElementLocation;
@@ -1,36 +1,12 @@
1
- import * as parse5 from 'parse5';
2
1
  import { CompilerDiagnostic } from '@lwc/errors';
3
- export declare type TemplateIdentifier = {
4
- type: 'Identifier';
5
- name: string;
6
- };
7
- export declare type TemplateExpression = {
8
- type: 'MemberExpression';
9
- object: TemplateExpression;
10
- property: TemplateExpression;
11
- computed: boolean;
12
- optional: boolean;
13
- } | {
14
- type: 'Literal';
15
- value: string | number | boolean | null;
16
- } | TemplateIdentifier;
17
2
  export declare type TemplateCompileResult = {
18
3
  code: string;
19
4
  warnings: CompilerDiagnostic[];
20
5
  };
21
6
  export declare type TemplateParseResult = {
22
- root?: IRElement | undefined;
7
+ root?: Root;
23
8
  warnings: CompilerDiagnostic[];
24
9
  };
25
- export interface ForEach {
26
- expression: TemplateExpression;
27
- item: TemplateIdentifier;
28
- index?: TemplateIdentifier;
29
- }
30
- export interface ForIterator {
31
- expression: TemplateExpression;
32
- iterator: TemplateIdentifier;
33
- }
34
10
  export declare enum LWCDirectiveDomMode {
35
11
  manual = "manual"
36
12
  }
@@ -38,74 +14,141 @@ export declare enum LWCDirectiveRenderMode {
38
14
  shadow = "shadow",
39
15
  light = "light"
40
16
  }
41
- export interface LWCDirectiveDynamic {
42
- prop: string;
17
+ export interface BaseNode {
18
+ type: string;
19
+ location: SourceLocation;
43
20
  }
44
- export interface LWCDirectives {
45
- dom?: LWCDirectiveDomMode;
46
- dynamic?: TemplateExpression;
47
- renderMode?: LWCDirectiveRenderMode;
48
- preserveComments?: IRBooleanAttribute;
49
- innerHTML?: TemplateExpression | string;
21
+ export interface SourceLocation {
22
+ startLine: number;
23
+ startColumn: number;
24
+ endLine: number;
25
+ endColumn: number;
26
+ start: number;
27
+ end: number;
50
28
  }
51
- export interface IRBaseNode<N extends parse5.Node> {
52
- type: string;
53
- location: parse5.Location;
54
- __original?: N;
55
- }
56
- export interface IRElement extends IRBaseNode<parse5.Element> {
57
- type: 'element';
58
- tag: string;
59
- namespace: string;
60
- children: IRNode[];
61
- location: parse5.ElementLocation;
62
- component?: string;
63
- on?: {
64
- [name: string]: TemplateExpression;
65
- };
66
- attrs?: {
67
- [name: string]: IRAttribute;
68
- };
69
- props?: {
70
- [name: string]: IRAttribute;
71
- };
72
- if?: TemplateExpression;
73
- ifModifier?: string;
74
- forEach?: ForEach;
75
- forOf?: ForIterator;
76
- forKey?: TemplateExpression;
77
- lwc?: LWCDirectives;
78
- slotName?: string;
79
- }
80
- export interface IRText extends IRBaseNode<parse5.TextNode> {
81
- type: 'text';
82
- value: string | TemplateExpression;
83
- }
84
- export interface IRComment extends IRBaseNode<parse5.CommentNode> {
85
- type: 'comment';
86
- value: string;
29
+ export interface ElementSourceLocation extends SourceLocation {
30
+ startTag: SourceLocation;
31
+ endTag?: SourceLocation;
32
+ }
33
+ export interface Literal<Value = string | boolean> {
34
+ type: 'Literal';
35
+ value: Value;
36
+ }
37
+ export interface Identifier extends BaseNode {
38
+ type: 'Identifier';
39
+ name: string;
40
+ }
41
+ export interface MemberExpression extends BaseNode {
42
+ type: 'MemberExpression';
43
+ object: Expression;
44
+ property: Identifier;
45
+ }
46
+ export declare type Expression = Identifier | MemberExpression;
47
+ export interface Attribute extends BaseNode {
48
+ type: 'Attribute';
49
+ name: string;
50
+ value: Literal | Expression;
87
51
  }
88
- export declare type IRNode = IRComment | IRElement | IRText;
89
- export declare enum IRAttributeType {
90
- Expression = 0,
91
- String = 1,
92
- Boolean = 2
52
+ export interface Property extends BaseNode {
53
+ type: 'Property';
54
+ name: string;
55
+ attributeName: string;
56
+ value: Literal | Expression;
57
+ }
58
+ export interface EventListener extends BaseNode {
59
+ type: 'EventListener';
60
+ name: string;
61
+ handler: Expression;
93
62
  }
94
- export interface IRBaseAttribute {
63
+ export interface Directive extends BaseNode {
64
+ type: 'Directive';
95
65
  name: string;
96
- location: parse5.Location;
97
- type: IRAttributeType;
66
+ value: Expression | Literal;
67
+ }
68
+ export interface KeyDirective extends Directive {
69
+ name: 'Key';
70
+ value: Expression;
71
+ }
72
+ export interface DynamicDirective extends Directive {
73
+ name: 'Dynamic';
74
+ value: Expression;
98
75
  }
99
- export interface IRExpressionAttribute extends IRBaseAttribute {
100
- type: IRAttributeType.Expression;
101
- value: TemplateExpression;
76
+ export interface DomDirective extends Directive {
77
+ name: 'Dom';
78
+ value: Literal<'manual'>;
102
79
  }
103
- export interface IRStringAttribute extends IRBaseAttribute {
104
- type: IRAttributeType.String;
80
+ export interface InnerHTMLDirective extends Directive {
81
+ name: `InnerHTML`;
82
+ value: Expression | Literal<string>;
83
+ }
84
+ export interface RenderModeDirective extends Directive {
85
+ name: 'RenderMode';
86
+ value: Literal<LWCDirectiveRenderMode>;
87
+ }
88
+ export interface PreserveCommentsDirective extends Directive {
89
+ name: 'PreserveComments';
90
+ value: Literal<boolean>;
91
+ }
92
+ export declare type ElementDirective = KeyDirective | DynamicDirective | DomDirective | InnerHTMLDirective;
93
+ export declare type RootDirective = RenderModeDirective | PreserveCommentsDirective;
94
+ export interface Text extends BaseNode {
95
+ type: 'Text';
96
+ value: Literal | Expression;
97
+ }
98
+ export interface Comment extends BaseNode {
99
+ type: 'Comment';
105
100
  value: string;
106
101
  }
107
- export interface IRBooleanAttribute extends IRBaseAttribute {
108
- type: IRAttributeType.Boolean;
109
- value: true;
102
+ export interface BaseParentNode extends BaseNode {
103
+ children: ChildNode[];
104
+ }
105
+ export interface AbstractBaseElement extends BaseParentNode {
106
+ name: string;
107
+ location: ElementSourceLocation;
108
+ properties: Property[];
109
+ attributes: Attribute[];
110
+ listeners: EventListener[];
111
+ directives: ElementDirective[];
112
+ namespace?: string;
113
+ }
114
+ export interface Element extends AbstractBaseElement {
115
+ type: 'Element';
116
+ }
117
+ export interface Component extends AbstractBaseElement {
118
+ type: 'Component';
119
+ }
120
+ export interface Slot extends AbstractBaseElement {
121
+ type: 'Slot';
122
+ /** Specifies slot element name. An empty string value maps to the default slot. */
123
+ slotName: string;
124
+ }
125
+ export declare type BaseElement = Element | Component | Slot;
126
+ export interface Root extends BaseParentNode {
127
+ type: 'Root';
128
+ location: ElementSourceLocation;
129
+ directives: RootDirective[];
130
+ }
131
+ interface DirectiveParentNode extends BaseParentNode {
132
+ directiveLocation: SourceLocation;
133
+ }
134
+ export interface If extends DirectiveParentNode {
135
+ type: 'If';
136
+ modifier: string;
137
+ condition: Expression;
138
+ }
139
+ export interface ForEach extends DirectiveParentNode {
140
+ type: 'ForEach';
141
+ expression: Expression;
142
+ item: Identifier;
143
+ index?: Identifier;
144
+ }
145
+ export interface ForOf extends DirectiveParentNode {
146
+ type: 'ForOf';
147
+ expression: Expression;
148
+ iterator: Identifier;
110
149
  }
111
- export declare type IRAttribute = IRStringAttribute | IRExpressionAttribute | IRBooleanAttribute;
150
+ export declare type ForBlock = ForEach | ForOf;
151
+ export declare type ParentNode = Root | ForBlock | If | BaseElement;
152
+ export declare type ChildNode = ForBlock | If | BaseElement | Comment | Text;
153
+ export declare type Node = Root | ForBlock | If | BaseElement | Comment | Text;
154
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lwc/template-compiler",
3
- "version": "2.7.2",
3
+ "version": "2.9.0",
4
4
  "description": "Template compiler package",
5
5
  "homepage": "https://lwc.dev/",
6
6
  "repository": {
@@ -25,8 +25,8 @@
25
25
  },
26
26
  "//": "Currently can't upgrade estree-walker to v3.0.0 because it dropped CommonJS support: https://git.io/JXguS",
27
27
  "dependencies": {
28
- "@lwc/errors": "2.7.2",
29
- "@lwc/shared": "2.7.2",
28
+ "@lwc/errors": "2.9.0",
29
+ "@lwc/shared": "2.9.0",
30
30
  "acorn": "~8.7.0",
31
31
  "astring": "~1.8.1",
32
32
  "estree-walker": "~2.0.2",
@@ -39,5 +39,5 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
- "gitHead": "f668a3ad36bbebbaa33a5863408a45bf4185d218"
42
+ "gitHead": "94040389ab8fc717b8753e503f659489480a327d"
43
43
  }
package/LICENSE DELETED
@@ -1,10 +0,0 @@
1
- MIT LICENSE
2
-
3
- Copyright (c) 2018, Salesforce.com, Inc.
4
- All rights reserved.
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
-
8
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
-
10
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,61 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.bindExpression = void 0;
23
- /*
24
- * Copyright (c) 2018, salesforce.com, inc.
25
- * All rights reserved.
26
- * SPDX-License-Identifier: MIT
27
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
28
- */
29
- const estree_walker_1 = require("estree-walker");
30
- const t = __importStar(require("../shared/estree"));
31
- const constants_1 = require("../shared/constants");
32
- const ir_1 = require("../shared/ir");
33
- /**
34
- * Bind the passed expression to the component instance. It applies the following transformation to the expression:
35
- * - {value} --> {$cmp.value}
36
- * - {value[index]} --> {$cmp.value[$cmp.index]}
37
- */
38
- function bindExpression(expression, irNode, parentStack) {
39
- if (t.isIdentifier(expression)) {
40
- if ((0, ir_1.isComponentProp)(expression, irNode, parentStack)) {
41
- return t.memberExpression(t.identifier(constants_1.TEMPLATE_PARAMS.INSTANCE), expression);
42
- }
43
- else {
44
- return expression;
45
- }
46
- }
47
- (0, estree_walker_1.walk)(expression, {
48
- leave(node, parent) {
49
- if (parent !== null &&
50
- t.isIdentifier(node) &&
51
- t.isMemberExpression(parent) &&
52
- parent.object === node &&
53
- (0, ir_1.isComponentProp)(node, irNode, parentStack)) {
54
- this.replace(t.memberExpression(t.identifier(constants_1.TEMPLATE_PARAMS.INSTANCE), node));
55
- }
56
- },
57
- });
58
- return expression;
59
- }
60
- exports.bindExpression = bindExpression;
61
- //# sourceMappingURL=scope.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"scope.js","sourceRoot":"","sources":["../../../src/codegen/scope.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;GAKG;AACH,iDAAqC;AAErC,oDAAsC;AACtC,mDAAsD;AACtD,qCAA+C;AAG/C;;;;GAIG;AACH,SAAgB,cAAc,CAC1B,UAA8B,EAC9B,MAAc,EACd,WAAqB;IAErB,IAAI,CAAC,CAAC,YAAY,CAAC,UAAU,CAAC,EAAE;QAC5B,IAAI,IAAA,oBAAe,EAAC,UAAU,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE;YAClD,OAAO,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,UAAU,CAAC,2BAAe,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,CAAC;SACjF;aAAM;YACH,OAAO,UAAU,CAAC;SACrB;KACJ;IAED,IAAA,oBAAI,EAAC,UAAU,EAAE;QACb,KAAK,CAAC,IAAI,EAAE,MAAM;YACd,IACI,MAAM,KAAK,IAAI;gBACf,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC;gBACpB,CAAC,CAAC,kBAAkB,CAAC,MAAM,CAAC;gBAC5B,MAAM,CAAC,MAAM,KAAK,IAAI;gBACtB,IAAA,oBAAe,EAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,EAC5C;gBACE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,UAAU,CAAC,2BAAe,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;aAClF;QACL,CAAC;KACJ,CAAC,CAAC;IAEH,OAAO,UAAU,CAAC;AACtB,CAAC;AA5BD,wCA4BC"}
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isComponentProp = exports.isIRBooleanAttribute = exports.isIRStringAttribute = exports.isIRExpressionAttribute = exports.isSlot = exports.isTemplate = exports.isCustomElement = exports.isCommentNode = exports.isTextNode = exports.isElement = exports.createComment = exports.createText = exports.createElement = void 0;
4
- const types_1 = require("./types");
5
- function createElement(original, location) {
6
- return {
7
- type: 'element',
8
- tag: original.tagName,
9
- namespace: original.namespaceURI,
10
- children: [],
11
- location,
12
- __original: original,
13
- };
14
- }
15
- exports.createElement = createElement;
16
- function createText(value, location) {
17
- return {
18
- type: 'text',
19
- value,
20
- location,
21
- };
22
- }
23
- exports.createText = createText;
24
- function createComment(value, location) {
25
- return {
26
- type: 'comment',
27
- value,
28
- location,
29
- };
30
- }
31
- exports.createComment = createComment;
32
- function isElement(node) {
33
- return node.type === 'element';
34
- }
35
- exports.isElement = isElement;
36
- function isTextNode(node) {
37
- return node.type === 'text';
38
- }
39
- exports.isTextNode = isTextNode;
40
- function isCommentNode(node) {
41
- return node.type === 'comment';
42
- }
43
- exports.isCommentNode = isCommentNode;
44
- function isCustomElement(node) {
45
- return isElement(node) && node.component !== undefined;
46
- }
47
- exports.isCustomElement = isCustomElement;
48
- function isTemplate(element) {
49
- return element.tag === 'template';
50
- }
51
- exports.isTemplate = isTemplate;
52
- function isSlot(element) {
53
- return element.tag === 'slot';
54
- }
55
- exports.isSlot = isSlot;
56
- function isIRExpressionAttribute(attribute) {
57
- return attribute.type === types_1.IRAttributeType.Expression;
58
- }
59
- exports.isIRExpressionAttribute = isIRExpressionAttribute;
60
- function isIRStringAttribute(attribute) {
61
- return attribute.type === types_1.IRAttributeType.String;
62
- }
63
- exports.isIRStringAttribute = isIRStringAttribute;
64
- function isIRBooleanAttribute(attribute) {
65
- return attribute.type === types_1.IRAttributeType.Boolean;
66
- }
67
- exports.isIRBooleanAttribute = isIRBooleanAttribute;
68
- function isComponentProp(identifier, root, parentStack) {
69
- var _a;
70
- const { name } = identifier;
71
- let current = root;
72
- // Walking up the AST and checking for each node to find if the identifer name is identical to
73
- // an iteration variable.
74
- for (let i = parentStack.length; i >= 0; i--) {
75
- if (isElement(current)) {
76
- const { forEach, forOf } = current;
77
- if ((forEach === null || forEach === void 0 ? void 0 : forEach.item.name) === name ||
78
- ((_a = forEach === null || forEach === void 0 ? void 0 : forEach.index) === null || _a === void 0 ? void 0 : _a.name) === name ||
79
- (forOf === null || forOf === void 0 ? void 0 : forOf.iterator.name) === name) {
80
- return false;
81
- }
82
- }
83
- current = parentStack[i - 1];
84
- }
85
- // The identifier is bound to a component property if no match is found after reaching to AST
86
- // root.
87
- return true;
88
- }
89
- exports.isComponentProp = isComponentProp;
90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AAChB,CAAC;AA7BD,0CA6BC"}
@@ -1,8 +0,0 @@
1
- import * as t from '../shared/estree';
2
- import { IRNode, TemplateExpression } from '../shared/types';
3
- /**
4
- * Bind the passed expression to the component instance. It applies the following transformation to the expression:
5
- * - {value} --> {$cmp.value}
6
- * - {value[index]} --> {$cmp.value[$cmp.index]}
7
- */
8
- export declare function bindExpression(expression: TemplateExpression, irNode: IRNode, parentStack: IRNode[]): t.Expression;