Skip to content

Commit

Permalink
refactor(parse/{readStringLiteral,readNumberLiteral}): convert to ts
Browse files Browse the repository at this point in the history
- add TemplateElementType element type enum
- add new file for template type interfaces
  • Loading branch information
marcalexiei committed May 27, 2020
1 parent 16acead commit 5ae8072
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
64 changes: 64 additions & 0 deletions src/config/types.ts
@@ -1,3 +1,67 @@
enum TemplateElementType {
TEXT = 1,
INTERPOLATOR = 2,
TRIPLE = 3,
SECTION = 4,
INVERTED = 5,
CLOSING = 6,
ELEMENT = 7,
PARTIAL = 8,
COMMENT = 9,
DELIMCHANGE = 10,
ANCHOR = 11,
ATTRIBUTE = 13,
CLOSING_TAG = 14,
COMPONENT = 15,
YIELDER = 16,
INLINE_PARTIAL = 17,
DOCTYPE = 18,
ALIAS = 19,

AWAIT = 55,

NUMBER_LITERAL = 20,
STRING_LITERAL = 21,
ARRAY_LITERAL = 22,
OBJECT_LITERAL = 23,
BOOLEAN_LITERAL = 24,
REGEXP_LITERAL = 25,

GLOBAL = 26,
KEY_VALUE_PAIR = 27,

REFERENCE = 30,
REFINEMENT = 31,
MEMBER = 32,
PREFIX_OPERATOR = 33,
BRACKETED = 34,
CONDITIONAL = 35,
INFIX_OPERATOR = 36,

INVOCATION = 40,

SECTION_IF = 50,
SECTION_UNLESS = 51,
SECTION_EACH = 52,
SECTION_WITH = 53,
SECTION_IF_WITH = 54,

ELSE = 60,
ELSEIF = 61,
THEN = 62,
CATCH = 63,

EVENT = 70,
DECORATOR = 71,
TRANSITION = 72,
BINDING_FLAG = 73,
DELEGATE_FLAG = 74
}

export default TemplateElementType;

// todo remove const and use enum

export const TEXT = 1;
export const INTERPOLATOR = 2;
export const TRIPLE = 3;
Expand Down
@@ -1,14 +1,16 @@
import { NUMBER_LITERAL } from '../../../../../config/types';
import TemplateElementType from 'config/types';
import { LiteralTemplateElement } from 'parse/templateElements';

// bulletproof number regex from https://gist.github.com/Rich-Harris/7544330
const numberPattern = /^(?:[+-]?)0*(?:(?:(?:[1-9]\d*)?\.\d+)|(?:(?:0|[1-9]\d*)\.)|(?:0|[1-9]\d*))(?:[eE][+-]?\d+)?/;

export default function readNumberLiteral(parser) {
// todo add correct type on Parser
export default function readNumberLiteral(parser): LiteralTemplateElement {
let result;

if ((result = parser.matchPattern(numberPattern))) {
return {
t: NUMBER_LITERAL,
t: TemplateElementType.NUMBER_LITERAL,
v: result
};
}
Expand Down
@@ -1,10 +1,12 @@
import { STRING_LITERAL } from '../../../../../config/types';
import makeQuotedStringMatcher from './stringLiteral/makeQuotedStringMatcher';
import TemplateElementType from 'config/types';
import { LiteralTemplateElement } from 'parse/templateElements';

const singleMatcher = makeQuotedStringMatcher(`"`);
const doubleMatcher = makeQuotedStringMatcher(`'`);

export default function(parser) {
// todo add correct type on Parser
export default function(parser): LiteralTemplateElement {
const start = parser.pos;
const quote = parser.matchString(`'`) || parser.matchString(`"`);

Expand All @@ -17,7 +19,7 @@ export default function(parser) {
}

return {
t: STRING_LITERAL,
t: TemplateElementType.STRING_LITERAL,
v: string
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/parse/templateElements.ts
@@ -0,0 +1,6 @@
import TemplateElementType from 'config/types';

export interface LiteralTemplateElement {
t: TemplateElementType.STRING_LITERAL | TemplateElementType.NUMBER_LITERAL;
v: string;
}

0 comments on commit 5ae8072

Please sign in to comment.