Skip to content

Commit 7ef39f4

Browse files
committed
Added babel/no-invalid-this rule. (Closes babel#12)
1 parent 80d66bc commit 7ef39f4

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ original ones as well!).
3434
"babel/arrow-parens": 1,
3535
"babel/no-await-in-loop": 1,
3636
"babel/flow-object-type": 1,
37-
"babel/func-params-comma-dangle": 1
37+
"babel/func-params-comma-dangle": 1,
38+
"babel/no-invalid-this": 1
3839
}
3940
}
4041
```
@@ -50,6 +51,7 @@ Each rule corresponds to a core `eslint` rule, and has the same options.
5051
- `babel/object-curly-spacing`: doesn't complain about `export x from "mod";` or `export * as x from "mod";` (🛠 )
5152
- `babel/object-shorthand`: doesn't fail when using object spread (`...obj`)
5253
- `babel/arrow-parens`: Handles async functions correctly (🛠 )
54+
- `babel/no-invalid-this`: doesn't fail when inside class properties (`class A { a = this.b; }`)
5355

5456
The following rules are not in `eslint`, but are relevant only to syntax that is not specified by
5557
the current JavaScript standard or supported by `eslint`.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"devDependencies": {
3030
"babel-eslint": "^6.1.0",
3131
"eslint": "^3.0.0",
32+
"lodash.clonedeep": "^4.5.0",
3233
"mocha": "^3.0.0"
3334
}
3435
}

rules/no-invalid-this.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ module.exports = {
3030
const stack = [],
3131
sourceCode = context.getSourceCode();
3232

33+
let insideClassProperty = false;
34+
3335
/**
3436
* Gets the current checking context.
3537
*
@@ -51,6 +53,23 @@ module.exports = {
5153
return current;
5254
};
5355

56+
/**
57+
* `this` should be fair game anywhere inside a class property.
58+
*
59+
* @returns {void}
60+
*/
61+
function enterClassProperty() {
62+
insideClassProperty = true;
63+
}
64+
65+
/**
66+
* Back to the normal check.
67+
* @returns {void}
68+
*/
69+
function exitClassProperty() {
70+
insideClassProperty = false;
71+
}
72+
5473
/**
5574
* Pushs new checking context into the stack.
5675
*
@@ -104,6 +123,8 @@ module.exports = {
104123
stack.pop();
105124
},
106125

126+
ClassProperty: enterClassProperty,
127+
"ClassProperty:exit": exitClassProperty,
107128
FunctionDeclaration: enterFunction,
108129
"FunctionDeclaration:exit": exitFunction,
109130
FunctionExpression: enterFunction,
@@ -113,7 +134,7 @@ module.exports = {
113134
ThisExpression(node) {
114135
const current = stack.getCurrent();
115136

116-
if (current && !current.valid) {
137+
if (!insideClassProperty && current && !current.valid) {
117138
context.report(node, "Unexpected 'this'.");
118139
}
119140
}

tests/rules/no-invalid-this.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Requirements
1010
//------------------------------------------------------------------------------
1111

12-
const lodash = require("lodash");
12+
const cloneDeep = require("lodash.clonedeep");
1313
const rule = require("../../rules/no-invalid-this");
1414
const RuleTester = require("../RuleTester");
1515

@@ -68,7 +68,7 @@ function extractPatterns(patterns, type) {
6868
// Clone and apply the pattern environment.
6969
const patternsList = patterns.map(function(pattern) {
7070
return pattern[type].map(function(applyCondition) {
71-
const thisPattern = lodash.cloneDeep(pattern);
71+
const thisPattern = cloneDeep(pattern);
7272

7373
applyCondition(thisPattern);
7474

@@ -582,6 +582,23 @@ const patterns = [
582582
valid: [NORMAL],
583583
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES]
584584
},
585+
586+
// babel/no-invalid-this
587+
588+
// Class Instance Properties.
589+
{
590+
code: "class A {a = this.b;};",
591+
parserOptions: { ecmaVersion: 6 },
592+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
593+
invalid: []
594+
},
595+
596+
{
597+
code: "class A {a = () => {return this.b;};};",
598+
parserOptions: { ecmaVersion: 6 },
599+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
600+
invalid: []
601+
},
585602
];
586603

587604
const ruleTester = new RuleTester();

0 commit comments

Comments
 (0)