[Pkg-javascript-commits] [node-magic-string] 01/03: New upstream version 0.22.3

Julien Puydt julien.puydt at laposte.net
Sun Jul 23 14:25:15 UTC 2017


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-magic-string.

commit 01095dcdb8ff5849fea2f87a2e906b86a89e48e6
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sun Jul 23 11:35:05 2017 +0200

    New upstream version 0.22.3
---
 .eslintrc                                  |   1 +
 CHANGELOG.md                               |  17 ++++
 index.d.ts                                 | 136 +++++++++++++++--------------
 package.json                               |   2 +-
 src/MagicString.js                         |  34 ++++----
 test/MagicString.js                        |  11 ++-
 test/utils/IntegrityCheckingMagicString.js |  46 ++++++++++
 7 files changed, 161 insertions(+), 86 deletions(-)

diff --git a/.eslintrc b/.eslintrc
index 7ed2294..704eeee 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -6,6 +6,7 @@
         "keyword-spacing": [ 2, { "before": true, "after": true } ],
         "space-before-blocks": [ 2, "always" ],
         "space-before-function-paren": [ 2, "always" ],
+        "space-in-parens": [ 2, "always" ],
         "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
         "object-shorthand":  [2, "always" ],
         "no-const-assign": 2,
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eda770a..0f11107 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,22 @@
 # magic-string changelog
 
+## 0.22.3
+
+* Add `original` to TS definitions
+
+## 0.22.2
+
+* Avoid `declare module` ([#127](https://github.com/Rich-Harris/magic-string/pull/127))
+
+## 0.22.1
+
+* Update TypeScript definitions ([#124](https://github.com/Rich-Harris/magic-string/pull/124))
+
+## 0.22.0
+
+* Prevent `overwrite` state corruption ([#115](https://github.com/Rich-Harris/magic-string/issues/115))
+* Various bugfixes ([#126](https://github.com/Rich-Harris/magic-string/pull/126))
+
 ## 0.21.3
 
 * Clone `indentExclusionRanges` correctly ([#122](https://github.com/Rich-Harris/magic-string/pull/122))
diff --git a/index.d.ts b/index.d.ts
index ebc9c1d..9c95bc4 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,78 +1,80 @@
-declare module "magic-string" {
-  export interface BundleOptions {
-    intro?: string;
-    separator?: string;
-  }
+export interface BundleOptions {
+  intro?: string;
+  separator?: string;
+}
 
-  export interface SourceMapOptions {
-    hires: boolean;
-    file: string;
-    sources: string[];
-    sourcesContent: string;
-    includeContent: boolean;
-    names: string[];
-    mappings: string[];
-  }
+export interface SourceMapOptions {
+  hires: boolean;
+  file: string;
+  source: string;
+  includeContent: boolean;
+}
 
-  class SourceMap {
-    constructor(properties: SourceMapOptions);
-    toString(): string;
-    toUrl(): string;
-  }
+export interface SourceMap {
+  file: string;
+  sources: string[];
+  sourcesContent: string;
+  names: string[];
+  mappings: string[];
 
-  export class Bundle {
-    constructor(options?: BundleOptions);
-    addSource(source: MagicString | { filename?: string, content: MagicString }): Bundle;
-    append(str: string, options: BundleOptions): Bundle;
-    clone(): Bundle;
-    generateMap(options?: Partial<SourceMapOptions>): SourceMap;
-    getIndentString(): string;
-    indent(indentStr?: string): Bundle;
-    prepend(str: string): Bundle;
-    toString(): string;
-    trimLines(): string;
-    trim(charType: string): string;
-    trimStart(charType: string): Bundle;
-    trimEnd(charType: string): Bundle;
-  }
+  toString(): string;
+  toUrl(): string;
+}
 
-  export type ExclusionRange = [ number, number ];
+export class Bundle {
+  constructor(options?: BundleOptions);
+  addSource(source: MagicString | { filename?: string, content: MagicString }): Bundle;
+  append(str: string, options: BundleOptions): Bundle;
+  clone(): Bundle;
+  generateMap(options?: Partial<SourceMapOptions>): SourceMap;
+  getIndentString(): string;
+  indent(indentStr?: string): Bundle;
+  prepend(str: string): Bundle;
+  toString(): string;
+  trimLines(): string;
+  trim(charType: string): string;
+  trimStart(charType: string): Bundle;
+  trimEnd(charType: string): Bundle;
+}
 
-  export interface MagicStringOptions {
-    filename: string,
-    indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
-  }
+export type ExclusionRange = [ number, number ];
 
-  export interface IndentOptions {
-    exclude: ExclusionRange | Array<ExclusionRange>;
-    indentStart: boolean;
-  }
+export interface MagicStringOptions {
+  filename: string,
+  indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
+}
+
+export interface IndentOptions {
+  exclude: ExclusionRange | Array<ExclusionRange>;
+  indentStart: boolean;
+}
+
+export interface OverwriteOptions {
+  storeName: boolean;
+  contentOnly: boolean;
+}
 
-  export interface OverwriteOptions {
-    storeName: boolean;
-    contentOnly: boolean;
-  }
+export default class MagicString {
+  constructor(str: string, options?: MagicStringOptions);
+  addSourcemapLocation(char: number): void;
+  append(content: string): MagicString;
+  appendLeft(index: number, content: string): MagicString;
+  appendRight(index: number, content: string): MagicString;
+  clone(): MagicString;
+  generateMap(options?: Partial<SourceMapOptions>): SourceMap;
+  getIndentString(): string;
 
-  export default class MagicString {
-    constructor(str: string, options?: MagicStringOptions);
-    addSourcemapLocation(char: number): void;
-    append(content: string): MagicString;
-    appendLeft(index: number, content: string): MagicString;
-    appendRight(index: number, content: string): MagicString;
-    clone(): MagicString;
-    generateMap(options?: Partial<SourceMapOptions>): SourceMap;
-    getIndentString(): string;
+  indent(options?: IndentOptions): 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;
+  prepend(content: string): MagicString;
+  prependLeft(index: number, content: string): MagicString;
+  prependRight(index: number, content: string): MagicString;
+  remove(start: number, end: number): MagicString;
+  slice(start: number, end: number): string;
+  snip(start: number, end: number): MagicString;
 
-    move(start: number, end: number, index: number): MagicString;
-    overwrite(start: number, end: number, content: string, options?: boolean | OverwriteOptions): MagicString;
-    prepend(content: string): MagicString;
-    prependLeft(index: number, content: string): MagicString;
-    prependRight(index: number, content: string): MagicString;
-    remove(start: number, end: number): MagicString;
-    slice(start: number, end: number): string;
-    snip(start: number, end: number): MagicString;
-  }
+  original: string;
 }
diff --git a/package.json b/package.json
index 98a57f7..657aa94 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
   "name": "magic-string",
   "description": "Modify strings, generate sourcemaps",
   "author": "Rich Harris",
-  "version": "0.21.3",
+  "version": "0.22.3",
   "repository": "https://github.com/rich-harris/magic-string",
   "main": "dist/magic-string.cjs.js",
   "module": "dist/magic-string.es.js",
diff --git a/src/MagicString.js b/src/MagicString.js
index d20c946..ea3b36e 100644
--- a/src/MagicString.js
+++ b/src/MagicString.js
@@ -235,13 +235,9 @@ MagicString.prototype = {
 							if ( charIndex === chunk.start ) {
 								chunk.prependRight( indentStr );
 							} else {
-								const rhs = chunk.split( charIndex );
-								rhs.prependRight( indentStr );
-
-								this.byStart[ charIndex ] = rhs;
-								this.byEnd[ charIndex ] = chunk;
-
-								chunk = rhs;
+								this._splitChunk( chunk, charIndex );
+								chunk = chunk.next;
+								chunk.prependRight( indentStr );
 							}
 						}
 					}
@@ -313,7 +309,7 @@ MagicString.prototype = {
 		}
 
 		first.previous = newLeft;
-		last.next = newRight;
+		last.next = newRight || null;
 
 		if ( !newLeft ) this.firstChunk = first;
 		if ( !newRight ) this.lastChunk = last;
@@ -362,15 +358,15 @@ MagicString.prototype = {
 
 			first.edit( content, storeName, contentOnly );
 
-			if ( last ) {
-				first.next = last.next;
-			} else {
-				first.next = null;
-				this.lastChunk = first;
-			}
+			if ( first !== last ) {
+				let chunk = first.next;
+				while ( chunk !== last ) {
+					chunk.edit( '', false );
+					chunk = chunk.next;
+				}
 
-			first.original = this.original.slice( start, end );
-			first.end = end;
+				chunk.edit( '', false );
+			}
 		}
 
 		else {
@@ -586,10 +582,13 @@ MagicString.prototype = {
 
 			// if chunk was trimmed, we have a new lastChunk
 			if ( chunk.end !== end ) {
-				this.lastChunk = chunk.next;
+				if ( this.lastChunk === chunk ) {
+					this.lastChunk = chunk.next;
+				}
 
 				this.byEnd[ chunk.end ] = chunk;
 				this.byStart[ chunk.next.start ] = chunk.next;
+				this.byEnd[ chunk.next.end ] = chunk.next;
 			}
 
 			if ( aborted ) return this;
@@ -617,6 +616,7 @@ MagicString.prototype = {
 
 				this.byEnd[ chunk.end ] = chunk;
 				this.byStart[ chunk.next.start ] = chunk.next;
+				this.byEnd[ chunk.next.end ] = chunk.next;
 			}
 
 			if ( aborted ) return this;
diff --git a/test/MagicString.js b/test/MagicString.js
index c9388e9..c4f31f0 100644
--- a/test/MagicString.js
+++ b/test/MagicString.js
@@ -1,6 +1,6 @@
 const assert = require( 'assert' );
 const SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;
-const MagicString = require( '../' );
+const MagicString = require( './utils/IntegrityCheckingMagicString' );
 
 require( 'source-map-support' ).install();
 require( 'console-group' ).install();
@@ -808,6 +808,15 @@ describe( 'MagicString', () => {
 			s.move( 6, 9, 3 );
 			assert.throws( () => s.overwrite( 5, 7, 'XX' ), /Cannot overwrite across a split point/ );
 		});
+
+		it ( 'allows later insertions at the end', () => {
+			const s = new MagicString( 'abcdefg' );
+
+			s.appendLeft(4, '(');
+			s.overwrite( 2, 7, '' );
+			s.appendLeft(7, 'h');
+			assert.equal( s.toString(), 'abh' );
+		});
 	});
 
 	describe( 'prepend', () => {
diff --git a/test/utils/IntegrityCheckingMagicString.js b/test/utils/IntegrityCheckingMagicString.js
new file mode 100644
index 0000000..9290a26
--- /dev/null
+++ b/test/utils/IntegrityCheckingMagicString.js
@@ -0,0 +1,46 @@
+const MagicString = require( '../../' );
+const assert = require( 'assert' );
+
+class IntegrityCheckingMagicString extends MagicString {
+	checkIntegrity () {
+		let prevChunk = null;
+		let chunk = this.firstChunk;
+		let numNodes = 0;
+		while ( chunk ) {
+			assert.strictEqual( this.byStart[chunk.start], chunk );
+			assert.strictEqual( this.byEnd[chunk.end], chunk );
+			assert.strictEqual( chunk.previous, prevChunk );
+			if ( prevChunk ) {
+				assert.strictEqual( prevChunk.next, chunk );
+			}
+			prevChunk = chunk;
+			chunk = chunk.next;
+			numNodes++;
+		}
+		assert.strictEqual( prevChunk, this.lastChunk );
+		assert.strictEqual( this.lastChunk.next, null );
+		assert.strictEqual( Object.keys(this.byStart).length, numNodes );
+		assert.strictEqual( Object.keys(this.byEnd).length, numNodes );
+	}
+}
+
+for (const key in MagicString.prototype) {
+	if ( !MagicString.prototype.hasOwnProperty( key ) ) {
+		continue;
+	}
+	const func = MagicString.prototype[key];
+	if ( typeof func === 'function' ) {
+		IntegrityCheckingMagicString.prototype[key] = function () {
+			const result = func.apply( this, arguments );
+			try {
+				this.checkIntegrity();
+			} catch ( e ) {
+				e.message = `Integrity error after invoking ${key}:\n${e.message}`;
+				throw e;
+			}
+			return result;
+		};
+	}
+}
+
+module.exports = IntegrityCheckingMagicString;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-magic-string.git



More information about the Pkg-javascript-commits mailing list