/**
* @author Yosuke Ota
*/
'use strict'
const RuleTester = require('../../eslint-compat').RuleTester
const rule = require('../../../lib/rules/no-irregular-whitespace')
const tester = new RuleTester({
languageOptions: { parser: require('vue-eslint-parser'), ecmaVersion: 2018 }
})
const IRREGULAR_WHITESPACES = [
'\f',
'\v',
'\u0085',
'\uFEFF',
'\u00A0',
'\u1680',
'\u180E',
'\u2000',
'\u2001',
'\u2002',
'\u2003',
'\u2004',
'\u2005',
'\u2006',
'\u2007',
'\u2008',
'\u2009',
'\u200A',
'\u200B',
'\u202F',
'\u205F',
'\u3000'
]
const IRREGULAR_LINE_TERMINATORS = ['\u2028', '\u2029']
const ALL_IRREGULAR_WHITESPACES = [
...IRREGULAR_WHITESPACES,
...IRREGULAR_LINE_TERMINATORS
]
const ALL_IRREGULAR_WHITESPACE_CODES = ALL_IRREGULAR_WHITESPACES.map((s) =>
`000${s.codePointAt(0).toString(16)}`.slice(-4)
)
tester.run('no-irregular-whitespace', rule, {
valid: [
'var a = \t\r\n b',
' \t\r\n s \t\r\n
',
// escapes
...ALL_IRREGULAR_WHITESPACE_CODES.map((s) => `/\\u${s}/+'\\u${s}'`),
// html escapes
...ALL_IRREGULAR_WHITESPACE_CODES.map(
(s) => `${s}s${s}
`
),
// strings
...IRREGULAR_WHITESPACES.map((s) => `'${s}'`),
...IRREGULAR_LINE_TERMINATORS.map((s) => `'\\${s}'`), // multiline string
...IRREGULAR_WHITESPACES.map((s) => `{{ '${s}' }}`),
// comments
...IRREGULAR_WHITESPACES.map((s) => ({
code: `//${s}`,
options: [{ skipComments: true }]
})),
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `/*${s}*/`,
options: [{ skipComments: true }]
})),
...IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ i//${s}\n }}
`,
options: [{ skipComments: true }]
})),
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ i/*${s}*/ }}
`,
options: [{ skipComments: true }]
})),
// regexps
...IRREGULAR_WHITESPACES.map((s) => ({
code: `/${s}/`,
options: [{ skipRegExps: true }]
})),
...IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ /${s}/ }}
`,
options: [{ skipRegExps: true }]
})),
// templates
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `\`${s}\``,
options: [{ skipTemplates: true }]
})),
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ \`${s}\` }}
`,
options: [{ skipTemplates: true }]
})),
// attribute values
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: ``,
options: [{ skipHTMLAttributeValues: true }]
})),
// text contents
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `${s}
`,
options: [{ skipHTMLTextContents: true }]
})),
// outside
`\u3000\u3000\u3000\u3000\u3000\u3000`
],
invalid: [
{
code: `var any \u000B = 'thing';`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 9
}
]
},
{
code: `
\u3000
\u3000
\u3000
`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 3,
column: 9
},
{
message: 'Irregular whitespace not allowed.',
line: 5,
column: 11
},
{
message: 'Irregular whitespace not allowed.',
line: 6,
column: 17
},
{
message: 'Irregular whitespace not allowed.',
line: 7,
column: 17
},
{
message: 'Irregular whitespace not allowed.',
line: 7,
column: 23
},
{
message: 'Irregular whitespace not allowed.',
line: 8,
column: 11
},
{
message: 'Irregular whitespace not allowed.',
line: 9,
column: 9
},
{
message: 'Irregular whitespace not allowed.',
line: 11,
column: 9
},
{
message: 'Irregular whitespace not allowed.',
line: 12,
column: 9
},
{
message: 'Irregular whitespace not allowed.',
line: 15,
column: 15
}
]
},
// strings
...IRREGULAR_WHITESPACES.map((s) => ({
code: `'${s}'`,
options: [{ skipStrings: false }],
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 2
}
]
})),
...IRREGULAR_LINE_TERMINATORS.map((s) => ({
code: `'\\${s}'`,
options: [{ skipStrings: false }],
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 3
}
]
})),
...IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ '${s}' }}`,
options: [{ skipStrings: false }],
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 15
}
]
})),
// comments
...IRREGULAR_WHITESPACES.map((s) => ({
code: `//${s}`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 3
}
]
})),
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `/*${s}*/`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 3
}
]
})),
...IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ i//${s}\n }}
`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 22
}
]
})),
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ i/*${s}*/ }}
`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 22
}
]
})),
// regexps
...IRREGULAR_WHITESPACES.map((s) => ({
code: `/${s}/`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 2
}
]
})),
...IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ /${s}/ }}
`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 20
}
]
})),
// templates
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `\`${s}\``,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 2
}
]
})),
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `{{ \`${s}\` }}
`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 20
}
]
})),
// attribute values
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: ``,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 22
}
]
})),
// text contents
...ALL_IRREGULAR_WHITESPACES.map((s) => ({
code: `${s}
`,
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 1,
column: 16
}
]
})),
// options
{
code: `
`,
options: [
{
skipComments: true,
skipStrings: true,
skipTemplates: true,
skipRegExps: true,
skipHTMLAttributeValues: true,
skipHTMLTextContents: true
}
],
errors: [
{
message: 'Irregular whitespace not allowed.',
line: 6,
column: 17
}
]
}
]
})