From 345f0b4e17533f97929c0bc8ca79d8961f3c728b Mon Sep 17 00:00:00 2001 From: azu Date: Sat, 24 Feb 2018 14:56:21 +0900 Subject: [PATCH 1/2] Add structured-source definitions --- types/structured-source/index.d.ts | 29 +++++++++++++++++++ .../structured-source-tests.ts | 15 ++++++++++ types/structured-source/tsconfig.json | 23 +++++++++++++++ types/structured-source/tslint.json | 1 + 4 files changed, 68 insertions(+) create mode 100644 types/structured-source/index.d.ts create mode 100644 types/structured-source/structured-source-tests.ts create mode 100644 types/structured-source/tsconfig.json create mode 100644 types/structured-source/tslint.json diff --git a/types/structured-source/index.d.ts b/types/structured-source/index.d.ts new file mode 100644 index 0000000000..ce7325cf9d --- /dev/null +++ b/types/structured-source/index.d.ts @@ -0,0 +1,29 @@ +// Type definitions for structured-source 3.0 +// Project: https://github.com/Constellation/structured-source +// Definitions by: azu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace StructuredSource { + interface SourcePosition { + // Line number starts with 1. + line: number; + // Column number starts with 0. + column: number; + } + interface SourceLocation { + start: SourcePosition; + end: SourcePosition; + } +} + +declare class StructuredSource { + /** + * @param source - source code text. + */ + constructor(source: string); + locationToRange(loc: StructuredSource.SourceLocation): [number, number]; + rangeToLocation(range: [number, number]): StructuredSource.SourceLocation; + positionToIndex(pos: StructuredSource.SourcePosition): number; + indexToPosition(index: number): StructuredSource.SourcePosition; +} +export = StructuredSource; diff --git a/types/structured-source/structured-source-tests.ts b/types/structured-source/structured-source-tests.ts new file mode 100644 index 0000000000..498cd7ddf2 --- /dev/null +++ b/types/structured-source/structured-source-tests.ts @@ -0,0 +1,15 @@ +import StructuredSource = require('structured-source'); + +const src = new StructuredSource('aaa\u2028aaaa\u2029aaaaa\n'); + +// positionToIndex({ line: number, column: number) -> number +src.positionToIndex({ line: 1, column: 2 }); +// indexToPosition(number) -> { line: number, column: number } +src.indexToPosition(2); +// rangeToLocation([ number, number ]) -> { start: { line: number, column: number}, end: { line: number, column: number } } +src.rangeToLocation([0, 2]); +// locationToRange({ start: { line: number, column: number}, end: { line: number, column: number } }) -> [ number, number ] +src.locationToRange({ + start: { line: 1, column: 0 }, + end: { line: 1, column: 2 } +}); diff --git a/types/structured-source/tsconfig.json b/types/structured-source/tsconfig.json new file mode 100644 index 0000000000..f0d949503e --- /dev/null +++ b/types/structured-source/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "structured-source-tests.ts" + ] +} diff --git a/types/structured-source/tslint.json b/types/structured-source/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/structured-source/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 9550c81046aef251d46c1b9b7570676cdbf732b9 Mon Sep 17 00:00:00 2001 From: azu Date: Sun, 25 Feb 2018 14:27:56 +0900 Subject: [PATCH 2/2] Add type assertion --- types/structured-source/structured-source-tests.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/structured-source/structured-source-tests.ts b/types/structured-source/structured-source-tests.ts index 498cd7ddf2..cda6e99af2 100644 --- a/types/structured-source/structured-source-tests.ts +++ b/types/structured-source/structured-source-tests.ts @@ -2,13 +2,13 @@ import StructuredSource = require('structured-source'); const src = new StructuredSource('aaa\u2028aaaa\u2029aaaaa\n'); -// positionToIndex({ line: number, column: number) -> number +// $ExpectType: number src.positionToIndex({ line: 1, column: 2 }); -// indexToPosition(number) -> { line: number, column: number } +// $ExpectType: { line: number, column: number } src.indexToPosition(2); -// rangeToLocation([ number, number ]) -> { start: { line: number, column: number}, end: { line: number, column: number } } +// $ExpectType: { start: { line: number, column: number}, end: { line: number, column: number } } src.rangeToLocation([0, 2]); -// locationToRange({ start: { line: number, column: number}, end: { line: number, column: number } }) -> [ number, number ] +// $ExpectType: [ number, number ] src.locationToRange({ start: { line: 1, column: 0 }, end: { line: 1, column: 2 }