Skip to content

Commit

Permalink
Add type annotations to all tokenizaton-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
ariya committed Jan 3, 2017
1 parent 0cdc8e0 commit cf23575
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 108 deletions.
5 changes: 3 additions & 2 deletions src/comment-handler.ts
@@ -1,10 +1,11 @@
import { SourceLocation } from './scanner';
import { Syntax } from './syntax';

interface Comment {
type: string;
value: string;
range?: any;
loc?: any;
range?: [number, number];
loc?: SourceLocation;
}

interface Entry {
Expand Down
27 changes: 20 additions & 7 deletions src/jsx-parser.ts
Expand Up @@ -24,6 +24,15 @@ enum JSXToken {
Text
}

interface RawJSXToken {
type: Token | JSXToken;
value: string;
lineNumber: number;
lineStart: number;
start: number;
end: number;
}

TokenName[JSXToken.Identifier] = 'JSXIdentifier';
TokenName[JSXToken.Text] = 'JSXText';

Expand Down Expand Up @@ -103,7 +112,7 @@ export class JSXParser extends Parser {
};
}

scanXHTMLEntity(quote: string) {
scanXHTMLEntity(quote: string): string {
let result = '&';

let valid = true;
Expand Down Expand Up @@ -158,7 +167,7 @@ export class JSXParser extends Parser {

// Scan the next JSX token. This replaces Scanner#lex when in JSX mode.

lexJSX(): any {
lexJSX(): RawJSXToken {
const cp = this.scanner.source.charCodeAt(this.scanner.index);

// < > / : = { }
Expand Down Expand Up @@ -222,6 +231,7 @@ export class JSXParser extends Parser {
// Only placeholder, since it will be rescanned as a real assignment expression.
return {
type: Token.Template,
value: '',
lineNumber: this.scanner.lineNumber,
lineStart: this.scanner.lineStart,
start: this.scanner.index,
Expand Down Expand Up @@ -256,9 +266,12 @@ export class JSXParser extends Parser {
}

this.scanner.throwUnexpectedToken();

/* istanbul ignore next */
throw new Error('Unreachable');
}

nextJSXToken() {
nextJSXToken(): RawJSXToken {
this.collectComments();

this.startMarker.index = this.scanner.index;
Expand All @@ -270,13 +283,13 @@ export class JSXParser extends Parser {
this.lastMarker.lineStart = this.scanner.lineStart;

if (this.config.tokens) {
this.tokens.push(this.convertToken(token));
this.tokens.push(this.convertToken(token as any));
}

return token;
}

nextJSXText() {
nextJSXText(): RawJSXToken {
this.startMarker.index = this.scanner.index;
this.startMarker.lineNumber = this.scanner.lineNumber;
this.startMarker.lineStart = this.scanner.lineStart;
Expand Down Expand Up @@ -314,13 +327,13 @@ export class JSXParser extends Parser {
};

if ((text.length > 0) && this.config.tokens) {
this.tokens.push(this.convertToken(token));
this.tokens.push(this.convertToken(token as any));
}

return token;
}

peekJSXToken() {
peekJSXToken(): RawJSXToken {
const previousIndex = this.scanner.index;
const previousLineNumber = this.scanner.lineNumber;
const previousLineStart = this.scanner.lineStart;
Expand Down
8 changes: 4 additions & 4 deletions src/nodes.ts
Expand Up @@ -618,14 +618,14 @@ export class Property {

export class RegexLiteral {
readonly type: string;
readonly value: string;
readonly value: RegExp;
readonly raw: string;
readonly regex: any;
constructor(value: string, raw: string, regex) {
readonly regex: { pattern: string, flags: string };
constructor(value: RegExp, raw: string, pattern: string, flags: string) {
this.type = Syntax.Literal;
this.value = value;
this.raw = raw;
this.regex = regex;
this.regex = { pattern, flags };
}
}

Expand Down

0 comments on commit cf23575

Please sign in to comment.