Skip to content

Commit

Permalink
Merge pull request graphql#35 from wincent/glh/types
Browse files Browse the repository at this point in the history
Declare new Range and Position interfaces
  • Loading branch information
wincent authored Feb 14, 2017
2 parents 042ee58 + 7f71ca4 commit 15e09df
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
13 changes: 12 additions & 1 deletion src/types/Types.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@ import type {
GraphQLType,
} from 'graphql/type/definition';
import type CharacterStream from '../parser/CharacterStream';
import type {Position, Range} from '../utils/Range';

// online-parser related
export interface Position {
line: number,
character: number,
lessThanOrEqualTo: (position: Position) => boolean,
}

export interface Range {
start: Position,
end: Position,
containsPosition: (position: Position) => boolean,
}

export type ParseRule =
((token: Token, stream: CharacterStream) => ?string) |
Array<Rule | string>;
Expand Down
32 changes: 15 additions & 17 deletions src/utils/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
*/

import type {Location} from 'graphql/language';

export class Range {
start: Position;
end: Position;
constructor(start: Position, end: Position): void {
import type {
Range as RangeInterface,
Position as PositionInterface,
} from '../types/Types';

export class Range implements RangeInterface {
start: PositionInterface;
end: PositionInterface;
constructor(start: PositionInterface, end: PositionInterface): void {
this.start = start;
this.end = end;
}

containsPosition(position: Position): boolean {
containsPosition = (position: PositionInterface): boolean => {
const withinLine =
this.start.line <= position.line && this.end.line >= position.line;
const withinCharacter =
Expand All @@ -28,24 +32,18 @@ export class Range {
}
}

export class Position {
export class Position implements PositionInterface {
line: number;
character: number;
constructor(line: number, character: number): void {
this.line = line;
this.character = character;
}

lessThanOrEqualTo(position: Position): boolean {
if (
this.line < position.line ||
(this.line === position.line && this.character <= position.character)
) {
return true;
}

return false;
}
lessThanOrEqualTo = (position: PositionInterface): boolean => (
this.line < position.line ||
(this.line === position.line && this.character <= position.character)
);
}

export function offsetToPosition(text: string, loc: number): Position {
Expand Down

0 comments on commit 15e09df

Please sign in to comment.