UNPKG

1.25 kBJavaScriptView Raw
1const {docsUrl} = require('../utilities');
2
3module.exports = {
4 meta: {
5 docs: {
6 description: 'Require (or disallow) semicolons for class properties.',
7 category: 'Stylistic Issues',
8 recommended: false,
9 uri: docsUrl('class-property-semi'),
10 },
11 fixable: 'code',
12 schema: [
13 {
14 enum: ['always', 'never'],
15 },
16 ],
17 },
18
19 create(context) {
20 const config = context.options[0] || 'always';
21 const always = config === 'always';
22
23 function isSemicolon(token) {
24 return token.type === 'Punctuator' && token.value === ';';
25 }
26
27 function checkClassProperty(node) {
28 const lastToken = context.getLastToken(node);
29 const hasSemicolon = isSemicolon(lastToken);
30
31 if (always && !hasSemicolon) {
32 context.report({
33 node,
34 message: 'Missing semicolon.',
35 fix(fixer) {
36 fixer.insertTextAfter(lastToken, ';');
37 },
38 });
39 } else if (!always && hasSemicolon) {
40 context.report({
41 node,
42 message: 'Extra semicolon.',
43 fix(fixer) {
44 fixer.remove(lastToken);
45 },
46 });
47 }
48 }
49
50 return {
51 ClassProperty: checkClassProperty,
52 };
53 },
54};