fix: replaceAll and use regex

pull/11326/head
paoloricciuti 2 years ago
parent ec329a954d
commit 41360331a9

@ -4,6 +4,7 @@ import * as b from '../../../../utils/builders.js';
import * as assert from '../../../../utils/assert.js'; import * as assert from '../../../../utils/assert.js';
import { get_prop_source, is_state_source, should_proxy_or_freeze } from '../utils.js'; import { get_prop_source, is_state_source, should_proxy_or_freeze } from '../utils.js';
import { extract_paths } from '../../../../utils/ast.js'; import { extract_paths } from '../../../../utils/ast.js';
import { regex_invalid_identifier_chars } from '../../../patterns.js';
/** @type {import('../types.js').ComponentVisitors} */ /** @type {import('../types.js').ComponentVisitors} */
export const javascript_visitors_runes = { export const javascript_visitors_runes = {
@ -67,7 +68,9 @@ export const javascript_visitors_runes = {
create_state_field(definition, is_private, name); create_state_field(definition, is_private, name);
} else if (definition.type === 'PropertyDefinition' && definition.key.type === 'Literal') { } else if (definition.type === 'PropertyDefinition' && definition.key.type === 'Literal') {
const name = definition.key.value?.toString().replace('-', '_'); const name = definition.key.value
?.toString()
.replaceAll(regex_invalid_identifier_chars, '_');
if (name) create_state_field(definition, false, name); if (name) create_state_field(definition, false, name);
} }
} }
@ -191,7 +194,9 @@ export const javascript_visitors_runes = {
continue; continue;
} }
} else if (definition.type === 'PropertyDefinition' && definition.key.type === 'Literal') { } else if (definition.type === 'PropertyDefinition' && definition.key.type === 'Literal') {
const name = definition.key.value?.toString().replace('-', '_'); const name = definition.key.value
?.toString()
.replaceAll(regex_invalid_identifier_chars, '_');
if (name && replace_class_body(definition, false, name)) { if (name && replace_class_body(definition, false, name)) {
continue; continue;

@ -15,6 +15,8 @@ export const regex_only_whitespaces = /^[ \t\n\r\f]+$/;
export const regex_not_newline_characters = /[^\n]/g; export const regex_not_newline_characters = /[^\n]/g;
export const regex_is_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/; export const regex_is_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
// used in replace all to remove all invalid chars from a literal identifier
export const regex_invalid_identifier_chars = /(^[^a-zA-Z_$]|[^a-zA-Z0-9_$])/g;
export const regex_starts_with_vowel = /^[aeiou]/; export const regex_starts_with_vowel = /^[aeiou]/;
export const regex_heading_tags = /^h[1-6]$/; export const regex_heading_tags = /^h[1-6]$/;

Loading…
Cancel
Save