From b9ae1c5e96644be1359033220e7905e091651b23 Mon Sep 17 00:00:00 2001 From: Chang Zhe Jiet Date: Thu, 11 Jan 2024 14:01:02 +0800 Subject: [PATCH] IDENTIFIER_REGEX --- src/blocks/Block.ts | 4 ++-- src/utils/Util.ts | 6 ++---- src/utils/constants.ts | 1 + src/utils/index.ts | 1 + 4 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 src/utils/constants.ts diff --git a/src/blocks/Block.ts b/src/blocks/Block.ts index d51a143..ae4b44c 100644 --- a/src/blocks/Block.ts +++ b/src/blocks/Block.ts @@ -1,5 +1,5 @@ import { Argument, Attribute } from '../arguments'; -import { TerraformArgs, TerraformElement, Util } from '../utils'; +import { IDENTIFIER_REGEX, TerraformArgs, TerraformElement, Util } from '../utils'; /** * @category Block @@ -140,7 +140,7 @@ export abstract class Block extends } #validateIdentifier(identifier: string): void { - if (!identifier.match(/^[a-zA-Z_\-]{1}[0-9a-zA-Z_\-]*$/)) { + if (!identifier.match(IDENTIFIER_REGEX)) { throw new Error(`Invalid identifier: ${identifier}`); } } diff --git a/src/utils/Util.ts b/src/utils/Util.ts index bc1bb67..f9afd27 100644 --- a/src/utils/Util.ts +++ b/src/utils/Util.ts @@ -1,5 +1,5 @@ import { Block } from '../blocks'; -import { TerraformArgs, TerraformElement } from '.'; +import { IDENTIFIER_REGEX, TerraformArgs, TerraformElement } from '.'; export class Util { @@ -37,7 +37,6 @@ export class Util { return true; } throw new Error(`Invalid value: ${value}`); - } static argumentToString(key: string, value: any): string { @@ -46,8 +45,7 @@ export class Util { return ''; } - // Escape key if it contains special characters. - if (!key.match(/^[a-zA-Z0-9_]+$/)) { + if (!key.match(IDENTIFIER_REGEX)) { key = `"${key}"`; } diff --git a/src/utils/constants.ts b/src/utils/constants.ts new file mode 100644 index 0000000..99f93a2 --- /dev/null +++ b/src/utils/constants.ts @@ -0,0 +1 @@ +export const IDENTIFIER_REGEX = /^[a-zA-Z_\-]{1}[0-9a-zA-Z_\-]*$/; diff --git a/src/utils/index.ts b/src/utils/index.ts index 0f1f502..2a9a9d7 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,4 @@ +export * from './constants'; export * from './types'; export * from './TerraformElement'; export * from './Util';