Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
node.body.body.splice(
0,
0,
jsc.methodDefinition(
'get',
jsc.identifier('template'),
jsc.functionExpression(
null, [], jsc.blockStatement([jsc.returnStatement(
templateLiteral)])),
true));
} else if (node.type === 'CallExpression') {
// A Polymer hybrid/legacy factory function element
const arg = node.arguments[0];
if (arg && arg.type === 'ObjectExpression') {
arg.properties.unshift(jsc.property(
'init', jsc.identifier('_template'), templateLiteral));
}
} else {
console.error(`Internal Error, Class or CallExpression expected, got ${
node.type}`);
}
}
return claimedDomModules;
}
if (!canDomModuleBeInlined(domModule)) {
continue;
}
claimedDomModules.add(domModule);
const template = dom5.query(domModule, (e) => e.tagName === 'template');
if (template === null) {
continue;
}
// It's ok to tag templates with the expression `Polymer.html` without
// adding an import because `Polymer.html` is re-exported by both
// polymer.html and polymer-element.html and, crucially, template
// inlining happens before rewriting references.
const templateLiteral = jsc.taggedTemplateExpression(
jsc.memberExpression(
jsc.identifier('Polymer'), jsc.identifier('html')),
serializeNodeToTemplateLiteral(
parse5.treeAdapters.default.getTemplateContent(template)));
const nodePath = getNodePathInProgram(program, element.astNode);
if (nodePath === undefined) {
console.warn(
new Warning({
code: 'not-found',
message: `Can't find recast node for element ${element.tagName}`,
parsedDocument: this.document.parsedDocument,
severity: Severity.WARNING,
sourceRange: element.sourceRange!
}).toString());
continue;
}
nodePath);
} else {
let name = fullyQualifiedNamePath[fullyQualifiedNamePath.length - 1];
// Special Polymer workaround: Register & rewrite the
// `Polymer._polymerFn` export as if it were the `Polymer()`
// namespace function.
let correctedNamespaceName = fullyQualifiedName;
if (fullyQualifiedName === 'Polymer._polymerFn') {
correctedNamespaceName = 'Polymer';
name = 'Polymer';
}
const variableKind =
this.mutableNames.has(correctedNamespaceName) ? 'let' : 'const';
const newExportNode = jsc.exportNamedDeclaration(jsc.variableDeclaration(
variableKind,
[jsc.variableDeclarator(jsc.identifier(name), exportedExpression)]));
replacePreservingComments(nodePath, newExportNode);
this.exportMigrationRecords.push(
{oldNamespacedName: correctedNamespaceName, es6ExportName: name});
}
}
jsc.expressionStatement(jsc.assignmentExpression(
'=',
jsc.memberExpression(
jsc.identifier(varName), jsc.identifier('innerHTML')),
templateValue));
const targetNode = activeInBody ? 'body' : 'head';
return [
createElementTemplate,
setDocumentContainerStatement,
jsc.expressionStatement(jsc.callExpression(
jsc.memberExpression(
jsc.memberExpression(
jsc.identifier('document'), jsc.identifier(targetNode)),
jsc.identifier('appendChild')),
[jsc.memberExpression(
jsc.identifier(varName), jsc.identifier('content'))]))
];
}
export function createDomNodeInsertStatements(
nodes: parse5.ASTNode[], activeInBody = false): estree.Statement[] {
const varName = `$_documentContainer`;
const fragment = {
nodeName: '#document-fragment',
attrs: [],
childNodes: nodes,
};
const templateValue = serializeNodeToTemplateLiteral(fragment as any, false);
const createElementDiv = jsc.variableDeclaration(
'const',
[jsc.variableDeclarator(
jsc.identifier(varName),
jsc.callExpression(
jsc.memberExpression(
jsc.identifier('document'), jsc.identifier('createElement')),
[jsc.literal('template')]))]);
const setDocumentContainerStatement =
jsc.expressionStatement(jsc.assignmentExpression(
'=',
jsc.memberExpression(
jsc.identifier(varName), jsc.identifier('innerHTML')),
templateValue));
if (activeInBody) {
return [
createElementDiv,
setDocumentContainerStatement,
jsc.expressionStatement(jsc.callExpression(
jsc.memberExpression(
}
const setDisplayNoneStatement = jsc.expressionStatement(jsc.callExpression(
jsc.memberExpression(
jsc.identifier(varName), jsc.identifier('setAttribute')),
[jsc.literal('style'), jsc.literal('display: none;')]));
return [
createElementDiv,
setDisplayNoneStatement,
setDocumentContainerStatement,
jsc.expressionStatement(jsc.callExpression(
jsc.memberExpression(
jsc.memberExpression(
jsc.identifier('document'), jsc.identifier('head')),
jsc.identifier('appendChild')),
[jsc.memberExpression(
jsc.identifier(varName), jsc.identifier('content'))]))
];
}
export function attachCommentsToEndOfProgram(
comments: string[],
statements: Array) {
if (comments.length === 0) {
return;
}
const message =
`\n FIXME(polymer-modulizer): the above comments were extracted\n` +
` from HTML and may be out of place here. Review them and\n` +
` then delete this comment!\n`;
comments.push(message);
const recastComments = getCommentsFromTexts(comments);
const lastStatement =
jsc.expressionStatement(jsc.identifier('')) as RecastNode &
estree.Statement;
lastStatement.comments =
(lastStatement.comments || []).concat(recastComments);
statements.push(lastStatement);
}
[jsc.literal('template')]))]);
const setDocumentContainerStatement =
jsc.expressionStatement(jsc.assignmentExpression(
'=',
jsc.memberExpression(
jsc.identifier(varName), jsc.identifier('innerHTML')),
templateValue));
const targetNode = activeInBody ? 'body' : 'head';
return [
createElementTemplate,
setDocumentContainerStatement,
jsc.expressionStatement(jsc.callExpression(
jsc.memberExpression(
jsc.memberExpression(
jsc.identifier('document'), jsc.identifier(targetNode)),
jsc.identifier('appendChild')),
[jsc.memberExpression(
jsc.identifier(varName), jsc.identifier('content'))]))
];
}