Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AST plugin functional #596

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions lib/ast-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,28 @@ function cleanupNamedBlocksPolyfillSyntax(sourceString) {
return sourceString;
}

class FreestyleSourceExtractionAstPlugin {
constructor(env) {
this.originalSource = env.contents;
}
function extractSource(nodes, contents) {
// nodes should be a contiguous collection of ast nodes
let startNode = nodes[0];
let endNode = nodes[nodes.length - 1];
let start = startNode.loc.start;
let end = endNode.loc.end;
let lines = contents.split('\n').slice(start.line - 1, end.line);
lines[0] = new Array(start.column).join(' ') + lines[0].slice(start.column);
lines[lines.length - 1] = lines[lines.length - 1].slice(0, end.column);
return stripIndent(lines.join('\n'));
}

extractSource(nodes) {
// nodes should be a contiguous collection of ast nodes
let startNode = nodes[0];
let endNode = nodes[nodes.length - 1];
let start = startNode.loc.start;
let end = endNode.loc.end;
let lines = this.originalSource.split('\n').slice(start.line - 1, end.line);
lines[0] = new Array(start.column).join(' ') + lines[0].slice(start.column);
lines[lines.length - 1] = lines[lines.length - 1].slice(0, end.column);
return stripIndent(lines.join('\n'));
}
module.exports = function ({ contents, syntax }) {
const { builders: b } = syntax;

return {
name: 'component-freestyle-source-extracter',

transform(ast) {
const plugin = this;
const { builders: b } = this.syntax;
const visitor = {
visitor: {
ElementNode(node) {
if (['FreestyleUsage', 'FreestyleDynamic'].includes(node.tag)) {
const sourceString = plugin.extractSource(node.children);
const sourceString = extractSource(node.children, contents);
node.attributes.push(b.attr('@source', b.text(sourceString)));
}
if (['Freestyle::Usage'].includes(node.tag)) {
Expand All @@ -80,15 +78,15 @@ class FreestyleSourceExtractionAstPlugin {
let exampleNode = node.children.find((n) => n.tag === ':example');
let sourceString;
if (exampleNode) {
sourceString = plugin.extractSource(exampleNode.children);
sourceString = extractSource(exampleNode.children, contents);
} else if (hasNamedBlocksPolyfillSyntax(node)) {
// Unfortunately, we may still run after the named-blocks-polyfill when in an Ember Addon, so we do the best we can here.
exampleNode = extractFromNamedBlocksPolyfillSyntax(node);
sourceString = plugin.extractSource(exampleNode.body);
sourceString = extractSource(exampleNode.body, contents);
sourceString = cleanupNamedBlocksPolyfillSyntax(sourceString);
} else {
exampleNode = node; // not using named blocks
sourceString = plugin.extractSource(exampleNode.children);
sourceString = extractSource(exampleNode.children, contents);
}
node.attributes.push(b.attr('@source', b.text(sourceString)));
}
Expand All @@ -103,15 +101,10 @@ class FreestyleSourceExtractionAstPlugin {
'templates/components/freestyle-dynamic.hbs'
))
) {
const sourceString = plugin.extractSource(node.program.body);
const sourceString = extractSource(node.program.body, contents);
node.hash.pairs.push(b.pair('source', b.string(sourceString)));
}
},
};

this.syntax.traverse(ast, visitor);
return ast;
}
}

module.exports = FreestyleSourceExtractionAstPlugin;
},
};
};