Sha256: e1f183be891f9d7edb5ca44db3abc4cbc3df2b6835855b9c921394b70337d849
Contents?: true
Size: 1.23 KB
Versions: 3
Compression:
Stored size: 1.23 KB
Contents
export default function transformTopLevelComponent( /* env */) { return { name: 'transform-top-level-component', visitor: { Program(node) { hasSingleComponentNode(node, (component) => { component.tag = `@${component.tag}`; component.isStatic = true; }); }, }, }; } function hasSingleComponentNode(program, componentCallback) { let { loc, body } = program; if (!loc || loc.start.line !== 1 || loc.start.column !== 0) { return; } let lastComponentNode; let nodeCount = 0; for (let i = 0; i < body.length; i++) { let curr = body[i]; // text node with whitespace only if (curr.type === 'TextNode' && /^[\s]*$/.test(curr.chars)) { continue; } // has multiple root elements if we've been here before if (nodeCount++ > 0) { return false; } if (curr.type === 'ComponentNode' || curr.type === 'ElementNode') { lastComponentNode = curr; } } if (!lastComponentNode) { return; } if (lastComponentNode.type === 'ComponentNode') { componentCallback(lastComponentNode); } }
Version data entries
3 entries across 3 versions & 1 rubygems