ソースを参照

Fix import path validation

sebavan 6 年 前
コミット
e8c4d1fc0b

+ 73 - 18
Tools/Gulp/helpers/gulp-validateImports.js

@@ -8,51 +8,64 @@ var colorConsole = require("../../NodeHelpers/colorConsole");
 var config = require("../../Config/config");
 
 const indexExlclusion = ["States", "EmitterTypes"];
-const forbiddenImports = ["Meshes/meshBuilder"];
+const forbiddenImports = ["meshBuilder"];
 
 const mapping = { };
 config.modules.forEach(moduleName => {
     mapping[config[moduleName].build.umd.packageName] = moduleName;
 });
 
-var validatePath = function(fileLocation, directory, module, lineNumber, errors) {
+var validatePath = function(fileLocation, directory, module, lineNumber, errors, isExport) {
+    let expressionType = isExport ? "Export" : "Import";
     let internalModulePath = path.join(directory, module + ".ts");
+
     // Check .ts path.
     if (!fs.existsSync(internalModulePath)) {
-        let internalModulePath = path.join(directory, module + ".tsx");
+        internalModulePath = path.join(directory, module + ".tsx");
         // Check .tsx path.
         if (!fs.existsSync(internalModulePath)) {
             // If not found, check index.ts for legacy and index files.
             if (fileLocation.indexOf("legacy") > -1 || fileLocation.indexOf("index") > -1) {
-                let internalModulePath = path.join(directory, module, "index.ts");
+                internalModulePath = path.join(directory, module, "index.ts");
                 if (!fs.existsSync(internalModulePath)) {
-                    errors.push(`Line ${lineNumber} Export from folder only allowes if index is present. ${module}`);
+                    errors.push(`Line ${lineNumber} ${expressionType} from folder only allows if index is present. ${module}`);
                 }
             }
             else {
-                errors.push(`Line ${lineNumber} Imports ${module} needs to be full path (not from directory) for tree shaking.`);
+                errors.push(`Line ${lineNumber} ${expressionType}s ${module} needs to be full path (not from directory) for tree shaking.`);
             }
         }
     }
 
     if (internalModulePath.indexOf("index.") > -1) {
         if (fileLocation.indexOf("legacy") === -1) {
-            let excluded = false;
-            for (let exclusion of indexExlclusion) {
-                if (internalModulePath.indexOf(exclusion) > -1) {
-                    excluded = true;
-                    break;
+            if (isExport) {
+                internalModulePath = path.join(directory, module + ".ts");
+                // Check .ts path.
+                if (!fs.existsSync(internalModulePath)) {
+                    errors.push(`Line ${lineNumber} Exports ${module} should be from the full path including index.`);
                 }
             }
-            if (!excluded) {
-                errors.push(`Line ${lineNumber} Imports ${module} should not be from index for tree shaking.`);
+            else {
+                let excluded = false;
+                for (let exclusion of indexExlclusion) {
+                    if (internalModulePath.indexOf(exclusion) > -1) {
+                        excluded = true;
+                        break;
+                    }
+                }
+                if (!excluded && fileLocation.indexOf("index.ts") === -1) {
+                    errors.push(`Line ${lineNumber} Imports ${module} should not be from index for tree shaking.`);
+                }
             }
         }
     }
 
-    for (let forbiddenImport of forbiddenImports) {
-        if (module.endsWith(forbiddenImport)) {
-            errors.push(`Line ${lineNumber} Imports ${module} is forbidden for tree shaking.`);
+    if (!isExport) {
+        for (let forbiddenImport of forbiddenImports) {
+            if (module.endsWith(forbiddenImport)) {
+                errors.push(`Line ${lineNumber} ${expressionType}s ${module} is forbidden for tree shaking.`);
+            }
         }
     }
 }
@@ -107,7 +120,7 @@ var validateImports = function(data, fileLocation, options) {
 
                     const directory = config[configName].computed.srcDirectory;
                     module = module.substring(splitter);
-                    validatePath(fileLocation, directory, module, index + 1, errors);
+                    validatePath(fileLocation, directory, module, index + 1, errors, false);
                 }
             }
             else {
@@ -117,7 +130,49 @@ var validateImports = function(data, fileLocation, options) {
                 }
                 else {
                     const directory = path.dirname(fileLocation);
-                    validatePath(fileLocation, directory, module, index + 1, errors);
+                    validatePath(fileLocation, directory, module, index + 1, errors, false);
+                }
+            }
+        }
+
+        // Find Exports.
+        if (options.isCore && line.indexOf("export") > -1) {
+            let regexTypeExport = new RegExp(`export .* from ['"](.*)['"];`, "g");
+            let match = regexTypeExport.exec(line);
+            if (match) {
+                module = match[1];
+
+                // Checks if line is about external module
+                if (options.externals) {
+                    for (let ext in options.externals) {
+                        if (line.indexOf(ext) > -1) {
+                            externalModule = ext;
+                            break;
+                        }
+                    }
+                }
+
+                // Check if path is correct internal.
+                if (externalModule) {
+                    const splitter = module.indexOf("/");
+                    const baseModule = module.substring(0, splitter);
+                    if (mapping[baseModule]) {
+                        const configName = mapping[baseModule];
+
+                        const directory = config[configName].computed.srcDirectory;
+                        module = module.substring(splitter);
+                        validatePath(fileLocation, directory, module, index + 1, errors, true);
+                    }
+                }
+                else {
+                    // Check Relative.
+                    if (!module.startsWith(".")) {
+                        errors.push(`Line ${index + 1} Export ${module} needs to be relative.`);
+                    }
+                    else {
+                        const directory = path.dirname(fileLocation);
+                        validatePath(fileLocation, directory, module, index + 1, errors, true);
+                    }
                 }
             }
         }

+ 2 - 1
Tools/Gulp/tasks/gulpTasks-importLint.js

@@ -27,7 +27,8 @@ var importLintLibrary = function(settings) {
     return gulp.src(settings.computed.tsGlob)
         .pipe(fxFilter)
         .pipe(validateImports({
-            externals: settings.build.umd.processDeclaration.classMap
+            externals: settings.build.umd.processDeclaration.classMap,
+            isCore: settings.isCore
         }));
 }