imports.forEach(i => {
   if (!i.isDefault) {
     output.appendLeft(0, `import * as ${i.qualifier} from '${i.specifier}';\n`);
   } else {
     output.appendLeft(0, `import ${i.qualifier} from '${i.specifier}';\n`);
   }
 });
Exemple #2
0
export function add_indentation(code: MagicString, node: Node, levels = 1) {
	const base_indent = code.getIndentString();
	const indent = repeat(base_indent, levels);
	const pattern = /\n/gm;

	const excluded = [];

	walk(node, {
		enter(node) {
			if (node.type === 'TemplateElement') {
				excluded.push(node);
			}
		}
	});

	const str = code.original.slice(node.start, node.end);

	let match;
	while (match = pattern.exec(str)) {
		const index = node.start + match.index;
		while (excluded[0] && excluded[0].end < index) excluded.shift();
		if (excluded[0] && excluded[0].start < index) continue;

		code.appendLeft(index + 1, indent);
	}
}
	render (code: MagicString, options: RenderOptions) {
		if (!this.module.graph.treeshake) {
			super.render(code, options);
		} else {
			const last = this.expressions[this.expressions.length - 1];
			last.render(code, options);

			if (
				this.parent.type === NodeType.CallExpression &&
				last.type === NodeType.MemberExpression &&
				this.expressions.length > 1
			) {
				this.expressions[0].included = true;
			}

			const included = this.expressions
				.slice(0, this.expressions.length - 1)
				.filter(expression => expression.included);
			if (included.length === 0) {
				code.remove(this.start, last.start);
				code.remove(last.end, this.end);
			} else {
				let previousEnd = this.start;
				for (const expression of included) {
					expression.render(code, options);
					code.remove(previousEnd, expression.start);
					code.appendLeft(expression.end, ', ');
					previousEnd = expression.end;
				}

				code.remove(previousEnd, last.start);
				code.remove(last.end, this.end);
			}
		}
	}
	private renderDeclarationEnd (
		code: MagicString,
		separatorString: string,
		lastSeparatorPos: number,
		actualContentEnd: number,
		renderedContentEnd: number,
		addSemicolon: boolean
	) {
		if (code.original.charCodeAt(this.end - 1) === 59 /*";"*/) {
			code.remove(this.end - 1, this.end);
		}
		if (addSemicolon) {
			separatorString += ';';
		}
		if (lastSeparatorPos !== null) {
			if (
				code.original.charCodeAt(actualContentEnd - 1) === 10 /*"\n"*/
				&& (code.original.charCodeAt(this.end) === 10 /*"\n"*/ || code.original.charCodeAt(this.end) === 13 /*"\r"*/)
			) {
				actualContentEnd--;
				if (code.original.charCodeAt(actualContentEnd) === 13 /*"\r"*/) {
					actualContentEnd--;
				}
			}
			if (actualContentEnd === lastSeparatorPos + 1) {
				code.overwrite(lastSeparatorPos, renderedContentEnd, separatorString);
			} else {
				code.overwrite(lastSeparatorPos, lastSeparatorPos + 1, separatorString);
				code.remove(actualContentEnd, renderedContentEnd);
			}
		} else {
			code.appendLeft(renderedContentEnd, separatorString);
		}
		return separatorString;
	}
Exemple #5
0
		function encapsulateBlock(block: Block) {
			let i = block.selectors.length;
			while (i--) {
				const selector = block.selectors[i];
				if (selector.type === 'PseudoElementSelector' || selector.type === 'PseudoClassSelector') continue;

				if (selector.type === 'TypeSelector' && selector.name === '*') {
					code.overwrite(selector.start, selector.end, attr);
				} else {
					code.appendLeft(selector.end, attr);
				}

				break;
			}

			i = block.selectors.length;
			while (i--) {
				const selector = block.selectors[i];

				if (selector.type === 'RefSelector') {
					code.overwrite(selector.start, selector.end, `[svelte-ref-${selector.name}]`, {
						contentOnly: true,
						storeName: false
					});
				}
			}
		}
Exemple #6
0
 /**
  *  Add the imports at the top of the file
  */
 addImports(
     output: MagicString, imports: {specifier: string; qualifier: string;}[],
     sf: ts.SourceFile): void {
   const insertionPoint = findEndOfImports(sf);
   const renderedImports =
       imports.map(i => `import * as ${i.qualifier} from '${i.specifier}';\n`).join('');
   output.appendLeft(insertionPoint, renderedImports);
 }
Exemple #7
0
 /**
  * Add the definitions to each decorated class
  */
 addDefinitions(output: MagicString, compiledClass: CompiledClass, definitions: string): void {
   const classSymbol = this.host.getClassSymbol(compiledClass.declaration);
   if (!classSymbol) {
     throw new Error(`Compiled class does not have a valid symbol: ${compiledClass.name}`);
   }
   const insertionPoint = classSymbol.valueDeclaration !.getEnd();
   output.appendLeft(insertionPoint, '\n' + definitions);
 }
Exemple #8
0
	render(code: MagicString, options: RenderOptions) {
		this.left.render(code, options);
		this.right.render(code, options);
		if (options.format === 'system' && this.left.variable && this.left.variable.exportName) {
			code.prependLeft(
				code.original.indexOf('=', this.left.end) + 1,
				` exports('${this.left.variable.exportName}',`
			);
			code.appendLeft(this.right.end, `)`);
		}
	}
	private renderDeclarationEnd(
		code: MagicString,
		separatorString: string,
		lastSeparatorPos: number,
		actualContentEnd: number,
		renderedContentEnd: number,
		addSemicolon: boolean,
		systemPatternExports: Variable[]
	): void {
		if (code.original.charCodeAt(this.end - 1) === 59 /*";"*/) {
			code.remove(this.end - 1, this.end);
		}
		if (addSemicolon) {
			separatorString += ';';
		}
		if (lastSeparatorPos !== null) {
			if (
				code.original.charCodeAt(actualContentEnd - 1) === 10 /*"\n"*/ &&
				(code.original.charCodeAt(this.end) === 10 /*"\n"*/ ||
					code.original.charCodeAt(this.end) === 13) /*"\r"*/
			) {
				actualContentEnd--;
				if (code.original.charCodeAt(actualContentEnd) === 13 /*"\r"*/) {
					actualContentEnd--;
				}
			}
			if (actualContentEnd === lastSeparatorPos + 1) {
				code.overwrite(lastSeparatorPos, renderedContentEnd, separatorString);
			} else {
				code.overwrite(lastSeparatorPos, lastSeparatorPos + 1, separatorString);
				code.remove(actualContentEnd, renderedContentEnd);
			}
		} else {
			code.appendLeft(renderedContentEnd, separatorString);
		}
		if (systemPatternExports.length > 0) {
			code.appendLeft(renderedContentEnd, ' ' + getSystemExportStatement(systemPatternExports));
		}
	}
Exemple #10
0
 addConstants(output: MagicString, constants: string, file: ts.SourceFile): void {
   if (constants === '') {
     return;
   }
   const insertionPoint = file.statements.reduce((prev, stmt) => {
     if (ts.isImportDeclaration(stmt) || ts.isImportEqualsDeclaration(stmt) ||
         ts.isNamespaceImport(stmt)) {
       return stmt.getEnd();
     }
     return prev;
   }, 0);
   output.appendLeft(insertionPoint, '\n' + constants + '\n');
 }