-
-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathno-decorators-before-export.js
More file actions
73 lines (66 loc) · 2.16 KB
/
no-decorators-before-export.js
File metadata and controls
73 lines (66 loc) · 2.16 KB
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
'use strict';
/** @type {import('eslint').Rule.RuleModule} */
function reportInvalidSyntax(context, node) {
if (isInvalidSyntax(node)) {
const isDefaultExport = node.type === 'ExportDefaultDeclaration';
const isNamed = node.declaration.id !== null;
const name = isNamed ? node.declaration.id.name : '';
const numDecorators = node.declaration.decorators.length;
const message = `Usage of class decorator${numDecorators > 1 ? 's' : ''} on the ${
isNamed ? '' : 'un-named '
}${isDefaultExport ? 'default ' : ''}export${
isNamed ? ` ${name}` : ''
} must occur prior to exporting the class.`;
if (isNamed) {
const sourceCode = context.getSourceCode();
const src = sourceCode.getText(node);
context.report({
node,
message,
fix(fixer) {
if (isDefaultExport) {
let newSrc = src.replace(`export default class ${name}`, `class ${name}`);
newSrc = `${newSrc}\nexport default ${name};\n`;
return fixer.replaceText(node, newSrc);
}
let newSrc = src.replace(`export class ${name}`, `class ${name}`);
newSrc = `${newSrc}\nexport { ${name} };\n`;
return fixer.replaceText(node, newSrc);
},
});
} else {
context.report({ node, message });
}
}
}
function isInvalidSyntax(node) {
return (
node.declaration &&
node.declaration.type === 'ClassDeclaration' &&
node.declaration.decorators?.length
);
}
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'disallow the use of a class decorator on an export declaration. Enforces use of the decorator on the class directly before exporting the result.',
category: 'Miscellaneous',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-decorators-before-export.md',
},
fixable: 'code',
schema: [],
},
create(context) {
return {
ExportDefaultDeclaration(node) {
return reportInvalidSyntax(context, node);
},
ExportNamedDeclaration(node) {
return reportInvalidSyntax(context, node);
},
};
},
};