forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement inline link-to as an AST transform (emberjs#12229)
- Loading branch information
1 parent
3fbc1a2
commit d31a14d
Showing
5 changed files
with
54 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/ember-template-compiler/lib/plugins/transform-inline-link-to.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export default function TransformInlineLinkTo(options) { | ||
this.options = options; | ||
this.syntax = null; | ||
} | ||
|
||
TransformInlineLinkTo.prototype.transform = function TransformInlineLinkTo_transform(ast) { | ||
let { traverse, builders: b } = this.syntax; | ||
|
||
function buildProgram(content) { | ||
return b.program([buildStatement(content)]); | ||
} | ||
|
||
function buildStatement(content) { | ||
switch (content.type) { | ||
case 'PathExpression': | ||
return b.mustache(content); | ||
|
||
case 'SubExpression': | ||
return b.mustache(content.path, content.params, content.hash); | ||
|
||
// The default case handles literals. | ||
default: | ||
return b.text('' + content.value); | ||
} | ||
} | ||
|
||
function unsafeHtml(expr) { | ||
return b.sexpr('-html-safe', [expr]); | ||
} | ||
|
||
traverse(ast, { | ||
MustacheStatement(node) { | ||
if (node.path.original === 'link-to') { | ||
let content = node.escaped ? node.params[0] : unsafeHtml(node.params[0]); | ||
return b.block( | ||
'link-to', | ||
node.params.slice(1), | ||
node.hash, | ||
buildProgram(content) | ||
); | ||
} | ||
} | ||
}); | ||
|
||
return ast; | ||
}; |
29 changes: 0 additions & 29 deletions
29
packages/ember-template-compiler/lib/plugins/transform-unescaped-inline-link-to.js
This file was deleted.
Oops, something went wrong.