-
Notifications
You must be signed in to change notification settings - Fork 21
/
no-invalid-names.js
142 lines (131 loc) · 5.88 KB
/
no-invalid-names.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @fileoverview Disallow invalid and missing test names.
* @author Kevin Partington
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const utils = require("../utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow invalid and missing test names",
category: "Best Practices",
url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/master/docs/rules/no-invalid-names.md"
},
fixable: "code",
messages: {
moduleNameEmpty: "Module name is empty.",
moduleNameInvalidType: "Module name \"{{ name }}\" is invalid type: {{ type }}.",
moduleNameMissing: "Module name is missing.",
moduleNameOuterQUnitDelimiters: "Module name \"{{ name }}\" has leading and/or trailing QUnit delimiter: (> or :).",
moduleNameOuterSpaces: "Module name has leading and/or trailing spaces.",
testNameEmpty: "Test name is empty.",
testNameInvalidType: "Test name \"{{ name }}\" is invalid type: {{ type }}.",
testNameMissing: "Test name is missing.",
testNameOuterQUnitDelimiters: "Test name \"{{ name }}\" has leading and/or trailing QUnit delimiter (> or :).",
testNameOuterSpaces: "Test name has leading and/or trailing spaces."
},
schema: []
},
create: function (context) {
const sourceCode = context.getSourceCode();
const FUNCTION_TYPES = new Set(["FunctionExpression", "ArrowFunctionExpression"]);
const INVALID_NAME_AST_TYPES = new Set([
"ArrayExpression",
"ObjectExpression",
"ThisExpression",
"UnaryExpression",
"UpdateExpression",
"BinaryExpression",
"AssignmentExpression",
"LogicalExpression"
]);
const QUNIT_NAME_DELIMITERS = [">", ":"];
/**
* Check name for starting or ending with QUnit delimiters.
* @param {string} name The test or module name to check.
* @returns {boolean} True if the name starts or ends with a QUnit name delimiter, false otherwise.
*/
function nameHasOuterQUnitDelimiters(name) {
return QUNIT_NAME_DELIMITERS.some(delimiter =>
name.startsWith(delimiter) || name.endsWith(delimiter)
);
}
/**
* Check the name argument of a module or test CallExpression.
* @param {ASTNode} firstArg The first argument of the test/module call.
* @param {"test"|"module"} objectType Whether this is a test or module call.
* @param {ASTNode} calleeForMissingName The callee, used as report location if the test/module name is missing.
* @returns {void}
*/
function checkNameArgument(firstArg, objectType, calleeForMissingName) {
if (!firstArg || FUNCTION_TYPES.has(firstArg.type)) {
context.report({
node: calleeForMissingName,
messageId: `${objectType}NameMissing`
});
} else if (INVALID_NAME_AST_TYPES.has(firstArg.type)) {
context.report({
node: firstArg,
messageId: `${objectType}NameInvalidType`,
data: {
type: firstArg.type,
name: sourceCode.getText(firstArg)
}
});
} else if (firstArg.type === "Literal") {
if (typeof firstArg.value !== "string") {
context.report({
node: firstArg,
messageId: `${objectType}NameInvalidType`,
data: {
type: typeof firstArg.value,
name: sourceCode.getText(firstArg)
}
});
} else if (firstArg.value.trim().length === 0) {
context.report({
node: firstArg,
messageId: `${objectType}NameEmpty`
});
} else if (firstArg.value.trim() !== firstArg.value) {
const trimmedValue = firstArg.value.trim();
const raw = firstArg.raw;
const startDelimiter = raw[0];
const endDelimiter = raw[raw.length - 1];
context.report({
node: firstArg,
messageId: `${objectType}NameOuterSpaces`,
fix: fixer => fixer.replaceText(
firstArg,
`${startDelimiter}${trimmedValue}${endDelimiter}`
)
});
} else if (nameHasOuterQUnitDelimiters(firstArg.value)) {
context.report({
node: firstArg,
messageId: `${objectType}NameOuterQUnitDelimiters`,
data: { name: firstArg.value }
});
}
}
}
return {
"CallExpression": function (node) {
/* istanbul ignore else: Correctly does nothing */
if (utils.isTest(node.callee)) {
checkNameArgument(node.arguments[0], "test", node.callee);
} else if (utils.isModule(node.callee)) {
checkNameArgument(node.arguments[0], "module", node.callee);
}
}
};
}
};