|  | 
| 11 | 11 | const path = require("path") | 
| 12 | 12 | const resolve = require("resolve") | 
| 13 | 13 | const getPackageJson = require("../util/get-package-json") | 
| 14 |  | -const getRequireTargets = require("../util/get-require-targets") | 
| 15 |  | -const getImportExportTargets = require("../util/get-import-export-targets") | 
|  | 14 | +const mergeVisitorsInPlace = require("../util/merge-visitors-in-place") | 
|  | 15 | +const visitImport = require("../util/visit-import") | 
|  | 16 | +const visitRequire = require("../util/visit-require") | 
| 16 | 17 | 
 | 
| 17 | 18 | const CORE_MODULES = new Set([ | 
| 18 | 19 |     "assert", | 
| @@ -98,49 +99,57 @@ module.exports = { | 
| 98 | 99 |         const ignoreIndirectDependencies = Boolean( | 
| 99 | 100 |             options.ignoreIndirectDependencies | 
| 100 | 101 |         ) | 
|  | 102 | +        const targets = [] | 
| 101 | 103 | 
 | 
| 102 |  | -        return { | 
| 103 |  | -            "Program:exit"(node) { | 
| 104 |  | -                const targets = [] | 
| 105 |  | -                    .concat( | 
| 106 |  | -                        getRequireTargets(context, true), | 
| 107 |  | -                        getImportExportTargets(context, node, { | 
| 108 |  | -                            includeCore: true, | 
| 109 |  | -                        }) | 
| 110 |  | -                    ) | 
| 111 |  | -                    .filter(t => CORE_MODULES.has(t.moduleName)) | 
|  | 104 | +        return [ | 
|  | 105 | +            visitImport(context, { includeCore: true }, importTargets => | 
|  | 106 | +                targets.push(...importTargets) | 
|  | 107 | +            ), | 
|  | 108 | +            visitRequire(context, { includeCore: true }, requireTargets => | 
|  | 109 | +                targets.push(...requireTargets) | 
|  | 110 | +            ), | 
|  | 111 | +            { | 
|  | 112 | +                "Program:exit"() { | 
|  | 113 | +                    for (const target of targets.filter(t => | 
|  | 114 | +                        CORE_MODULES.has(t.moduleName) | 
|  | 115 | +                    )) { | 
|  | 116 | +                        const name = target.moduleName | 
|  | 117 | +                        const allowed = | 
|  | 118 | +                            allow.indexOf(name) !== -1 || | 
|  | 119 | +                            (ignoreDirectDependencies && deps.has(name)) || | 
|  | 120 | +                            (ignoreIndirectDependencies && !deps.has(name)) | 
| 112 | 121 | 
 | 
| 113 |  | -                for (const target of targets) { | 
| 114 |  | -                    const name = target.moduleName | 
| 115 |  | -                    const allowed = | 
| 116 |  | -                        allow.indexOf(name) !== -1 || | 
| 117 |  | -                        (ignoreDirectDependencies && deps.has(name)) || | 
| 118 |  | -                        (ignoreIndirectDependencies && !deps.has(name)) | 
|  | 122 | +                        if (allowed) { | 
|  | 123 | +                            continue | 
|  | 124 | +                        } | 
| 119 | 125 | 
 | 
| 120 |  | -                    if (allowed) { | 
| 121 |  | -                        continue | 
| 122 |  | -                    } | 
|  | 126 | +                        const resolved = resolve.sync(name, { | 
|  | 127 | +                            basedir: dirPath, | 
|  | 128 | +                        }) | 
|  | 129 | +                        const isCore = resolved === name | 
| 123 | 130 | 
 | 
| 124 |  | -                    const resolved = resolve.sync(name, { basedir: dirPath }) | 
| 125 |  | -                    const isCore = resolved === name | 
|  | 131 | +                        if (isCore) { | 
|  | 132 | +                            continue | 
|  | 133 | +                        } | 
| 126 | 134 | 
 | 
| 127 |  | -                    if (isCore) { | 
| 128 |  | -                        continue | 
|  | 135 | +                        context.report({ | 
|  | 136 | +                            node: target.node, | 
|  | 137 | +                            loc: target.node.loc, | 
|  | 138 | +                            message: | 
|  | 139 | +                                "Unexpected import of third-party module '{{name}}'.", | 
|  | 140 | +                            data: { | 
|  | 141 | +                                name: path | 
|  | 142 | +                                    .relative(dirPath, resolved) | 
|  | 143 | +                                    .replace(BACK_SLASH, "/"), | 
|  | 144 | +                            }, | 
|  | 145 | +                        }) | 
| 129 | 146 |                     } | 
| 130 |  | - | 
| 131 |  | -                    context.report({ | 
| 132 |  | -                        node: target.node, | 
| 133 |  | -                        loc: target.node.loc, | 
| 134 |  | -                        message: | 
| 135 |  | -                            "Unexpected import of third-party module '{{name}}'.", | 
| 136 |  | -                        data: { | 
| 137 |  | -                            name: path | 
| 138 |  | -                                .relative(dirPath, resolved) | 
| 139 |  | -                                .replace(BACK_SLASH, "/"), | 
| 140 |  | -                        }, | 
| 141 |  | -                    }) | 
| 142 |  | -                } | 
|  | 147 | +                }, | 
| 143 | 148 |             }, | 
| 144 |  | -        } | 
|  | 149 | +        ].reduce( | 
|  | 150 | +            (mergedVisitor, thisVisitor) => | 
|  | 151 | +                mergeVisitorsInPlace(mergedVisitor, thisVisitor), | 
|  | 152 | +            {} | 
|  | 153 | +        ) | 
| 145 | 154 |     }, | 
| 146 | 155 | } | 
0 commit comments