1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | "use strict";
|
9 |
|
10 | module.exports = function(context) {
|
11 |
|
12 | var mode = (function(option) {
|
13 | if (option == null || typeof option === "string") {
|
14 | return {
|
15 | before: { before: true, after: false },
|
16 | after: { before: false, after: true },
|
17 | both: { before: true, after: true },
|
18 | neither: { before: false, after: false }
|
19 | }[option || "before"];
|
20 | }
|
21 | return option;
|
22 | }(context.options[0]));
|
23 |
|
24 | function isAsyncGenerator(node){
|
25 | return context.getFirstToken(node, 2).value === '*'
|
26 | }
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function checkSpacing(side, leftToken, rightToken) {
|
38 | if (!!(rightToken.range[0] - leftToken.range[1]) !== mode[side]) {
|
39 | context.report(
|
40 | leftToken.value === "*" ? leftToken : rightToken,
|
41 | "{{type}} space {{side}} *.",
|
42 | {
|
43 | type: mode[side] ? "Missing" : "Unexpected",
|
44 | side: side
|
45 | }
|
46 | );
|
47 | }
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | function checkFunction(node) {
|
56 | var first = context.getFirstToken(node)
|
57 | , isMethod = node.parent.method || node.parent.type === "MethodDefinition"
|
58 | , isAsync = first.value === 'async';
|
59 |
|
60 | var prevToken, starToken, nextToken;
|
61 |
|
62 |
|
63 | if ( !node.generator || (isAsync && !isAsyncGenerator(node))) {
|
64 | return;
|
65 | }
|
66 |
|
67 | if (isMethod) {
|
68 | starToken = context.getTokenBefore(node, 1);
|
69 | } else {
|
70 | starToken = context.getFirstToken(node, isAsync ? 2 : 1);
|
71 | }
|
72 |
|
73 |
|
74 | prevToken = context.getTokenBefore(starToken);
|
75 | if (prevToken.value === "function" || prevToken.value === "static") {
|
76 | checkSpacing("before", prevToken, starToken);
|
77 | }
|
78 |
|
79 |
|
80 | nextToken = context.getTokenAfter(starToken);
|
81 | if (nextToken.type === "Identifier") {
|
82 | checkSpacing("after", starToken, nextToken);
|
83 | }
|
84 | }
|
85 |
|
86 | return {
|
87 | "FunctionDeclaration": checkFunction,
|
88 | "FunctionExpression": checkFunction
|
89 | };
|
90 |
|
91 | };
|
92 |
|
93 | module.exports.schema = [
|
94 | {
|
95 | "oneOf": [
|
96 | {
|
97 | "enum": ["before", "after", "both", "neither"]
|
98 | },
|
99 | {
|
100 | "type": "object",
|
101 | "properties": {
|
102 | "before": {"type": "boolean"},
|
103 | "after": {"type": "boolean"}
|
104 | },
|
105 | "additionalProperties": false
|
106 | }
|
107 | ]
|
108 | }
|
109 | ];
|