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

wip: move a11y to its on file #3725

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@
"@typescript-eslint/parser": "^2.1.0",
"acorn": "^7.0.0",
"agadoo": "^1.1.0",
"aria-query": "^3.0.0",
"axobject-query": "^2.0.2",
"c8": "^5.0.1",
"code-red": "0.0.17",
"codecov": "^3.5.0",
"css-tree": "1.0.0-alpha22",
"emoji-regex": "^7.0.3",
"eslint": "^6.3.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-svelte3": "^2.7.3",
Expand Down
53 changes: 43 additions & 10 deletions src/compiler/compile/nodes/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Text from './Text';
import Expression from './shared/Expression';
import TemplateScope from './shared/TemplateScope';
import { x } from 'code-red';
import validateA11y from '../utils/validate-a11y/Attribute';

export default class Attribute extends Node {
type: 'Attribute';
Expand Down Expand Up @@ -60,6 +61,12 @@ export default class Attribute extends Node {
return expression;
});
}

this.validate();
}

validate() {
validateA11y(this);
}

get_dependencies() {
Expand All @@ -86,7 +93,11 @@ export default class Attribute extends Node {
}

let expression = this.chunks
.map(chunk => chunk.type === 'Text' ? string_literal(chunk.data) : chunk.manipulate(block))
.map(chunk =>
chunk.type === 'Text'
? string_literal(chunk.data)
: chunk.manipulate(block)
)
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);

if (this.chunks[0].type !== 'Text') {
Expand All @@ -95,16 +106,16 @@ export default class Attribute extends Node {

return expression;
}

get_static_value() {
if (this.is_spread || this.dependencies.size > 0) return null;

return this.is_true
? true
: this.chunks[0]
// method should be called only when `is_static = true`
? (this.chunks[0] as Text).data
: '';
if (this.is_spread || this.dependencies.size > 0) return undefined;
if (this.is_true) return true;
if (this.is_static) return this.chunks[0] ? (this.chunks[0] as Text).data: '';
if (this.chunks[0]) {
const expression = (this.chunks[0] as Expression).node;
return evaluate_value(expression);
}
return undefined;
}

should_cache() {
Expand All @@ -116,3 +127,25 @@ export default class Attribute extends Node {
: true;
}
}

function evaluate_value(node) {
switch (node.type) {
case 'Literal':
return node.value;
case 'UnaryExpression':
switch (node.operator) {
case '~':
return ~evaluate_value(node.argument);
case '!':
return !evaluate_value(node.argument);
case '+':
return +evaluate_value(node.argument);
case '-':
return -evaluate_value(node.argument);
}
break;
case 'TemplateLiteral':
return node.quasis[0].value.cooked;
}
return undefined;
}
Loading