Skip to content

Commit

Permalink
Merge pull request #122 from eventualbuddha/fix-type-info
Browse files Browse the repository at this point in the history
Update types to better match reality.
  • Loading branch information
Rich-Harris committed May 30, 2017
2 parents 7ca62db + 1b169db commit b352395
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
14 changes: 8 additions & 6 deletions index.d.ts
Expand Up @@ -27,7 +27,7 @@ declare module "magic-string" {
clone(): Bundle;
generateMap(options?: Partial<SourceMapOptions>): SourceMap;
getIndentString(): string;
indent(indentStr: string): Bundle;
indent(indentStr?: string): Bundle;
prepend(str: string): Bundle;
toString(): string;
trimLines(): string;
Expand All @@ -36,13 +36,15 @@ declare module "magic-string" {
trimEnd(charType: string): Bundle;
}

export type ExclusionRange = [ number, number ];

export interface MagicStringOptions {
filename: string,
indentExclusionRanges: any; // TODO
indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
}

export interface IndentOptions {
exclude: any; // TODO
exclude: ExclusionRange | Array<ExclusionRange>;
indentStart: boolean;
}

Expand All @@ -58,11 +60,11 @@ declare module "magic-string" {
appendLeft(index: number, content: string): MagicString;
appendRight(index: number, content: string): MagicString;
clone(): MagicString;
generateMap(options?: Partial<SourceMapOptions>);
generateMap(options?: Partial<SourceMapOptions>): SourceMap;
getIndentString(): string;

indent(options?): MagicString;
indent(indentStr: string, options: IndentOptions): MagicString;
indent(options?: IndentOptions): MagicString;
indent(indentStr?: string, options?: IndentOptions): MagicString;

move(start: number, end: number, index: number): MagicString;
overwrite(start: number, end: number, content: string, options?: boolean | OverwriteOptions): MagicString;
Expand Down
4 changes: 1 addition & 3 deletions src/MagicString.js
Expand Up @@ -116,9 +116,7 @@ MagicString.prototype = {
cloned.lastChunk = clonedChunk;

if ( this.indentExclusionRanges ) {
cloned.indentExclusionRanges = typeof this.indentExclusionRanges[0] === 'number' ?
[ this.indentExclusionRanges[0], this.indentExclusionRanges[1] ] :
this.indentExclusionRanges.map( range => [ range.start, range.end ] );
cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
}

Object.keys( this.sourcemapLocations ).forEach( loc => {
Expand Down
13 changes: 13 additions & 0 deletions test/MagicString.js
Expand Up @@ -141,6 +141,19 @@ describe( 'MagicString', () => {
assert.deepEqual( source.indentExclusionRanges, clone.indentExclusionRanges );
});

it( 'should clone complex indentExclusionRanges', () => {
const array = [ [ 3, 6 ], [ 7, 9 ] ];
const source = new MagicString( 'abcdefghijkl', {
filename: 'foo.js',
indentExclusionRanges: array
});

const clone = source.clone();

assert.notStrictEqual( source.indentExclusionRanges, clone.indentExclusionRanges );
assert.deepEqual( source.indentExclusionRanges, clone.indentExclusionRanges );
});

it( 'should clone sourcemapLocations', () => {
const source = new MagicString( 'abcdefghijkl', {
filename: 'foo.js'
Expand Down

0 comments on commit b352395

Please sign in to comment.