123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
-
- "use strict";
-
-
-
-
-
-
- function getValue(node) {
- if (node && node.source && node.source.value) {
- return node.source.value.trim();
- }
-
- return "";
- }
-
-
- function checkAndReport(context, node, value, array, message) {
- if (array.indexOf(value) !== -1) {
- context.report({
- node,
- message: "'{{module}}' {{message}}",
- data: {
- module: value,
- message
- }
- });
- }
- }
-
-
-
-
- function handleImports(context, includeExports, importsInFile, exportsInFile) {
- return function(node) {
- const value = getValue(node);
-
- if (value) {
- checkAndReport(context, node, value, importsInFile, "import is duplicated.");
-
- if (includeExports) {
- checkAndReport(context, node, value, exportsInFile, "import is duplicated as export.");
- }
-
- importsInFile.push(value);
- }
- };
- }
-
-
- function handleExports(context, importsInFile, exportsInFile) {
- return function(node) {
- const value = getValue(node);
-
- if (value) {
- checkAndReport(context, node, value, exportsInFile, "export is duplicated.");
- checkAndReport(context, node, value, importsInFile, "export is duplicated as import.");
-
- exportsInFile.push(value);
- }
- };
- }
-
- module.exports = {
- meta: {
- type: "problem",
-
- docs: {
- description: "disallow duplicate module imports",
- category: "ECMAScript 6",
- recommended: false,
- url: "https://eslint.org/docs/rules/no-duplicate-imports"
- },
-
- schema: [{
- type: "object",
- properties: {
- includeExports: {
- type: "boolean"
- }
- },
- additionalProperties: false
- }]
- },
-
- create(context) {
- const includeExports = (context.options[0] || {}).includeExports,
- importsInFile = [],
- exportsInFile = [];
-
- const handlers = {
- ImportDeclaration: handleImports(context, includeExports, importsInFile, exportsInFile)
- };
-
- if (includeExports) {
- handlers.ExportNamedDeclaration = handleExports(context, importsInFile, exportsInFile);
- handlers.ExportAllDeclaration = handleExports(context, importsInFile, exportsInFile);
- }
-
- return handlers;
- }
- };
|