Convert src/compiler/compile/utils/nodes_to_template_literal.ts to JavaScript

pull/8569/head
Simon Holthausen 3 years ago
parent a2e2bacb3a
commit 2c9b1ea776

@ -1,6 +1,7 @@
import { roles as roles_map, elementRoles } from 'aria-query'; import { roles as roles_map, elementRoles } from 'aria-query';
import { AXObjects, AXObjectRoles, elementAXObjects } from 'axobject-query'; import { AXObjects, AXObjectRoles, elementAXObjects } from 'axobject-query';
import { regex_whitespaces } from '../../utils/patterns.js'; import { regex_whitespaces } from '../../utils/patterns.js';
const aria_roles = roles_map.keys(); const aria_roles = roles_map.keys();
const abstract_roles = new Set(aria_roles.filter((role) => roles_map.get(role).abstract)); const abstract_roles = new Set(aria_roles.filter((role) => roles_map.get(role).abstract));
const non_abstract_roles = aria_roles.filter((name) => !abstract_roles.has(name)); const non_abstract_roles = aria_roles.filter((name) => !abstract_roles.has(name));
@ -33,21 +34,21 @@ const interactive_roles = new Set(
); );
/** /**
* @param {ARIARoleDefinitionKey} role * @param {import('aria-query').ARIARoleDefinitionKey} role
*/ */
export function is_non_interactive_roles(role) { export function is_non_interactive_roles(role) {
return non_interactive_roles.has(role); return non_interactive_roles.has(role);
} }
/** /**
* @param {ARIARoleDefinitionKey} role * @param {import('aria-query').ARIARoleDefinitionKey} role
*/ */
export function is_interactive_roles(role) { export function is_interactive_roles(role) {
return interactive_roles.has(role); return interactive_roles.has(role);
} }
/** /**
* @param {ARIARoleDefinitionKey} role * @param {import('aria-query').ARIARoleDefinitionKey} role
*/ */
export function is_abstract_role(role) { export function is_abstract_role(role) {
return abstract_roles.has(role); return abstract_roles.has(role);
@ -55,7 +56,7 @@ export function is_abstract_role(role) {
const presentation_roles = new Set(['presentation', 'none']); const presentation_roles = new Set(['presentation', 'none']);
/** /**
* @param {ARIARoleDefinitionKey} role * @param {import('aria-query').ARIARoleDefinitionKey} role
*/ */
export function is_presentation_role(role) { export function is_presentation_role(role) {
return presentation_roles.has(role); return presentation_roles.has(role);
@ -63,7 +64,7 @@ export function is_presentation_role(role) {
/** /**
* @param {string} tag_name * @param {string} tag_name
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
*/ */
export function is_hidden_from_screen_reader(tag_name, attribute_map) { export function is_hidden_from_screen_reader(tag_name, attribute_map) {
if (tag_name === 'input') { if (tag_name === 'input') {
@ -80,7 +81,7 @@ export function is_hidden_from_screen_reader(tag_name, attribute_map) {
} }
/** /**
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
*/ */
export function has_disabled_attribute(attribute_map) { export function has_disabled_attribute(attribute_map) {
const disabled_attr = attribute_map.get('disabled'); const disabled_attr = attribute_map.get('disabled');
@ -99,7 +100,7 @@ export function has_disabled_attribute(attribute_map) {
} }
/** /**
* @type {ARIARoleRelationConcept[]} * @type {import('aria-query').ARIARoleRelationConcept[]}
*/ */
const non_interactive_element_role_schemas = []; const non_interactive_element_role_schemas = [];
elementRoles.entries().forEach(([schema, roles]) => { elementRoles.entries().forEach(([schema, roles]) => {
@ -109,7 +110,7 @@ elementRoles.entries().forEach(([schema, roles]) => {
}); });
/** /**
* @type {ARIARoleRelationConcept[]} * @type {import('aria-query').ARIARoleRelationConcept[]}
*/ */
const interactive_element_role_schemas = []; const interactive_element_role_schemas = [];
elementRoles.entries().forEach(([schema, roles]) => { elementRoles.entries().forEach(([schema, roles]) => {
@ -127,7 +128,7 @@ const non_interactive_ax_objects = new Set(
); );
/** /**
* @type {ARIARoleRelationConcept[]} * @type {import('aria-query').ARIARoleRelationConcept[]}
*/ */
const interactive_element_ax_object_schemas = []; const interactive_element_ax_object_schemas = [];
elementAXObjects.entries().forEach(([schema, ax_object]) => { elementAXObjects.entries().forEach(([schema, ax_object]) => {
@ -137,7 +138,7 @@ elementAXObjects.entries().forEach(([schema, ax_object]) => {
}); });
/** /**
* @type {ARIARoleRelationConcept[]} * @type {import('aria-query').ARIARoleRelationConcept[]}
*/ */
const non_interactive_element_ax_object_schemas = []; const non_interactive_element_ax_object_schemas = [];
elementAXObjects.entries().forEach(([schema, ax_object]) => { elementAXObjects.entries().forEach(([schema, ax_object]) => {
@ -147,9 +148,9 @@ elementAXObjects.entries().forEach(([schema, ax_object]) => {
}); });
/** /**
* @param {ARIARoleRelationConcept} schema * @param {import('aria-query').ARIARoleRelationConcept} schema
* @param {string} tag_name * @param {string} tag_name
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
*/ */
function match_schema(schema, tag_name, attribute_map) { function match_schema(schema, tag_name, attribute_map) {
if (schema.name !== tag_name) return false; if (schema.name !== tag_name) return false;
@ -163,17 +164,17 @@ function match_schema(schema, tag_name, attribute_map) {
return true; return true;
}); });
} }
export var ElementInteractivity;
(function (ElementInteractivity) { export const ElementInteractivity = /** @type {const} */ ({
ElementInteractivity['Interactive'] = 'interactive'; Interactive: 'interactive',
ElementInteractivity['NonInteractive'] = 'non-interactive'; NonInteractive: 'non-interactive',
ElementInteractivity['Static'] = 'static'; Static: 'static'
})(ElementInteractivity || (ElementInteractivity = {})); });
/** /**
* @param {string} tag_name * @param {string} tag_name
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
* @returns {import("C:/repos/svelte/svelte/a11y.ts-to-jsdoc").ElementInteractivity} * @returns {ElementInteractivity[keyof ElementInteractivity]}
*/ */
export function element_interactivity(tag_name, attribute_map) { export function element_interactivity(tag_name, attribute_map) {
if ( if (
@ -208,7 +209,7 @@ export function element_interactivity(tag_name, attribute_map) {
/** /**
* @param {string} tag_name * @param {string} tag_name
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
* @returns {boolean} * @returns {boolean}
*/ */
export function is_interactive_element(tag_name, attribute_map) { export function is_interactive_element(tag_name, attribute_map) {
@ -217,7 +218,7 @@ export function is_interactive_element(tag_name, attribute_map) {
/** /**
* @param {string} tag_name * @param {string} tag_name
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
* @returns {boolean} * @returns {boolean}
*/ */
export function is_non_interactive_element(tag_name, attribute_map) { export function is_non_interactive_element(tag_name, attribute_map) {
@ -226,7 +227,7 @@ export function is_non_interactive_element(tag_name, attribute_map) {
/** /**
* @param {string} tag_name * @param {string} tag_name
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
* @returns {boolean} * @returns {boolean}
*/ */
export function is_static_element(tag_name, attribute_map) { export function is_static_element(tag_name, attribute_map) {
@ -234,9 +235,9 @@ export function is_static_element(tag_name, attribute_map) {
} }
/** /**
* @param {ARIARoleDefinitionKey} role * @param {import('aria-query').ARIARoleDefinitionKey} role
* @param {string} tag_name * @param {string} tag_name
* @param {Map<string, Attribute>} attribute_map * @param {Map<string, import('../nodes/Attribute.js').default>} attribute_map
*/ */
export function is_semantic_role_element(role, tag_name, attribute_map) { export function is_semantic_role_element(role, tag_name, attribute_map) {
for (const [schema, ax_object] of elementAXObjects.entries()) { for (const [schema, ax_object] of elementAXObjects.entries()) {

@ -2,8 +2,8 @@ import { p, x } from 'code-red';
import { string_literal } from './stringify.js'; import { string_literal } from './stringify.js';
/** /**
* @param {Map<string, Attribute>} values * @param {Map<string, import('../nodes/Attribute.js').default>} values
* @param {Block} block * @param {import('../render_dom/Block.js').default} block
*/ */
export default function get_slot_data(values, block = null) { export default function get_slot_data(values, block = null) {
return { return {
@ -25,8 +25,8 @@ export default function get_slot_data(values, block = null) {
} }
/** /**
* @param {Block} block * @param {import('../render_dom/Block.js').default} block
* @param {Attribute} attribute * @param {import('../nodes/Attribute.js').default} attribute
*/ */
function get_value(block, attribute) { function get_value(block, attribute) {
if (attribute.is_true) return x`true`; if (attribute.is_true) return x`true`;
@ -47,8 +47,8 @@ function get_value(block, attribute) {
} }
/** /**
* @param {Block} block * @param {import('../render_dom/Block.js').default} block
* @param {Attribute} attribute * @param {import('../nodes/Attribute.js').default} attribute
*/ */
function get_spread_value(block, attribute) { function get_spread_value(block, attribute) {
return block ? attribute.expression.manipulate(block) : attribute.expression.node; return block ? attribute.expression.manipulate(block) : attribute.expression.node;

@ -1,37 +1,47 @@
import { TemplateElement, TemplateLiteral } from 'estree';
import { MustacheTag, Text } from '../../interfaces';
/** /**
* Transforms a list of Text and MustacheTags into a TemplateLiteral expression. * Transforms a list of Text and MustacheTags into a TemplateLiteral expression.
* Start/End positions on the elements of the expression are not set. * Start/End positions on the elements of the expression are not set.
* @param {Array<Text | MustacheTag>} value
* @returns {import("C:/repos/svelte/svelte/node_modules/.pnpm/@types+estree@1.0.0/node_modules/@types/estree/index").TemplateLiteral}
*/ */
export function nodes_to_template_literal(value: Array<Text | MustacheTag>): TemplateLiteral { export function nodes_to_template_literal(value) {
const literal: TemplateLiteral = {
type: 'TemplateLiteral',
expressions: [],
quasis: []
};
let quasi: TemplateElement = { /**
type: 'TemplateElement', * @type {TemplateLiteral}
value: { raw: '', cooked: null }, */
tail: false const literal = {
}; type: 'TemplateLiteral',
expressions: [],
quasis: []
};
value.forEach((node) => { /**
if (node.type === 'Text') { * @type {TemplateElement}
quasi.value.raw += node.raw; */
} else if (node.type === 'MustacheTag') { let quasi = {
literal.quasis.push(quasi); type: 'TemplateElement',
literal.expressions.push(node.expression as any); value: { raw: '', cooked: null },
quasi = { tail: false
type: 'TemplateElement', };
value: { raw: '', cooked: null }, value.forEach((node) => {
tail: false if (node.type === 'Text') {
}; quasi.value.raw += node.raw;
} }
}); else if (node.type === 'MustacheTag') {
quasi.tail = true; literal.quasis.push(quasi);
literal.quasis.push(quasi); literal.expressions.push(node.expression);
return literal; quasi = {
type: 'TemplateElement',
value: { raw: '', cooked: null },
tail: false
};
}
});
quasi.tail = true;
literal.quasis.push(quasi);
return literal;
} }

Loading…
Cancel
Save