| 43 | }, |
| 44 | create: function (context) { |
| 45 | let processNode = node => { |
| 46 | if (!node.source || node.importKind === 'type') { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | let source = node.source.value.replace(/^[a-z-]+:/, ''); |
| 51 | if (source.startsWith('.') || builtinModules.includes(source)) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Split the import specifier on slashes. If it starts with an @ then it's |
| 56 | // a scoped package, otherwise just take the first part. |
| 57 | let parts = source.split('/'); |
| 58 | let pkgName = source.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]; |
| 59 | |
| 60 | // Search for a package.json starting from the current filename |
| 61 | let pkgPath = findUp.sync('package.json', {cwd: path.dirname(context.getFilename())}); |
| 62 | if (!pkgPath) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 67 | |
| 68 | // The only dev dependency should be spectrum-css. |
| 69 | if (exists(pkg.devDependencies, pkgName) && devDependencies.has(pkgName)) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (pkgName === 'vanilla-starter' || pkgName === 'tailwind-starter') { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | if ( |
| 78 | !exists(pkg.dependencies, pkgName) && |
| 79 | !exists(pkg.peerDependencies, pkgName) && |
| 80 | pkgName !== pkg.name |
| 81 | ) { |
| 82 | context.report({ |
| 83 | node, |
| 84 | message: `Missing dependency on ${pkgName}.`, |
| 85 | fix(fixer) { |
| 86 | // Attempt to find a package in the monorepo. If the dep is for an external library, |
| 87 | // then we cannot auto fix it because we don't know the version to add. |
| 88 | let depPath = __dirname + '/../packages/' + pkgName + '/package.json'; |
| 89 | if (!fs.existsSync(depPath)) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | let depPkg = JSON.parse(fs.readFileSync(depPath, 'utf8')); |
| 94 | let pkgVersion = substrings.some(v => depPkg.version.includes(v)) |
| 95 | ? depPkg.version |
| 96 | : `^${depPkg.version}`; |
| 97 | |
| 98 | if (pkgName === '@react-spectrum/provider') { |
| 99 | pkg.peerDependencies = insertObject(pkg.peerDependencies, pkgName, pkgVersion); |
| 100 | } else { |
| 101 | pkg.dependencies = insertObject(pkg.dependencies, pkgName, pkgVersion); |
| 102 | } |