refactor a bit to better comply with projects standards
MODIFY
packages/@romejs/js-compiler/__rtests__/__rsnapshots__/lint.ts.rsnap
packages/@romejs/js-compiler/__rtests__/__rsnapshots__/lint.ts.rsnap
"unsafe negation": "Object {\n filters: Array []\n src: '!1 in [1,2]'\n diagnostics: Array [\n Object {\n category: 'lint/unsafeNegation'\n filename: 'unknown'\n language: 'js'\n message: 'Unsafe usage of negation operator in left side of binary expression'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n}",
"no cond assign": "Object {\n filters: Array []\n src: 'for (let i = 1; i = 10; i++) {\\n console.log(\\'foo\\')\\n }'\n diagnostics: Array [\n Object {\n category: 'lint/noCondAssign'\n filename: 'unknown'\n language: 'js'\n message: 'Cannot assign variable in loop condition'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 5\n index: 61\n line: 3\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n}",
"disallow var": "Object {\n filters: Array []\n src: 'var foobar;\\nfoobar'\n diagnostics: Array [\n Object {\n category: 'lint/disallowVar'\n filename: 'unknown'\n language: 'js'\n message: 'Variable declarations using `var` are disallowed, use `let` or `const` instead.'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 11\n index: 11\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n}",
+ "prefer template": "Object {\n filters: Array []\n src: 'const foo = \\'bar\\'; console.log(foo + \\'baz\\')'\n diagnostics: Array [\n Object {\n category: 'lint/preferTemplate'\n filename: 'unknown'\n language: 'js'\n message: 'You\\'re using string concatenation when template literals are preferred'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 42\n index: 42\n line: 1\n }\n start: Object {\n column: 31\n index: 31\n line: 1\n }\n }\n Object {\n category: 'lint/undeclaredVariables'\n filename: 'unknown'\n language: 'js'\n message: 'Undeclared variable <emphasis>foo</emphasis>'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 34\n index: 34\n line: 1\n }\n start: Object {\n column: 31\n index: 31\n line: 1\n }\n }\n Object {\n category: 'lint/unusedVariables'\n filename: 'unknown'\n language: 'js'\n message: 'Unused variable <emphasis>foo</emphasis>'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 9\n index: 9\n line: 1\n }\n start: Object {\n column: 6\n index: 6\n line: 1\n }\n }\n ]\n}",
"no cond assign: 1": "Object {\n filters: Array []\n src: 'if(foo = \\'bar\\') {\\n console.log(\\'foo\\')\\n }'\n diagnostics: Array [\n Object {\n category: 'lint/noCondAssign'\n filename: 'unknown'\n language: 'js'\n message: 'Cannot assign variable in loop condition'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 5\n index: 48\n line: 3\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n}",
+ "prefer template: 1": "Object {\n filters: Array []\n src: 'console.log((1 * 2) + \\'baz\\')'\n diagnostics: Array [\n Object {\n category: 'lint/preferTemplate'\n filename: 'unknown'\n language: 'js'\n message: 'You\\'re using string concatenation when template literals are preferred'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 27\n index: 27\n line: 1\n }\n start: Object {\n column: 12\n index: 12\n line: 1\n }\n }\n ]\n}",
"no debugger": "Array [\n Object {\n category: 'lint/noDebugger'\n filename: 'unknown'\n fixable: true\n language: 'js'\n message: 'Unexpected \\'debugger\\' statement'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 9\n index: 9\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n]",
"no cond assign: 2": "Object {\n filters: Array []\n src: 'while (foo = \\'bar\\' {\\n console.log(\\'foo\\')\\n }'\n diagnostics: Array [\n Object {\n category: 'parse/js'\n filename: 'unknown'\n message: 'Unclosed while test'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'js-parser'}]\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 6\n index: 6\n line: 1\n }\n advice: Array [\n log {\n category: 'info'\n message: 'We expected to find the closing character <emphasis>)</emphasis> here'\n }\n frame {\n filename: 'unknown'\n end: Object {\n column: 19\n index: 19\n line: 1\n }\n start: Object {\n column: 19\n index: 19\n line: 1\n }\n }\n ]\n }\n Object {\n category: 'lint/noCondAssign'\n filename: 'unknown'\n language: 'js'\n message: 'Cannot assign variable in loop condition'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 5\n index: 51\n line: 3\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n}",
"no async promise executor": "Object {\n filters: Array []\n src: 'new Promise(async function foo() {})'\n diagnostics: Array [\n Object {\n category: 'lint/noAsyncPromiseExecutor'\n filename: 'unknown'\n language: 'js'\n message: 'Promise executor functions should not be async.'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 35\n index: 35\n line: 1\n }\n start: Object {\n column: 12\n index: 12\n line: 1\n }\n }\n ]\n}",
"no cond assign: 3": "Object {\n filters: Array []\n src: 'do {\\n console.log(\\'foo)\\n } while (foo = \\'bar\\')'\n diagnostics: Array [\n Object {\n advice: undefined\n category: 'parse/js'\n filename: 'unknown'\n message: 'Unterminated string constant'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'js-parser'}]\n end: Object {\n column: 0\n index: 28\n line: 3\n }\n start: Object {\n column: 0\n index: 28\n line: 3\n }\n }\n Object {\n category: 'lint/undeclaredVariables'\n filename: 'unknown'\n language: 'js'\n message: 'Undeclared variable <emphasis>INVALID_PLACEHOLDER</emphasis>'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 50\n index: 55\n line: 2\n }\n start: Object {\n column: 50\n index: 55\n line: 2\n }\n }\n Object {\n category: 'lint/undeclaredVariables'\n filename: 'unknown'\n language: 'js'\n message: 'Undeclared variable <emphasis>bar</emphasis>'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 47\n index: 52\n line: 2\n }\n start: Object {\n column: 44\n index: 49\n line: 2\n }\n }\n ]\n}",
"no async promise executor: 1": "Object {\n filters: Array []\n src: 'new Promise(async () => {})'\n diagnostics: Array [\n Object {\n category: 'lint/noAsyncPromiseExecutor'\n filename: 'unknown'\n language: 'js'\n message: 'Promise executor functions should not be async.'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 26\n index: 26\n line: 1\n }\n start: Object {\n column: 12\n index: 12\n line: 1\n }\n }\n ]\n}",
- "no async promise executor: 2": "Object {\n filters: Array []\n src: 'new Promise(((((async () => {})))))'\n diagnostics: Array [\n Object {\n category: 'lint/noAsyncPromiseExecutor'\n filename: 'unknown'\n language: 'js'\n message: 'Promise executor functions should not be async.'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 30\n index: 30\n line: 1\n }\n start: Object {\n column: 16\n index: 16\n line: 1\n }\n }\n ]\n}",
- "prefer template": "Object {\n filters: Array []\n src: 'const foo = \\'bar\\'; console.log(foo + \\'baz\\')'\n diagnostics: Array [\n Object {\n category: 'lint/preferTemplate'\n filename: 'unknown'\n language: 'js'\n message: 'You\\'re using string concatenation when template literals are preferred'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 42\n index: 42\n line: 1\n }\n start: Object {\n column: 31\n index: 31\n line: 1\n }\n }\n ]\n}",
- "prefer template: 1": "Object {\n filters: Array []\n src: 'console.log((1 * 2) + \\'baz\\')'\n diagnostics: Array [\n Object {\n category: 'lint/preferTemplate'\n filename: 'unknown'\n language: 'js'\n message: 'You\\'re using string concatenation when template literals are preferred'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 27\n index: 27\n line: 1\n }\n start: Object {\n column: 12\n index: 12\n line: 1\n }\n }\n ]\n}"
+ "no async promise executor: 2": "Object {\n filters: Array []\n src: 'new Promise(((((async () => {})))))'\n diagnostics: Array [\n Object {\n category: 'lint/noAsyncPromiseExecutor'\n filename: 'unknown'\n language: 'js'\n message: 'Promise executor functions should not be async.'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 30\n index: 30\n line: 1\n }\n start: Object {\n column: 16\n index: 16\n line: 1\n }\n }\n ]\n}"
}
\ No newline at end of file

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, templateElement, TemplateElement, TemplateLiteral, templateLiteral} from '@romejs/js-ast';
+import {AnyNode, BinaryExpression, templateElement, TemplateElement, templateLiteral} from '@romejs/js-ast';
+
-const nodeToQuasi = (fromConcat: string, isTail: boolean): TemplateElement => (
- fromConcat === 'StringLiteral' ?
- templateElement.create({
- cooked: fromConcat,
- raw: fromConcat,
- tail: isTail
- }) :
- templateElement.create({
- cooked: '',
- raw: '',
- tail: isTail
- })
-)
export default {
name: 'preferTemplate',
@@ -30,8 +18,8 @@ export default {
if (
node.type === 'BinaryExpression' &&
node.operator === '+' &&
- (node.left.type === 'StringLiteral' ||
- node.right.type === 'StringLiteral')
+ ((node.left.type === 'StringLiteral' && !node.left.value.includes('`')) ||
+ (node.right.type === 'StringLiteral' && !node.right.value.includes('`')))
) {
path.context.addNodeDiagnostic(node, {
category: 'lint/preferTemplate',
@@ -41,11 +29,26 @@ export default {
return templateLiteral.create({
expressions: [node.left, node.right].filter(node => node.type !== 'StringLiteral'),
- quasis: [{value: node.left, side: 'left'}, {value: node.right, side: 'right'}].map(quasi =>
- nodeToQuasi(quasi.value.type, quasi.side === 'right'))
+ quasis: nodeToQuasis(node)
})
}
return node;
},
+
};
+
+function nodeToQuasis (node: BinaryExpression): TemplateElement[] {
+ return [
+ templateElement.create({
+ cooked: node.left.type === 'StringLiteral' ? node.left.value : '',
+ raw: node.left.type === 'StringLiteral' ? node.left.value : '',
+ tail: false
+ }),
+ templateElement.create({
+ cooked: node.right.type === 'StringLiteral' ? node.right.value : '',
+ raw: node.right.type === 'StringLiteral' ? node.right.value : '',
+ tail: true
+ })
+ ];
+}

refactor a bit to better comply with projects standards