Skip to content

Commit 27404ac

Browse files
committed
Merge pull request #30 from Rich-Harris/patches
Patches
2 parents 0dea7ba + a3bd72c commit 27404ac

8 files changed

Lines changed: 507 additions & 519 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# changelog
22

3+
## 0.10.0
4+
5+
* Complete rewrite, resulting in ~40x speed increase ([#30](https://github.com/Rich-Harris/magic-string/pull/30))
6+
* Breaking – `magicString.locate` and `locateOrigin` are deprecated
7+
* More forgiving rules about contiguous patches, and which ranges are valid with `magicString.slice(...)`
8+
39
## 0.9.1
410

511
* Update deps

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ var s = new MagicString( 'problems = 99' );
5252

5353
s.overwrite( 0, 8, 'answer' );
5454
s.toString(); // 'answer = 99'
55-
s.locate( 9 ); // 7 - the character originally at index 9 ('=') is now at index 7
56-
s.locateOrigin( 7 ); // 9
5755

5856
s.overwrite( 11, 13, '42' ); // character indices always refer to the original string
5957
s.toString(); // 'answer = 42'
@@ -126,11 +124,11 @@ Inserts the specified `content` at the `index` in the original string. Returns `
126124

127125
### s.locate( index )
128126

129-
Finds the location, in the generated string, of the character at `index` in the original string. Returns `null` if the character in question has been removed or overwritten.
127+
**DEPRECATED** since 0.10 – see [#30](https://github.com/Rich-Harris/magic-string/pull/30)
130128

131129
### s.locateOrigin( index )
132130

133-
The opposite of `s.locate()`. Returns `null` if the character in question was inserted with `s.append()`, `s.prepend()` or `s.overwrite()`.
131+
**DEPRECATED** since 0.10 – see [#30](https://github.com/Rich-Harris/magic-string/pull/30)
134132

135133
### s.overwrite( start, end, content[, storeName] )
136134

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magic-string",
33
"description": "Modify strings, generate sourcemaps",
44
"author": "Rich Harris",
5-
"version": "0.9.1",
5+
"version": "0.10.0",
66
"repository": "https://github.com/rich-harris/magic-string",
77
"main": "dist/magic-string.cjs.js",
88
"jsnext:main": "dist/magic-string.es6.js",
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"babel-preset-es2015-rollup": "^1.0.0",
1515
"codecov.io": "^0.1.6",
16+
"console-group": "^0.1.2",
1617
"es6-promise": "^3.0.2",
1718
"eslint": "^1.10.3",
1819
"istanbul": "^0.4.0",
@@ -22,7 +23,8 @@
2223
"rollup": "^0.22.0",
2324
"rollup-plugin-babel": "^2.2.0",
2425
"rollup-plugin-npm": "^1.1.0",
25-
"source-map": "^0.5.3"
26+
"source-map": "^0.5.3",
27+
"source-map-support": "^0.4.0"
2628
},
2729
"keywords": [
2830
"string",
@@ -33,13 +35,16 @@
3335
],
3436
"scripts": {
3537
"test": "mocha",
36-
"pretest": "npm run build",
37-
"pretest-coverage": "npm run build",
38+
"pretest": "npm run build:cjs",
39+
"pretest-coverage": "npm run build:cjs",
3840
"test-coverage": "rm -rf coverage/* && istanbul cover --report json node_modules/.bin/_mocha -- -u exports -R spec test/index.js",
3941
"posttest-coverage": "remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.json -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.lcov -t lcovonly -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped -t html -b dist",
4042
"ci": "npm run test-coverage && codecov < coverage/coverage-remapped.lcov",
41-
"build": "rm -rf dist && rollup -c -f cjs -o dist/magic-string.cjs.js && rollup -c -f es6 -o dist/magic-string.es6.js && export DEPS=true && rollup -c -f umd -o dist/magic-string.umd.js",
42-
"prepublish": "npm test",
43+
"build:cjs": "rollup -c -f cjs -o dist/magic-string.cjs.js",
44+
"build:es6": "rollup -c -f es6 -o dist/magic-string.es6.js",
45+
"build:umd": "export DEPS=true && rollup -c -f umd -o dist/magic-string.umd.js",
46+
"build": " npm run build:cjs && npm run build:es6 && npm run build:umd",
47+
"prepublish": "rm -rf dist && npm test && npm run build:es6 && npm run build:umd",
4348
"lint": "eslint src"
4449
},
4550
"files": [

src/Bundle.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import isObject from './utils/isObject.js';
66

77
export default function Bundle ( options = {} ) {
88
this.intro = options.intro || '';
9-
this.outro = options.outro || '';
109
this.separator = options.separator !== undefined ? options.separator : '\n';
1110

1211
this.sources = [];
@@ -65,7 +64,6 @@ Bundle.prototype = {
6564
clone () {
6665
const bundle = new Bundle({
6766
intro: this.intro,
68-
outro: this.outro,
6967
separator: this.separator
7068
});
7169

@@ -85,8 +83,7 @@ Bundle.prototype = {
8583

8684
let names = [];
8785
this.sources.forEach( source => {
88-
Object.keys( source.content.nameLocations ).forEach( location => {
89-
const name = source.content.nameLocations[ location ];
86+
Object.keys( source.content.storedNames ).forEach( name => {
9087
if ( !~names.indexOf( name ) ) names.push( name );
9188
});
9289
});
@@ -106,8 +103,7 @@ Bundle.prototype = {
106103
}
107104

108105
return prefix + mappings;
109-
}).join( '' ) +
110-
getSemis( this.outro )
106+
}).join( '' )
111107
);
112108

113109
return new SourceMap({
@@ -158,7 +154,8 @@ Bundle.prototype = {
158154
indentStart//: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
159155
});
160156

161-
trailingNewline = source.content.str.slice( 0, -1 ) === '\n';
157+
// TODO this is a very slow way to determine this
158+
trailingNewline = source.content.toString().slice( 0, -1 ) === '\n';
162159
});
163160

164161
if ( this.intro ) {
@@ -167,8 +164,6 @@ Bundle.prototype = {
167164
});
168165
}
169166

170-
this.outro = this.outro.replace( /^[^\n]/gm, indentStr + '$&' );
171-
172167
return this;
173168
},
174169

@@ -185,7 +180,7 @@ Bundle.prototype = {
185180
return str;
186181
}).join( '' );
187182

188-
return this.intro + body + this.outro;
183+
return this.intro + body;
189184
},
190185

191186
trimLines () {
@@ -201,45 +196,41 @@ Bundle.prototype = {
201196
this.intro = this.intro.replace( rx, '' );
202197

203198
if ( !this.intro ) {
204-
let source; // TODO put inside loop if safe
199+
let source;
205200
let i = 0;
206201

207202
do {
208203
source = this.sources[i];
209204

210205
if ( !source ) {
211-
this.outro = this.outro.replace( rx, '' );
212206
break;
213207
}
214208

215-
source.content.trimStart();
209+
source.content.trimStart( charType );
216210
i += 1;
217-
} while ( source.content.str === '' );
211+
} while ( source.content.toString() === '' ); // TODO faster way to determine non-empty source?
218212
}
219213

220214
return this;
221215
},
222216

223217
trimEnd ( charType ) {
224218
const rx = new RegExp( ( charType || '\\s' ) + '+$' );
225-
this.outro = this.outro.replace( rx, '' );
226219

227-
if ( !this.outro ) {
228-
let source;
229-
let i = this.sources.length - 1;
220+
let source;
221+
let i = this.sources.length - 1;
230222

231-
do {
232-
source = this.sources[i];
223+
do {
224+
source = this.sources[i];
233225

234-
if ( !source ) {
235-
this.intro = this.intro.replace( rx, '' );
236-
break;
237-
}
226+
if ( !source ) {
227+
this.intro = this.intro.replace( rx, '' );
228+
break;
229+
}
238230

239-
source.content.trimEnd(charType);
240-
i -= 1;
241-
} while ( source.content.str === '' );
242-
}
231+
source.content.trimEnd( charType );
232+
i -= 1;
233+
} while ( source.content.toString() === '' ); // TODO faster way to determine non-empty source?
243234

244235
return this;
245236
}

0 commit comments

Comments
 (0)