-
Notifications
You must be signed in to change notification settings - Fork 72
/
no-duplicate-string.ts
116 lines (103 loc) · 3.66 KB
/
no-duplicate-string.ts
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
/*
* eslint-plugin-sonarjs
* Copyright (C) 2018-2021 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// https://jira.sonarsource.com/browse/RSPEC-1192
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { Rule } from '../utils/types';
import docsUrl from '../utils/docs-url';
// Number of times a literal must be duplicated to trigger an issue
const DEFAULT_THRESHOLD = 3;
const MIN_LENGTH = 10;
const NO_SEPARATOR_REGEXP = /^\w*$/;
const EXCLUDED_CONTEXTS = [
'ImportDeclaration',
'JSXAttribute',
'ExportAllDeclaration',
'ExportNamedDeclaration',
];
const MESSAGE = 'Define a constant instead of duplicating this literal {{times}} times.';
type Options = [number];
const rule: Rule.RuleModule<string, Options> = {
meta: {
type: 'suggestion',
docs: {
description: 'String literals should not be duplicated',
category: 'Best Practices',
recommended: 'error',
url: docsUrl(__filename),
},
schema: [{ type: 'integer', minimum: 2 }],
},
create(context) {
const literalsByValue: Map<string, TSESTree.Literal[]> = new Map();
const threshold: number =
context.options[0] !== undefined ? context.options[0] : DEFAULT_THRESHOLD;
return {
Literal: (node: TSESTree.Node) => {
const literal = node as TSESTree.Literal;
const { parent } = literal;
if (
typeof literal.value === 'string' &&
parent &&
!['ExpressionStatement', 'TSLiteralType'].includes(parent.type)
) {
const stringContent = literal.value.trim();
if (
!isExcludedByUsageContext(context, literal) &&
stringContent.length >= MIN_LENGTH &&
!stringContent.match(NO_SEPARATOR_REGEXP)
) {
const sameStringLiterals = literalsByValue.get(stringContent) || [];
sameStringLiterals.push(literal);
literalsByValue.set(stringContent, sameStringLiterals);
}
}
},
'Program:exit'() {
literalsByValue.forEach(literals => {
if (literals.length >= threshold) {
context.report({
message: MESSAGE,
node: literals[0],
data: { times: literals.length.toString() },
});
}
});
},
};
},
};
function isExcludedByUsageContext(context: Rule.RuleContext, literal: TSESTree.Literal) {
const parent = literal.parent!;
const parentType = parent.type;
return (
EXCLUDED_CONTEXTS.includes(parentType) ||
isRequireContext(parent, context) ||
isObjectPropertyKey(parent, literal)
);
}
function isRequireContext(parent: TSESTree.Node, context: Rule.RuleContext) {
return (
parent.type === 'CallExpression' && context.getSourceCode().getText(parent.callee) === 'require'
);
}
function isObjectPropertyKey(parent: TSESTree.Node, literal: TSESTree.Literal) {
return parent.type === 'Property' && parent.key === literal;
}
export = rule;