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..cda6e99af2 --- /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'); + +// $ExpectType: number +src.positionToIndex({ line: 1, column: 2 }); +// $ExpectType: { line: number, column: number } +src.indexToPosition(2); +// $ExpectType: { start: { line: number, column: number}, end: { line: number, column: number } } +src.rangeToLocation([0, 2]); +// $ExpectType: [ 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" }