Skip to content

Commit

Permalink
IDENTIFIER_REGEX
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang Zhe Jiet authored and Chang Zhe Jiet committed Jan 11, 2024
1 parent 344c023 commit b9ae1c5
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/blocks/Block.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -140,7 +140,7 @@ export abstract class Block<Args extends TerraformArgs = TerraformArgs> 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}`);
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/utils/Util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Block } from '../blocks';
import { TerraformArgs, TerraformElement } from '.';
import { IDENTIFIER_REGEX, TerraformArgs, TerraformElement } from '.';

export class Util {

Expand Down Expand Up @@ -37,7 +37,6 @@ export class Util {
return true;
}
throw new Error(`Invalid value: ${value}`);

}

static argumentToString(key: string, value: any): string {
Expand All @@ -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}"`;
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const IDENTIFIER_REGEX = /^[a-zA-Z_\-]{1}[0-9a-zA-Z_\-]*$/;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './constants';
export * from './types';
export * from './TerraformElement';
export * from './Util';

0 comments on commit b9ae1c5

Please sign in to comment.