mirror of
https://github.com/hanzoai/eslint.git
synced 2026-10-30 19:38:10 +00:00
154 lines
2.8 KiB
JavaScript
154 lines
2.8 KiB
JavaScript
/**
|
|
* @fileoverview disallow unnecessary concatenation of literals or template literals
|
|
* @author Henry Zhu
|
|
*/
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Requirements
|
|
//------------------------------------------------------------------------------
|
|
|
|
const rule = require("../../../lib/rules/no-useless-concat"),
|
|
RuleTester = require("../../../lib/rule-tester/rule-tester");
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Tests
|
|
//------------------------------------------------------------------------------
|
|
|
|
const ruleTester = new RuleTester();
|
|
|
|
ruleTester.run("no-useless-concat", rule, {
|
|
valid: [
|
|
"var a = 1 + 1;",
|
|
"var a = 1 * '2';",
|
|
"var a = 1 - 2;",
|
|
"var a = foo + bar;",
|
|
"var a = 'foo' + bar;",
|
|
"var foo = 'foo' +\n 'bar';",
|
|
|
|
// https://github.com/eslint/eslint/issues/3575
|
|
"var string = (number + 1) + 'px';",
|
|
"'a' + 1",
|
|
"1 + '1'",
|
|
{ code: "1 + `1`", languageOptions: { ecmaVersion: 6 } },
|
|
{ code: "`1` + 1", languageOptions: { ecmaVersion: 6 } },
|
|
{ code: "(1 + +2) + `b`", languageOptions: { ecmaVersion: 6 } },
|
|
],
|
|
|
|
invalid: [
|
|
{
|
|
code: "'a' + 'b'",
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 5,
|
|
endLine: 1,
|
|
endColumn: 6,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "'a' +\n'b' + 'c'",
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 2,
|
|
column: 5,
|
|
endLine: 2,
|
|
endColumn: 6,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "foo + 'a' + 'b'",
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 11,
|
|
endLine: 1,
|
|
endColumn: 12,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "'a' + 'b' + 'c'",
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 5,
|
|
endLine: 1,
|
|
endColumn: 6,
|
|
},
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 11,
|
|
endLine: 1,
|
|
endColumn: 12,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "(foo + 'a') + ('b' + 'c')",
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 13,
|
|
endLine: 1,
|
|
endColumn: 14,
|
|
},
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 20,
|
|
endLine: 1,
|
|
endColumn: 21,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "`a` + 'b'",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 5,
|
|
endLine: 1,
|
|
endColumn: 6,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "`a` + `b`",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 5,
|
|
endLine: 1,
|
|
endColumn: 6,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "foo + `a` + `b`",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpectedConcat",
|
|
line: 1,
|
|
column: 11,
|
|
endLine: 1,
|
|
endColumn: 12,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|