forked from sveltejs/svelte
-
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.
[fix] adjust style directive AST (sveltejs#7127)
- deduplicate type name: Is now "StyleDirective" - Don't transform value array to template literal in the AST phase but in the compiler phase. This ensures other tools know what the raw output was and that start/end positions are available
- Loading branch information
1 parent
9ae94eb
commit a796b91
Showing
11 changed files
with
474 additions
and
116 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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
import { TemplateNode } from '../../interfaces'; | ||
import Component from '../Component'; | ||
import { nodes_to_template_literal } from '../utils/nodes_to_template_literal'; | ||
import Expression from './shared/Expression'; | ||
import Node from './shared/Node'; | ||
import TemplateScope from './shared/TemplateScope'; | ||
|
||
export default class StyleDirective extends Node { | ||
type: 'StyleDirective'; | ||
name: string; | ||
expression: Expression; | ||
should_cache: boolean; | ||
|
||
constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) { | ||
super(component, parent, scope, info); | ||
|
||
this.name = info.name; | ||
|
||
// Convert the value array to an expression so it's easier to handle | ||
// the StyleDirective going forward. | ||
if (info.value === true || (info.value.length === 1 && info.value[0].type === 'MustacheTag')) { | ||
const identifier = info.value === true | ||
? { | ||
type: 'Identifier', | ||
start: info.end - info.name.length, | ||
end: info.end, | ||
name: info.name | ||
} as any | ||
: info.value[0].expression; | ||
this.expression = new Expression(component, this, scope, identifier); | ||
this.should_cache = false; | ||
} else { | ||
const raw_expression = nodes_to_template_literal(info.value); | ||
this.expression = new Expression(component, this, scope, raw_expression); | ||
this.should_cache = raw_expression.expressions.length > 0; | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { TemplateElement, TemplateLiteral } from 'estree'; | ||
import { MustacheTag, Text } from '../../interfaces'; | ||
|
||
/** | ||
* Transforms a list of Text and MustacheTags into a TemplateLiteral expression. | ||
* Start/End positions on the elements of the expression are not set. | ||
*/ | ||
export function nodes_to_template_literal(value: Array<Text | MustacheTag>): TemplateLiteral { | ||
const literal: TemplateLiteral = { | ||
type: 'TemplateLiteral', | ||
expressions: [], | ||
quasis: [] | ||
}; | ||
|
||
let quasi: TemplateElement = { | ||
type: 'TemplateElement', | ||
value: { raw: '', cooked: null }, | ||
tail: false | ||
}; | ||
|
||
value.forEach((node) => { | ||
if (node.type === 'Text') { | ||
quasi.value.raw += node.raw; | ||
} else if (node.type === 'MustacheTag') { | ||
literal.quasis.push(quasi); | ||
literal.expressions.push(node.expression as any); | ||
quasi = { | ||
type: 'TemplateElement', | ||
value: { raw: '', cooked: null }, | ||
tail: false | ||
}; | ||
} | ||
}); | ||
quasi.tail = true; | ||
literal.quasis.push(quasi); | ||
return literal; | ||
} |
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
8 changes: 7 additions & 1 deletion
8
test/parser/samples/attribute-style-directive-string/input.svelte
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 |
---|---|---|
@@ -1 +1,7 @@ | ||
<div style:color="red"></div> | ||
<div style:color="red"></div> | ||
<div style:color='red'></div> | ||
<div style:color=red></div> | ||
<div style:color="red{variable}"></div> | ||
<div style:color='red{variable}'></div> | ||
<div style:color=red{variable}></div> | ||
<div style:color={`template${literal}`}></div> |
Oops, something went wrong.