split out preferTemplate tests
ADD
packages/@romejs/js-compiler/transforms/lint/__rtests__/__rsnapshots__/preferTemplate.snap.md
+# `preferTemplate.ts`
+
+**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-compiler/transforms/lint/__rtests__/preferTemplate.ts --update-snapshots` to update.
+
+## `prefer template`
+
+### `0`
+
+```javascript
+Object {
+ src: 'const foo = \'bar\'; console.log(foo + \'baz\')'
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ category: 'lint/preferTemplate'
+ filename: 'unknown'
+ language: 'js'
+ message: 'You\'re using string concatenation when template literals are preferred'
+ mtime: undefined
+ sourceType: 'module'
+ origins: Array [Object {category: 'lint'}]
+ end: Object {
+ column: 42
+ index: 42
+ line: 1
+ }
+ start: Object {
+ column: 31
+ index: 31
+ line: 1
+ }
+ }
+ Object {
+ category: 'lint/undeclaredVariables'
+ filename: 'unknown'
+ language: 'js'
+ message: 'Undeclared variable <emphasis>foo</emphasis>'
+ mtime: undefined
+ sourceType: 'module'
+ origins: Array [Object {category: 'lint'}]
+ end: Object {
+ column: 34
+ index: 34
+ line: 1
+ }
+ start: Object {
+ column: 31
+ index: 31
+ line: 1
+ }
+ }
+ Object {
+ category: 'lint/unusedVariables'
+ filename: 'unknown'
+ language: 'js'
+ message: 'Unused variable <emphasis>foo</emphasis>'
+ mtime: undefined
+ sourceType: 'module'
+ origins: Array [Object {category: 'lint'}]
+ end: Object {
+ column: 9
+ index: 9
+ line: 1
+ }
+ start: Object {
+ column: 6
+ index: 6
+ line: 1
+ }
+ }
+ ]
+}
+```
+
+### `1`
+
+```javascript
+Object {
+ src: 'console.log((1 * 2) + \'baz\')'
+ suppressions: Array []
+ diagnostics: Array [
+ Object {
+ category: 'lint/preferTemplate'
+ filename: 'unknown'
+ language: 'js'
+ message: 'You\'re using string concatenation when template literals are preferred'
+ mtime: undefined
+ sourceType: 'module'
+ origins: Array [Object {category: 'lint'}]
+ end: Object {
+ column: 27
+ index: 27
+ line: 1
+ }
+ start: Object {
+ column: 12
+ index: 12
+ line: 1
+ }
+ }
+ ]
+}
+```

ADD
packages/@romejs/js-compiler/transforms/lint/__rtests__/preferTemplate.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 test from '@romejs/test';
+import {
+ testLint,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+} from '../../../__rtests__/lint';
+
+test('prefer template', async t => {
+ const template1 = await testLint(
+ `const foo = 'bar'; console.log(foo + 'baz')`,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ );
+
+ t.snapshot(template1);
+
+ const template2 = await testLint(
+ `console.log((1 * 2) + 'baz')`,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ );
+
+ t.snapshot(template2);
+});

MODIFY
packages/@romejs/js-compiler/transforms/lint/preferTemplate.ts
packages/@romejs/js-compiler/transforms/lint/preferTemplate.ts
*/
import {Path} from '@romejs/js-compiler';
-import {AnyNode, BinaryExpression, templateElement, TemplateElement, templateLiteral} from '@romejs/js-ast';
-
-
+import {
+ AnyNode,
+ BinaryExpression,
+ templateElement,
+ TemplateElement,
+ templateLiteral,
+} from '@romejs/js-ast';
export default {
name: 'preferTemplate',
@@ -19,7 +23,8 @@ export default {
node.type === 'BinaryExpression' &&
node.operator === '+' &&
((node.left.type === 'StringLiteral' && !node.left.value.includes('`')) ||
- (node.right.type === 'StringLiteral' && !node.right.value.includes('`')))
+ (node.right.type === 'StringLiteral' &&
+ !node.right.value.includes('`')))
) {
path.context.addNodeDiagnostic(node, {
category: 'lint/preferTemplate',
@@ -28,14 +33,15 @@ export default {
});
return templateLiteral.create({
- expressions: [node.left, node.right].filter(node => node.type !== 'StringLiteral'),
- quasis: nodeToQuasis(node)
- })
+ expressions: [node.left, node.right].filter(
+ node => node.type !== 'StringLiteral',
+ ),
+ quasis: nodeToQuasis(node),
+ });
}
return node;
},
-
};
function nodeToQuasis(node: BinaryExpression): TemplateElement[] {
@@ -43,12 +49,12 @@ function nodeToQuasis (node: BinaryExpression): TemplateElement[] {
templateElement.create({
cooked: node.left.type === 'StringLiteral' ? node.left.value : '',
raw: node.left.type === 'StringLiteral' ? node.left.value : '',
- tail: false
+ tail: false,
}),
templateElement.create({
cooked: node.right.type === 'StringLiteral' ? node.right.value : '',
raw: node.right.type === 'StringLiteral' ? node.right.value : '',
- tail: true
- })
+ tail: true,
+ }),
];
}

split out preferTemplate tests