From 40aa0c5519eb0c2f90b8575038cd5ab11b9e8719 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 13 May 2018 20:18:07 +0200 Subject: [PATCH 1/2] support .lastLine() --- src/MagicString.js | 40 ++++++++++++++++++++++++++++++++++++++++ test/MagicString.js | 23 +++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/MagicString.js b/src/MagicString.js index e73353a..47c1503 100644 --- a/src/MagicString.js +++ b/src/MagicString.js @@ -7,6 +7,8 @@ import getLocator from './utils/getLocator.js'; import Mappings from './utils/Mappings.js'; import Stats from './utils/Stats.js'; +const n = '\n'; + const warned = { insertLeft: false, insertRight: false, @@ -460,6 +462,8 @@ export default class MagicString { } lastChar() { + if (this.outro.length) + return this.outro[this.outro.length - 1]; let chunk = this.lastChunk; do { if (chunk.outro.length) @@ -469,9 +473,45 @@ export default class MagicString { if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1]; } while (chunk = chunk.previous); + if (this.intro.length) + return this.intro[this.intro.length - 1]; return ''; } + lastLine() { + let lineIndex = this.outro.lastIndexOf(n); + if (lineIndex !== -1) + return this.outro.substr(lineIndex + 1); + let lineStr = this.outro; + let chunk = this.lastChunk; + do { + if (chunk.outro.length > 0) { + lineIndex = chunk.outro.lastIndexOf(n); + if (lineIndex !== -1) + return chunk.outro.substr(lineIndex + 1) + lineStr; + lineStr = chunk.outro + lineStr; + } + + if (chunk.content.length > 0) { + lineIndex = chunk.content.lastIndexOf(n); + if (lineIndex !== -1) + return chunk.content.substr(lineIndex + 1) + lineStr; + lineStr = chunk.content + lineStr; + } + + if (chunk.intro.length > 0) { + lineIndex = chunk.intro.lastIndexOf(n); + if (lineIndex !== -1) + return chunk.intro.substr(lineIndex + 1) + lineStr; + lineStr = chunk.intro + lineStr; + } + } while (chunk = chunk.previous); + lineIndex = this.intro.lastIndexOf(n); + if (lineIndex !== -1) + return this.intro.substr(lineIndex + 1) + lineStr; + return this.intro + lineStr; + } + slice(start = 0, end = this.original.length) { while (start < 0) start += this.original.length; while (end < 0) end += this.original.length; diff --git a/test/MagicString.js b/test/MagicString.js index 5726f8e..80421b7 100644 --- a/test/MagicString.js +++ b/test/MagicString.js @@ -1191,4 +1191,27 @@ describe( 'MagicString', () => { assert.equal( s.isEmpty(), true ); }); }); + + describe( 'lastLine', () => { + it( 'should support lastLine', () => { + const s = new MagicString( ' abcde\nfghijkl ' ); + + assert.equal( s.lastLine(), 'fghijkl ' ); + + s.prepend( ' ' ); + s.append( ' ' ); + s.remove( 1, 6 ); + s.remove( 9, 15 ); + + assert.equal( s.lastLine(), 'fg ' ); + + s.overwrite( 7, 8, '\n' ); + + assert.equal( s.lastLine(), 'g ' ); + + s.append('\n//lastline'); + + assert.equal( s.lastLine(), '//lastline' ); + }); + }); }); From 477c93a1cd599ce5bd3f3e5888cc9b399156da89 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 13 May 2018 20:21:51 +0200 Subject: [PATCH 2/2] update typing --- index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.d.ts b/index.d.ts index 5d9e1e1..f725754 100644 --- a/index.d.ts +++ b/index.d.ts @@ -93,6 +93,8 @@ export default class MagicString { snip(start: number, end: number): MagicString; trim(): MagicString; + lastChar(): string; + lastLine(): string; isEmpty(): boolean; original: string;