Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/*eslint-disable no-unused-vars*/
import { ExpressionStatement } from 'estree';
import { Transformer } from './index';
/*eslint-enable no-unused-vars*/
import { createHtmlProcessorWrapper } from '../node-builder';
import { Syntax } from 'esotope-hammerhead';
// Transform:
// x = 5; "hello" --> x = 5; parent.__proc$Html(window, "hello")
// someAction(); generateHtmlPage() --> someAction(); parent.__proc$Html(window, generateHtmlPage())
const transformer: Transformer = {
nodeReplacementRequireTransform: true,
nodeTypes: Syntax.ExpressionStatement,
condition: (node, parent) => !!transformer.wrapLastExpr && !!parent && parent.type === Syntax.Program &&
parent.body[parent.body.length - 1] === node,
run: node => {
transformer.wrapLastExpr = false;
return createHtmlProcessorWrapper(node);
}
};
export default transformer;
function isStrictMode (ast: Program): boolean {
if (ast.body.length) {
const firstChild = ast.body[0];
if (firstChild.type === Syntax.ExpressionStatement && firstChild.expression.type === Syntax.Literal)
return firstChild.expression.value === 'use strict';
}
return false;
}
export function createAssignmentExprStmt (left: MemberExpression, right: Identifier): ExpressionStatement {
return {
type: Syntax.ExpressionStatement,
expression: {
type: Syntax.AssignmentExpression,
operator: '=',
left: left,
right: right
}
};
}
function isArrayDataScript (ast: Program): boolean {
const firstChild = ast.body[0];
return ast.body.length === 1 &&
firstChild.type === Syntax.ExpressionStatement &&
firstChild.expression.type === Syntax.ArrayExpression;
}