From 969cb08c87eedc7d23155bdb9701d837837ff346 Mon Sep 17 00:00:00 2001 From: Alan Pierce Date: Sun, 27 Nov 2016 09:39:48 -0800 Subject: [PATCH] fix: silence magic-string warnings and update usage (#571) magic-string 0.17.0 deprecated insertLeft and insertRight in favor of more precise prepend/append names. See this issue for a discussion on that: https://github.com/Rich-Harris/magic-string/issues/109 To silence that warning, this commit moves the code to the new API, which was pretty easy because there are only a few call sites. I also cleaned up that part of `NodePatcher`, which didn't make a lot of sense. Now, instead of `NodePatcher.insert` calling `NodePatcher.insertRight` calling `MagicString.insertLeft` and `NodePatcher.insertLeft` being unused, we just have `NodePatcher.insert` calling `MagicString.appendLeft`. At least for now, I think it's best to always use the same insert method so that inserts go in order. I also changed insertRight to appendRight in one case because it probably makes more sense anyway and the tests pass. --- src/patchers/NodePatcher.js | 32 ++----------------- src/patchers/types.js | 4 +-- .../normalize/patchers/ConditionalPatcher.js | 2 +- src/stages/semicolons/index.js | 2 +- src/utils/escape.js | 2 +- 5 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/patchers/NodePatcher.js b/src/patchers/NodePatcher.js index 4b0bfe45c..7a452aa20 100644 --- a/src/patchers/NodePatcher.js +++ b/src/patchers/NodePatcher.js @@ -307,28 +307,7 @@ export default class NodePatcher { /** * Insert content at the specified index. */ - insertLeft(index: number, content: string) { - if (typeof index !== 'number') { - throw new Error( - `cannot insert ${JSON.stringify(content)} at non-numeric index ${index}` - ); - } - this.log( - 'INSERT LEFT', - index, - JSON.stringify(content), - 'AFTER', - JSON.stringify(this.context.source.slice(index - 8, index)) - ); - - this.adjustBoundsToInclude(index); - this.editor.insertLeft(index, content); - } - - /** - * Insert content at the specified index. - */ - insertRight(index: number, content: string) { + insert(index: number, content: string) { if (typeof index !== 'number') { throw new Error( `cannot insert ${JSON.stringify(content)} at non-numeric index ${index}` @@ -343,14 +322,7 @@ export default class NodePatcher { ); this.adjustBoundsToInclude(index); - this.editor.insertLeft(index, content); - } - - /** - * Alias for `#insertRight`. - */ - insert(index: number, content: string) { - this.insertRight(index, content); + this.editor.appendLeft(index, content); } allowPatchingOuterBounds(): boolean { diff --git a/src/patchers/types.js b/src/patchers/types.js index 1ef639aac..84bb93e10 100644 --- a/src/patchers/types.js +++ b/src/patchers/types.js @@ -16,8 +16,8 @@ export type ParseContext = { }; export type Editor = { - insertLeft: (index: number, content: string) => Editor; - insertRight: (index: number, content: string) => Editor; + appendLeft: (index: number, content: string) => Editor; + appendRight: (index: number, content: string) => Editor; overwrite: (start: number, end: number, content: string) => Editor; remove: (start: number, end: number) => Editor; slice: (start: number, end: number) => string; diff --git a/src/stages/normalize/patchers/ConditionalPatcher.js b/src/stages/normalize/patchers/ConditionalPatcher.js index 22d4ed050..38519fc97 100644 --- a/src/stages/normalize/patchers/ConditionalPatcher.js +++ b/src/stages/normalize/patchers/ConditionalPatcher.js @@ -52,7 +52,7 @@ export default class ConditionalPatcher extends NodePatcher { if (ifToken) { let consequentCode = this.slice(this.consequent.outerStart, this.consequent.outerEnd); this.remove(this.consequent.outerStart, ifToken.start); - this.insertRight(this.condition.outerEnd, ` then ${consequentCode}`); + this.insert(this.condition.outerEnd, ` then ${consequentCode}`); } } diff --git a/src/stages/semicolons/index.js b/src/stages/semicolons/index.js index 72b048d08..e6e510b17 100644 --- a/src/stages/semicolons/index.js +++ b/src/stages/semicolons/index.js @@ -33,7 +33,7 @@ export default class SemicolonsStage { asi(config); - config.insertions.forEach(({ index, content }) => editor.insertLeft(index, content)); + config.insertions.forEach(({ index, content }) => editor.appendLeft(index, content)); config.removals.forEach(({ start, end }) => editor.remove(start, end)); return { diff --git a/src/utils/escape.js b/src/utils/escape.js index bb65aed04..c56f8119d 100644 --- a/src/utils/escape.js +++ b/src/utils/escape.js @@ -14,7 +14,7 @@ export default function escape(patcher: MagicString, characters: Array|( if (source[i] === '\\') { i++; } else if (predicate(source[i], i, source)) { - patcher.insertRight(i, '\\'); + patcher.appendRight(i, '\\'); } } }