This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
/
exportNameRule.ts
216 lines (185 loc) · 8.14 KB
/
exportNameRule.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';
import { Utils } from './utils/Utils';
import { AstUtils } from './utils/AstUtils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';
interface Options {
allExceptions: string[] | undefined;
ignoreCase: boolean;
}
export const OPTION_IGNORE_CASE: string = 'ignore-case';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: 'export-name',
type: 'maintainability',
description: 'The name of the exported module must match the filename of the source file',
options: {
type: 'list',
listType: {
anyOf: [
{
type: 'string'
},
{
type: 'object',
properties: {
'ignore-case': {
type: 'boolean'
},
allow: {
type: 'array',
items: {
type: 'string'
}
}
}
}
]
}
},
optionsDescription: '',
typescriptOnly: true,
issueClass: 'Ignored',
issueType: 'Warning',
severity: 'Low',
level: 'Opportunity for Excellence',
group: 'Clarity',
commonWeaknessEnumeration: '710'
};
public static FAILURE_STRING: string = 'The exported module or identifier name must match the file name. Found: ';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
}
private parseOptions(options: Lint.IOptions): Options {
return {
allExceptions: this.getExceptions(options),
ignoreCase: this.getIgnoreCase(options)
};
}
/* tslint:disable:function-name */
public getExceptions(options: Lint.IOptions): string[] | undefined {
/* tslint:enable:function-name */
if (options.ruleArguments instanceof Array) {
const ruleArg = options.ruleArguments[0];
return typeof ruleArg === 'object' ? ruleArg.allow : options.ruleArguments;
}
if (options instanceof Array) {
return typeof options[0] === 'object' ? options[0].allow : options;
}
return undefined;
}
/* tslint:disable:function-name */
public getIgnoreCase(options: Lint.IOptions): boolean {
/* tslint:enable:function-name */
if (options instanceof Array) {
return typeof options[0] === 'object' ? options[0]['ignore-case'] : true;
}
return true;
}
}
function isExportedDeclaration(element: ts.Statement): boolean {
return element.modifiers !== undefined && AstUtils.hasModifier(element.modifiers, ts.SyntaxKind.ExportKeyword);
}
type ExportStatement = ts.ExportDeclaration | ts.ExportAssignment;
function isExportStatement(node: ts.Statement): node is ExportStatement {
return ts.isExportAssignment(node) || ts.isExportDeclaration(node);
}
function getExportsFromStatement(node: ExportStatement): [string, ts.Node][] {
if (ts.isExportAssignment(node)) {
return [[node.expression.getText(), node.expression]];
}
if (node.exportClause) {
const symbolAndNodes: [string, ts.Node][] = [];
node.exportClause.elements.forEach(e => {
symbolAndNodes.push([e.name.getText(), node]);
});
return symbolAndNodes;
}
// Re-exports `export * from ...` do not have export clause - no names to validate.
// Effectively will be skipped in check later.
return [];
}
function walk(ctx: Lint.WalkContext<Options>) {
const { allExceptions, ignoreCase } = ctx.options;
function getExportStatementsWithinModules(moduleDeclaration: ts.ModuleDeclaration): ts.Statement[] | undefined {
if (moduleDeclaration.body === undefined) {
return undefined;
}
if (moduleDeclaration.body.kind === ts.SyntaxKind.ModuleDeclaration) {
// modules may be nested so recur into the structure
return getExportStatementsWithinModules(<ts.ModuleDeclaration>moduleDeclaration.body);
}
if (moduleDeclaration.body.kind === ts.SyntaxKind.ModuleBlock) {
const moduleBlock: ts.ModuleBlock = <ts.ModuleBlock>moduleDeclaration.body;
return moduleBlock.statements.filter(isExportedDeclaration);
}
return undefined;
}
function validateExportedElements(exportedElements: ts.Statement[]): void {
// only validate the exported elements when a single export statement is made
if (exportedElements.length === 1) {
const element = exportedElements[0];
if (ts.isModuleDeclaration(element) || ts.isClassDeclaration(element) || ts.isFunctionDeclaration(element)) {
if (element.name !== undefined) {
validateExport(element.name.text, exportedElements[0]);
}
} else if (exportedElements[0].kind === ts.SyntaxKind.VariableStatement) {
const variableStatement: ts.VariableStatement = <ts.VariableStatement>exportedElements[0];
// ignore comma separated variable lists
if (variableStatement.declarationList.declarations.length === 1) {
const variableDeclaration: ts.VariableDeclaration = variableStatement.declarationList.declarations[0];
validateExport(variableDeclaration.name.getText(), variableDeclaration);
}
}
}
}
function validateExport(exportedName: string, tsNode: ts.Node): void {
const flags = ignoreCase ? 'i' : '';
const regex: RegExp = new RegExp(`^${exportedName}\\..+`, flags); // filename must be exported name plus any extension
const fileName = Utils.fileBasename(ctx.sourceFile.fileName);
const fileNameAsCamelCase = convertSnakeOrKebabCaseName(fileName);
if (!regex.test(fileNameAsCamelCase)) {
if (!isSuppressed(exportedName)) {
const failureString: string = Rule.FAILURE_STRING + fileName + ' and ' + exportedName;
ctx.addFailureAt(tsNode.getStart(), tsNode.getWidth(), failureString);
}
}
}
function convertSnakeOrKebabCaseName(rawName: string): string {
const snakeOrKebabRegex = /((\-|\_)\w)/g;
return rawName.replace(snakeOrKebabRegex, (match: string): string => match[1].toUpperCase());
}
function isSuppressed(exportedName: string): boolean {
return Utils.exists(
allExceptions,
(exception: string): boolean => {
return new RegExp(exception).test(exportedName);
}
);
}
const node: ts.SourceFile = ctx.sourceFile;
// look for single export assignment from file first
const singleExport = node.statements.filter(isExportStatement);
if (singleExport.length === 1) {
const symbolsAndNodes = getExportsFromStatement(singleExport[0]);
if (symbolsAndNodes.length === 1) {
validateExport(symbolsAndNodes[0][0], symbolsAndNodes[0][1]);
}
return; // there is a single export and it is valid, so do not proceed
}
// exports are normally declared at the top level
let exportedTopLevelElements: ts.Statement[] = node.statements.filter(isExportedDeclaration);
// exports might be hidden inside a namespace
if (exportedTopLevelElements.length === 0) {
node.statements.forEach(
(element: ts.Statement): void => {
if (tsutils.isModuleDeclaration(element)) {
const exportStatements = getExportStatementsWithinModules(element) || [];
exportedTopLevelElements = exportedTopLevelElements.concat(exportStatements);
}
}
);
}
validateExportedElements(exportedTopLevelElements);
}