Shallowly remove location in preferTemplate
MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -243,7 +243,7 @@ export const descriptions = createMessages(
PREFER_TEMPLATE: {
category: 'lint/preferTemplate',
- message: "You're using string concatenation when template literals are preferred",
+ message: 'Template literals are preferred over string concatenation',
},
UNSAFE_NEGATION: {

MODIFY
packages/@romejs/js-ast-utils/index.ts
packages/@romejs/js-ast-utils/index.ts
@@ -27,6 +27,7 @@ export {default as renameBindings} from './renameBindings';
export {default as template} from './template';
export {default as isInTypeAnnotation} from './isInTypeAnnotation';
export {default as removeLoc} from './removeLoc';
+export {default as removeShallowLoc} from './removeShallowLoc';
export {default as inheritLoc} from './inheritLoc';
export {default as getRequireSource} from './getRequireSource';
export {default as isIdentifierish} from './isIdentifierish';

ADD
packages/@romejs/js-ast-utils/removeShallowLoc.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {AnyNode} from '@romejs/js-ast';
+
+export default function removeShallowLoc<T extends AnyNode>(node: T): T {
+ return {
+ ...node,
+ loc: undefined,
+ };
+}

MODIFY
packages/@romejs/js-compiler/transforms/lint/preferTemplate.ts
packages/@romejs/js-compiler/transforms/lint/preferTemplate.ts
@@ -13,6 +13,7 @@ import {
} from '@romejs/js-ast';
import {descriptions} from '@romejs/diagnostics';
import {TransformExitResult} from '@romejs/js-compiler/types';
+import {removeShallowLoc} from '@romejs/js-ast-utils';
export default {
name: 'preferTemplate',
@@ -37,7 +38,7 @@ export default {
cooked: node.right.value,
}),
];
- const expressions = [node.left];
+ const expressions = [removeShallowLoc(node.left)];
autofix = templateLiteral.create({
expressions,
quasis,
@@ -56,7 +57,10 @@ export default {
cooked: '',
}),
];
- const expressions = [node.right];
+
+ // We need to remove the location or else if we were to show a preview the source map would resolve to the end of
+ // this node
+ const expressions = [removeShallowLoc(node.right)];
autofix = templateLiteral.create({
expressions,
quasis,

Shallowly remove location in preferTemplate