eslint-plugin-import
eslint-plugin-import copied to clipboard
WIP: named: commonjs exports
This + #1222 closes #1145
This adds a new schema to named
:
commonjs: boolean | { require: boolean, exports: boolean }
The option commonjs
accepts either a boolean or a shape with require
and exports
booleans within. Defaults to false
.
The reason why the shape is allowed is to avoid having commonjs require
statements linted. Using the boolean only option will apply to both require
and exports
.
The following cases of assigning exports are checked:
Object.defineProperty(exports, "key", { value: "myValue" });
Object.defineProperty(module.exports, "key", { value: "myValue" });
Object.defineProperty(module["exports"], "key", { value: "myValue" });
Object.defineProperty(exports, "key", { "value": "myValue" });
Object.defineProperty(module.exports, "key", { "value": "myValue" });
Object.defineProperty(module["exports"], "key", { "value": "myValue" });
exports = {...};
module.exports = {...};
module["exports"] = {...};
exports.key = ...;
module.exports.key = ...;
module["exports"].key = ....;
All declarations must be at the top level. For example, this will not be checked:
if (foo) {
exports.foo = foo;
}
The reason why we check Object.defineProperty
is that babel
will build es6 modules that way, i.e.:
Object.defineProperty(exports, "__esModule", { value: true });
We need to handle those (truthy) __esModule
properties correctly -- as in the default export will be the entire module.exports
if that's not set.
ExportMap.for
and ExportMap.parse
now take an options
parameter (defaults to {}
), with the keys useCommonjsExports
and noInterop
. Reason for noInterop
is that plain require
statements should not have .default
being the entire module.exports
if it doesn't have a truthy __esModule
value.
TODO: Configure for babel-plugin-add-module-exports
built files. (Check for module.exports = exports["default"]
at the end.)
Coverage decreased (-1.2%) to 96.195% when pulling 322d11ecdad008d78671b4b71109f03f2e7a639b on vikr01:commonjs-named-exports into db471a85573a88e0bb9f4a1d53f40fed603651d1 on benmosher:master.
Ah cool! It actually used to do this back in v0.x or v1, but I removed it for efficiency and to narrow the scope. I will have to think about this carefully before reintroducing.
+1 on this "feature", I mostly use exports like this
module.exports = {
foo: true,
...
}
and it is annoying that I have some unnoticed wrong imports.