UNPKG

5.59 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14import * as babel from '@babel/types';
15import * as parse5 from 'parse5';
16import { ParsedHtmlDocument } from '../html/html-document';
17import { JavaScriptDocument } from '../javascript/javascript-document';
18import { SourceRange, Warning } from '../model/model';
19import { ParsedDocument } from '../parser/document';
20export interface Template extends parse5.ASTNode {
21 content: parse5.ASTNode;
22}
23/**
24 * Given a node, return all databinding templates inside it.
25 *
26 * A template is "databinding" if polymer databinding expressions are expected
27 * to be evaluated inside. e.g. <template is='dom-if'> or <dom-module><template>
28 *
29 * Results include both direct and nested templates (e.g. dom-if inside
30 * dom-module).
31 */
32export declare function getAllDataBindingTemplates(node: parse5.ASTNode): IterableIterator<Template>;
33export declare type HtmlDatabindingExpression = TextNodeDatabindingExpression | AttributeDatabindingExpression;
34/**
35 * Some expressions are limited. For example, in a property declaration,
36 * `observer` must be the identifier of a method, and `computed` must be a
37 * function call expression.
38 */
39export declare type ExpressionLimitation = 'full' | 'identifierOnly' | 'callExpression';
40export declare abstract class DatabindingExpression {
41 readonly sourceRange: SourceRange;
42 readonly warnings: Warning[];
43 readonly expressionText: string;
44 private readonly _expressionAst;
45 private readonly locationOffset;
46 private readonly _document;
47 /**
48 * Toplevel properties on the model that are referenced in this expression.
49 *
50 * e.g. in {{foo(bar, baz.zod)}} the properties are foo, bar, and baz
51 * (but not zod).
52 */
53 properties: Array<{
54 name: string;
55 sourceRange: SourceRange;
56 }>;
57 constructor(sourceRange: SourceRange, expressionText: string, ast: babel.Program, limitation: ExpressionLimitation, document: ParsedDocument);
58 /**
59 * Given an estree node in this databinding expression, give its source range.
60 */
61 sourceRangeForNode(node: babel.Node): SourceRange | undefined;
62 private _extractPropertiesAndValidate;
63 private _validateLimitation;
64 private _extractAndValidateSubExpression;
65 private _validationWarning;
66}
67export declare class AttributeDatabindingExpression extends DatabindingExpression {
68 /**
69 * The element whose attribute/property is assigned to.
70 */
71 readonly astNode: parse5.ASTNode;
72 readonly databindingInto = "attribute";
73 /**
74 * If true, this is databinding into the complete attribute. Polymer treats
75 * such databindings specially, e.g. they're setting the property by default,
76 * not the attribute.
77 *
78 * e.g.
79 * foo="{{bar}}" is complete, foo="hello {{bar}} world" is not complete.
80 *
81 * An attribute may have multiple incomplete bindings. They will be separate
82 * AttributeDatabindingExpressions.
83 */
84 readonly isCompleteBinding: boolean;
85 /** The databinding syntax used. */
86 readonly direction: '{' | '[';
87 /**
88 * If this is a two-way data binding, and an event name was specified
89 * (using ::eventName syntax), this is that event name.
90 */
91 readonly eventName: string | undefined;
92 /** The attribute we're databinding into. */
93 readonly attribute: parse5.ASTAttribute;
94 constructor(astNode: parse5.ASTNode, isCompleteBinding: boolean, direction: '{' | '[', eventName: string | undefined, attribute: parse5.ASTAttribute, sourceRange: SourceRange, expressionText: string, ast: babel.Program, document: ParsedHtmlDocument);
95}
96export declare class TextNodeDatabindingExpression extends DatabindingExpression {
97 /** The databinding syntax used. */
98 readonly direction: '{' | '[';
99 /**
100 * The HTML text node that contains this databinding.
101 */
102 readonly astNode: parse5.ASTNode;
103 readonly databindingInto = "text-node";
104 constructor(direction: '{' | '[', astNode: parse5.ASTNode, sourceRange: SourceRange, expressionText: string, ast: babel.Program, document: ParsedHtmlDocument);
105}
106export declare class JavascriptDatabindingExpression extends DatabindingExpression {
107 readonly astNode: babel.Node;
108 readonly databindingInto = "javascript";
109 constructor(astNode: babel.Node, sourceRange: SourceRange, expressionText: string, ast: babel.Program, kind: ExpressionLimitation, document: JavaScriptDocument);
110}
111/**
112 * Find and parse Polymer databinding expressions in HTML.
113 */
114export declare function scanDocumentForExpressions(document: ParsedHtmlDocument): {
115 expressions: HtmlDatabindingExpression[];
116 warnings: Warning[];
117};
118export declare function scanDatabindingTemplateForExpressions(document: ParsedHtmlDocument, template: Template): {
119 expressions: HtmlDatabindingExpression[];
120 warnings: Warning[];
121};
122export declare function parseExpressionInJsStringLiteral(document: JavaScriptDocument, stringLiteral: babel.Node, kind: 'identifierOnly' | 'callExpression' | 'full'): {
123 databinding: JavascriptDatabindingExpression | undefined;
124 warnings: Warning[];
125};