Skip to content

Commit

Permalink
Merge eab54ca into 52b4f48
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac committed Sep 13, 2018
2 parents 52b4f48 + eab54ca commit ab97d3f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const rule = require("../").rules;
const RuleTester = require("eslint").RuleTester;

RuleTester.setDefaultConfig({
parser: "babel-eslint",
parserOptions: {
ecmaVersion: 6,
sourceType: "module"
Expand All @@ -13,6 +14,30 @@ RuleTester.setDefaultConfig({
var ruleTester = new RuleTester();

describe("ESLint plugin", () => {
ruleTester.run("no-dynamic-import-moment", rule["no-dynamic-import-moment"], {
valid: ["import('date-fns').then()"],
invalid: [
{
code: "import('moment').then(()=>{})",
errors: [
{
message: "Use date-fns or Native Date methods instead of moment.js",
type: "CallExpression"
}
]
},
{
code: "(async () => { const m = await import('moment'); })()",
errors: [
{
message: "Use date-fns or Native Date methods instead of moment.js",
type: "CallExpression"
}
]
}
]
});

ruleTester.run("no-import-moment", rule["no-import-moment"], {
valid: [
"import { format, formatDistance, formatRelative, subDays } from 'date-fns'"
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
const rules = {
"no-dynamic-import-moment": require("./rules/no-dynamic-import-moment"),
"no-import-moment": require("./rules/no-import-moment"),
"no-moment-constructor": require("./rules/no-moment-constructor"),
...require("./rules/no-moment-methods")
Expand Down
17 changes: 17 additions & 0 deletions lib/rules/no-dynamic-import-moment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var message = "Use date-fns or Native Date methods instead of moment.js";

module.exports = function(context) {
return {
CallExpression: function(node) {
if (node.callee.type !== "Import") {
return;
}

var arg = node.arguments[0];

if (arg.type === "Literal" && arg.value === "moment") {
context.report(node, message);
}
}
};
};

0 comments on commit ab97d3f

Please sign in to comment.