linting: disallow t.identifier("undefined") in plugins (#6096)
* add new custom eslint rule, replace remaining t.identifier("undefined") with buildUndefinedNode(), update tests
* change no-undefined-identifier reporting descriptor
This commit is contained in:
parent
4577bd1b7c
commit
2db0c3ad1d
4
Makefile
4
Makefile
@ -29,13 +29,13 @@ watch: clean
|
||||
BABEL_ENV=development ./node_modules/.bin/gulp watch
|
||||
|
||||
lint:
|
||||
./node_modules/.bin/eslint scripts packages *.js --format=codeframe
|
||||
./node_modules/.bin/eslint scripts packages *.js --format=codeframe --rulesdir="./eslint_rules"
|
||||
|
||||
flow:
|
||||
./node_modules/.bin/flow check --strip-root
|
||||
|
||||
fix:
|
||||
./node_modules/.bin/eslint scripts packages *.js --format=codeframe --fix
|
||||
./node_modules/.bin/eslint scripts packages *.js --format=codeframe --fix --rulesdir="./eslint_rules"
|
||||
|
||||
clean: test-clean
|
||||
rm -rf packages/babel-polyfill/browser*
|
||||
|
||||
46
eslint_rules/no-undefined-identifier.js
Normal file
46
eslint_rules/no-undefined-identifier.js
Normal file
@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
function argumentsIsUndefinedString(argumentsArray) {
|
||||
return (
|
||||
argumentsArray.length === 1 &&
|
||||
argumentsArray[0].type === "Literal" &&
|
||||
argumentsArray[0].value === "undefined"
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
schema: [],
|
||||
},
|
||||
create: function(context) {
|
||||
if (context.getFilename().indexOf("packages/babel-plugin-") === -1) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
CallExpression: function(node) {
|
||||
const callee = node.callee;
|
||||
|
||||
if (
|
||||
callee.type === "MemberExpression" &&
|
||||
argumentsIsUndefinedString(node.arguments)
|
||||
) {
|
||||
const object = callee.object,
|
||||
property = callee.property;
|
||||
|
||||
if (
|
||||
object.type === "Identifier" &&
|
||||
object.name === "t" &&
|
||||
property.type === "Identifier" &&
|
||||
property.name === "identifier"
|
||||
) {
|
||||
context.report(
|
||||
node,
|
||||
"Use path.scope.buildUndefinedNode() to create an undefined identifier directly."
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@ -67,7 +67,7 @@
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.js": [
|
||||
"eslint --format=codeframe --fix",
|
||||
"eslint --format=codeframe --fix --rulesdir='./eslint_rules'",
|
||||
"git add"
|
||||
]
|
||||
},
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
{
|
||||
"rules": {
|
||||
"prettier/prettier": ["error", { "trailingComma": "all" }]
|
||||
"prettier/prettier": ["error", { "trailingComma": "all" }],
|
||||
"no-undefined-identifier": 2
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.foo = undefined;
|
||||
exports.foo = void 0;
|
||||
|
||||
let foo = exports.foo = (() => {
|
||||
var _ref = babelHelpers.asyncToGenerator(function* () {});
|
||||
|
||||
@ -4,7 +4,7 @@ let foo = (() => {
|
||||
var _ref = _asyncToGenerator(function* (_ref2) {
|
||||
let a = _ref2.a,
|
||||
_ref2$b = _ref2.b,
|
||||
b = _ref2$b === undefined ? mandatory("b") : _ref2$b;
|
||||
b = _ref2$b === void 0 ? mandatory("b") : _ref2$b;
|
||||
return Promise.resolve(b);
|
||||
});
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = undefined;
|
||||
exports.default = void 0;
|
||||
|
||||
var _net = require("net");
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = undefined;
|
||||
exports.default = void 0;
|
||||
|
||||
var _BaseFoo2 = require("./BaseFoo");
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = undefined;
|
||||
exports.default = void 0;
|
||||
|
||||
var _react = require("react");
|
||||
|
||||
|
||||
@ -112,7 +112,11 @@ export default function({ types: t }) {
|
||||
//
|
||||
|
||||
const tempConditional = t.conditionalExpression(
|
||||
t.binaryExpression("===", tempValueRef, t.identifier("undefined")),
|
||||
t.binaryExpression(
|
||||
"===",
|
||||
tempValueRef,
|
||||
this.scope.buildUndefinedNode(),
|
||||
),
|
||||
pattern.right,
|
||||
tempValueRef,
|
||||
);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
var z = {};
|
||||
var _z$x = z.x;
|
||||
_z$x = _z$x === undefined ? {} : _z$x;
|
||||
_z$x = _z$x === void 0 ? {} : _z$x;
|
||||
var y = _z$x.y;
|
||||
|
||||
@ -4,5 +4,5 @@ var _ref = {},
|
||||
_ref$c = _ref.c,
|
||||
d = _ref$c.d,
|
||||
_ref$c$e$f = _ref$c.e.f,
|
||||
f = _ref$c$e$f === undefined ? 4 : _ref$c$e$f;
|
||||
f = _ref$c$e$f === void 0 ? 4 : _ref$c$e$f;
|
||||
export { a, b, d, f };
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
function somethingAdvanced(_ref, p2, p3) {
|
||||
var _ref$topLeft = _ref.topLeft;
|
||||
_ref$topLeft = _ref$topLeft === undefined ? {} : _ref$topLeft;
|
||||
_ref$topLeft = _ref$topLeft === void 0 ? {} : _ref$topLeft;
|
||||
var x1 = _ref$topLeft.x,
|
||||
y1 = _ref$topLeft.y,
|
||||
_ref$bottomRight = _ref.bottomRight;
|
||||
_ref$bottomRight = _ref$bottomRight === undefined ? {} : _ref$bottomRight;
|
||||
_ref$bottomRight = _ref$bottomRight === void 0 ? {} : _ref$bottomRight;
|
||||
var x2 = _ref$bottomRight.x,
|
||||
y2 = _ref$bottomRight.y;
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = undefined;
|
||||
exports.default = void 0;
|
||||
|
||||
var _last2 = require("lodash/last");
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = undefined;
|
||||
exports.default = void 0;
|
||||
|
||||
var _store = require("./store");
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@ define(["exports", "./evens"], function (exports, _evens) {
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.isOdd = undefined;
|
||||
exports.isOdd = void 0;
|
||||
exports.nextOdd = nextOdd;
|
||||
|
||||
function nextOdd(n) {
|
||||
|
||||
@ -4,7 +4,7 @@ define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports,
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.test2 = exports.test = undefined;
|
||||
exports.test2 = exports.test = void 0;
|
||||
var foo2 = babelHelpers.interopRequireWildcard(_foo);
|
||||
exports.test = test;
|
||||
var test2 = exports.test2 = 5;
|
||||
|
||||
@ -201,7 +201,7 @@ export default function() {
|
||||
state.opts.allowTopLevelThis !== true &&
|
||||
!path.findParent(path => THIS_BREAK_KEYS.indexOf(path.type) >= 0)
|
||||
) {
|
||||
path.replaceWith(t.identifier("undefined"));
|
||||
path.replaceWith(path.scope.buildUndefinedNode());
|
||||
}
|
||||
},
|
||||
|
||||
@ -568,7 +568,7 @@ export default function() {
|
||||
maxHoistedExportsNodeAssignmentLength,
|
||||
);
|
||||
|
||||
let hoistedExportsNode = t.identifier("undefined");
|
||||
let hoistedExportsNode = scope.buildUndefinedNode();
|
||||
|
||||
nonHoistedExportNamesChunk.forEach(function(name) {
|
||||
hoistedExportsNode = buildExportsAssignment(
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.test2 = exports.test = undefined;
|
||||
exports.test2 = exports.test = void 0;
|
||||
|
||||
/*after*/
|
||||
|
||||
|
||||
@ -3,8 +3,8 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.foo100 = undefined;
|
||||
exports.foo99 = exports.foo98 = exports.foo97 = exports.foo96 = exports.foo95 = exports.foo94 = exports.foo93 = exports.foo92 = exports.foo91 = exports.foo90 = exports.foo89 = exports.foo88 = exports.foo87 = exports.foo86 = exports.foo85 = exports.foo84 = exports.foo83 = exports.foo82 = exports.foo81 = exports.foo80 = exports.foo79 = exports.foo78 = exports.foo77 = exports.foo76 = exports.foo75 = exports.foo74 = exports.foo73 = exports.foo72 = exports.foo71 = exports.foo70 = exports.foo69 = exports.foo68 = exports.foo67 = exports.foo66 = exports.foo65 = exports.foo64 = exports.foo63 = exports.foo62 = exports.foo61 = exports.foo60 = exports.foo59 = exports.foo58 = exports.foo57 = exports.foo56 = exports.foo55 = exports.foo54 = exports.foo53 = exports.foo52 = exports.foo51 = exports.foo50 = exports.foo49 = exports.foo48 = exports.foo47 = exports.foo46 = exports.foo45 = exports.foo44 = exports.foo43 = exports.foo42 = exports.foo41 = exports.foo40 = exports.foo39 = exports.foo38 = exports.foo37 = exports.foo36 = exports.foo35 = exports.foo34 = exports.foo33 = exports.foo32 = exports.foo31 = exports.foo30 = exports.foo29 = exports.foo28 = exports.foo27 = exports.foo26 = exports.foo25 = exports.foo24 = exports.foo23 = exports.foo22 = exports.foo21 = exports.foo20 = exports.foo19 = exports.foo18 = exports.foo17 = exports.foo16 = exports.foo15 = exports.foo14 = exports.foo13 = exports.foo12 = exports.foo11 = exports.foo10 = exports.foo9 = exports.foo8 = exports.foo7 = exports.foo6 = exports.foo5 = exports.foo4 = exports.foo3 = exports.foo2 = exports.foo1 = exports.foo = undefined;
|
||||
exports.foo100 = void 0;
|
||||
exports.foo99 = exports.foo98 = exports.foo97 = exports.foo96 = exports.foo95 = exports.foo94 = exports.foo93 = exports.foo92 = exports.foo91 = exports.foo90 = exports.foo89 = exports.foo88 = exports.foo87 = exports.foo86 = exports.foo85 = exports.foo84 = exports.foo83 = exports.foo82 = exports.foo81 = exports.foo80 = exports.foo79 = exports.foo78 = exports.foo77 = exports.foo76 = exports.foo75 = exports.foo74 = exports.foo73 = exports.foo72 = exports.foo71 = exports.foo70 = exports.foo69 = exports.foo68 = exports.foo67 = exports.foo66 = exports.foo65 = exports.foo64 = exports.foo63 = exports.foo62 = exports.foo61 = exports.foo60 = exports.foo59 = exports.foo58 = exports.foo57 = exports.foo56 = exports.foo55 = exports.foo54 = exports.foo53 = exports.foo52 = exports.foo51 = exports.foo50 = exports.foo49 = exports.foo48 = exports.foo47 = exports.foo46 = exports.foo45 = exports.foo44 = exports.foo43 = exports.foo42 = exports.foo41 = exports.foo40 = exports.foo39 = exports.foo38 = exports.foo37 = exports.foo36 = exports.foo35 = exports.foo34 = exports.foo33 = exports.foo32 = exports.foo31 = exports.foo30 = exports.foo29 = exports.foo28 = exports.foo27 = exports.foo26 = exports.foo25 = exports.foo24 = exports.foo23 = exports.foo22 = exports.foo21 = exports.foo20 = exports.foo19 = exports.foo18 = exports.foo17 = exports.foo16 = exports.foo15 = exports.foo14 = exports.foo13 = exports.foo12 = exports.foo11 = exports.foo10 = exports.foo9 = exports.foo8 = exports.foo7 = exports.foo6 = exports.foo5 = exports.foo4 = exports.foo3 = exports.foo2 = exports.foo1 = exports.foo = void 0;
|
||||
|
||||
var _foo = require("foo");
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.isOdd = undefined;
|
||||
exports.isOdd = void 0;
|
||||
exports.nextOdd = nextOdd;
|
||||
|
||||
var _evens = require("./evens");
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.test2 = exports.test = undefined;
|
||||
exports.test2 = exports.test = void 0;
|
||||
|
||||
require("foo");
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.isOdd = undefined;
|
||||
exports.isOdd = void 0;
|
||||
exports.nextOdd = nextOdd;
|
||||
|
||||
function nextOdd(n) {
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.test2 = exports.test = undefined;
|
||||
exports.test2 = exports.test = void 0;
|
||||
var foo2 = babelHelpers.interopRequireWildcard(_foo);
|
||||
exports.test = test;
|
||||
var test2 = exports.test2 = 5;
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
function a() {
|
||||
var foo = function () {
|
||||
return bar.apply(undefined, arguments);
|
||||
return bar.apply(void 0, arguments);
|
||||
};
|
||||
|
||||
foo.apply(undefined, arguments);
|
||||
foo.apply(void 0, arguments);
|
||||
}
|
||||
|
||||
function b() {
|
||||
var foo = function () {
|
||||
return bar.apply(undefined, arguments);
|
||||
return bar.apply(void 0, arguments);
|
||||
};
|
||||
|
||||
foo.apply(undefined, arguments);
|
||||
foo.apply(void 0, arguments);
|
||||
}
|
||||
|
||||
function c() {
|
||||
var foo = function () {
|
||||
return bar.apply(undefined, arguments);
|
||||
return bar.apply(void 0, arguments);
|
||||
};
|
||||
|
||||
foo([]);
|
||||
@ -24,7 +24,7 @@ function c() {
|
||||
|
||||
function d(thing) {
|
||||
var foo = function () {
|
||||
return bar.apply(undefined, arguments);
|
||||
return bar.apply(void 0, arguments);
|
||||
};
|
||||
|
||||
{
|
||||
|
||||
@ -15,6 +15,6 @@ function broken(x) {
|
||||
foo[_key - 1] = arguments[_key];
|
||||
}
|
||||
|
||||
return hello.apply(undefined, foo);
|
||||
return hello.apply(void 0, foo);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
// optimisation
|
||||
function foo() {
|
||||
foo.apply(undefined, arguments);
|
||||
foo.apply(void 0, arguments);
|
||||
} // deoptimisation
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ function foo(a) {
|
||||
b[_key - 1] = arguments[_key];
|
||||
}
|
||||
|
||||
foo.apply(undefined, b);
|
||||
foo.apply(void 0, b);
|
||||
}
|
||||
|
||||
function foo() {
|
||||
@ -17,7 +17,7 @@ function foo() {
|
||||
b[_key2] = arguments[_key2];
|
||||
}
|
||||
|
||||
foo.apply(undefined, [1].concat(b));
|
||||
foo.apply(void 0, [1].concat(b));
|
||||
}
|
||||
|
||||
function foo() {
|
||||
@ -26,5 +26,5 @@ function foo() {
|
||||
}
|
||||
|
||||
args.pop();
|
||||
foo.apply(undefined, args);
|
||||
foo.apply(void 0, args);
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ export default function({ types: t }) {
|
||||
const calleePath = path.get("callee");
|
||||
if (calleePath.isSuper()) return;
|
||||
|
||||
let contextLiteral = t.identifier("undefined");
|
||||
let contextLiteral = scope.buildUndefinedNode();
|
||||
|
||||
node.arguments = [];
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
function foo() {
|
||||
return bar.apply(undefined, ["test"].concat(Array.prototype.slice.call(arguments)));
|
||||
return bar.apply(void 0, ["test"].concat(Array.prototype.slice.call(arguments)));
|
||||
}
|
||||
|
||||
function bar(one, two, three) {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
function foo() {
|
||||
return bar.apply(undefined, arguments);
|
||||
return bar.apply(void 0, arguments);
|
||||
}
|
||||
|
||||
function bar(one, two, three) {
|
||||
|
||||
@ -1 +1 @@
|
||||
f.apply(undefined, [1, 2, 3]);
|
||||
f.apply(void 0, [1, 2, 3]);
|
||||
|
||||
@ -1 +1 @@
|
||||
add.apply(undefined, babelHelpers.toConsumableArray(numbers).concat([foo, bar]));
|
||||
add.apply(void 0, babelHelpers.toConsumableArray(numbers).concat([foo, bar]));
|
||||
|
||||
@ -1 +1 @@
|
||||
add.apply(undefined, [foo].concat(babelHelpers.toConsumableArray(numbers), [bar]));
|
||||
add.apply(void 0, [foo].concat(babelHelpers.toConsumableArray(numbers), [bar]));
|
||||
|
||||
@ -1 +1 @@
|
||||
add.apply(undefined, [foo, bar].concat(babelHelpers.toConsumableArray(numbers)));
|
||||
add.apply(void 0, [foo, bar].concat(babelHelpers.toConsumableArray(numbers)));
|
||||
|
||||
@ -1 +1 @@
|
||||
add.apply(undefined, [foo].concat(babelHelpers.toConsumableArray(numbers), [bar, what], babelHelpers.toConsumableArray(test)));
|
||||
add.apply(void 0, [foo].concat(babelHelpers.toConsumableArray(numbers), [bar, what], babelHelpers.toConsumableArray(test)));
|
||||
|
||||
@ -1 +1 @@
|
||||
add.apply(undefined, babelHelpers.toConsumableArray(numbers));
|
||||
add.apply(void 0, babelHelpers.toConsumableArray(numbers));
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var foo = () => undefined;
|
||||
var foo = () => void 0;
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
undefined.foo();
|
||||
(void 0).foo();
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
var self = undefined;
|
||||
var self = void 0;
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
undefined;
|
||||
void 0;
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var _this = undefined;
|
||||
var _this = void 0;
|
||||
|
||||
"1".concat(a);
|
||||
(function () {
|
||||
babelHelpers.newArrowCheck(this, _this);
|
||||
}).bind(undefined);
|
||||
}).bind(void 0);
|
||||
|
||||
function a() {
|
||||
var _this2 = this;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user