Initial OSS commit
This commit is contained in:
commit
4a7303f40a
391 changed files with 245738 additions and 0 deletions
131
devEnv/eslint/rules/no-relative-imports.js
Normal file
131
devEnv/eslint/rules/no-relative-imports.js
Normal file
|
@ -0,0 +1,131 @@
|
|||
const path = require('path')
|
||||
const minimatch = require('minimatch')
|
||||
|
||||
// @todo find and acknowledge the original source of this (it was a github gist)
|
||||
// @todo this rule is clearly ignoring a bunch of files that do have parent
|
||||
// relative paths but don't get caught.
|
||||
|
||||
const replace = {
|
||||
meta: {
|
||||
type: 'layout',
|
||||
fixable: 'code',
|
||||
schema: [
|
||||
{
|
||||
type: 'object',
|
||||
properties: {
|
||||
ignore: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
method: {
|
||||
type: 'string',
|
||||
enum: ['all', 'only-parent'],
|
||||
},
|
||||
aliases: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
properties: {
|
||||
name: {
|
||||
type: 'string',
|
||||
},
|
||||
path: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
required: ['name', 'path'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['aliases'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
],
|
||||
messages: {
|
||||
'can-replace':
|
||||
'Relative import found. Run autofix to replace these relative imports!',
|
||||
'cannot-replace':
|
||||
'Relative import found. No alias has been defined which matches this import path',
|
||||
},
|
||||
},
|
||||
create: (context) => {
|
||||
return {
|
||||
ImportDeclaration: (node) => {
|
||||
const config = getConfiguration(context)
|
||||
|
||||
if (
|
||||
config.ignorePatterns.some((pattern) =>
|
||||
minimatch(context.getFilename(), pattern),
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
evaluateImport(node, config, context)
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
function evaluateImport(node, config, context) {
|
||||
const {
|
||||
source: {value, range, loc},
|
||||
} = node
|
||||
if (value.startsWith('./') && config.replaceMethod === 'only-parent') {
|
||||
return
|
||||
} else if (!value.startsWith('../')) {
|
||||
return
|
||||
}
|
||||
|
||||
let canFix = false
|
||||
const currentFileDirectory = path.dirname(context.getFilename())
|
||||
for (const alias of config.replaceAliases) {
|
||||
const {replaceDir, replaceWith} = alias
|
||||
const fullImportPath = path.resolve(currentFileDirectory, value)
|
||||
|
||||
if (fullImportPath.startsWith(replaceDir)) {
|
||||
canFix = true
|
||||
|
||||
context.report({
|
||||
messageId: 'can-replace',
|
||||
loc,
|
||||
fix: (fixer) =>
|
||||
fixer.replaceTextRange(
|
||||
range,
|
||||
`'${fullImportPath.replace(replaceDir, replaceWith)}'`,
|
||||
),
|
||||
})
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (!canFix) {
|
||||
context.report({
|
||||
messageId: 'cannot-replace',
|
||||
loc,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function getConfiguration(context) {
|
||||
const options = {
|
||||
ignore: [],
|
||||
method: 'only-parent',
|
||||
...context.options[0],
|
||||
}
|
||||
|
||||
return {
|
||||
ignorePatterns: options.ignore,
|
||||
replaceMethod: options.method,
|
||||
replaceAliases: options.aliases.map((alias) => ({
|
||||
replaceDir: alias.path,
|
||||
replaceWith: alias.name,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = replace
|
2
devEnv/getAliasesFromTSConfig.d.ts
vendored
Normal file
2
devEnv/getAliasesFromTSConfig.d.ts
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
export function getAliasesFromTsConfigForWebpack(): Record<string, string>
|
||||
export function getAliasesFromTsConfigForJest(): Record<string, string>
|
58
devEnv/getAliasesFromTSConfig.js
Normal file
58
devEnv/getAliasesFromTSConfig.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
const path = require('path')
|
||||
|
||||
const monorepoRoot = path.resolve(__dirname, '../')
|
||||
|
||||
function getAliasesFromTsConfigForWebpack() {
|
||||
const tsConfigPaths = require('../tsconfig.base.json').compilerOptions.paths
|
||||
|
||||
const aliases = {}
|
||||
|
||||
for (let [key, value] of Object.entries(tsConfigPaths)) {
|
||||
if (key.match(/\*$/)) {
|
||||
key = key.replace(/\/\*$/, '')
|
||||
} else {
|
||||
key = key + '$'
|
||||
}
|
||||
aliases[key] = path.join(monorepoRoot, value[0].replace(/\/\*$/, ''))
|
||||
}
|
||||
|
||||
return aliases
|
||||
}
|
||||
|
||||
module.exports.getAliasesFromTsConfigForWebpack = getAliasesFromTsConfigForWebpack
|
||||
|
||||
function getAliasesFromTsConfigForESLint() {
|
||||
const tsConfigPaths = require('../tsconfig.base.json').compilerOptions.paths
|
||||
|
||||
const aliases = []
|
||||
|
||||
for (const [key, value] of Object.entries(tsConfigPaths)) {
|
||||
aliases.push({
|
||||
name: key.replace(/\/\*$/, ''),
|
||||
path: value[0].replace(/\/\*$/, ''),
|
||||
})
|
||||
}
|
||||
|
||||
return aliases
|
||||
}
|
||||
|
||||
module.exports.getAliasesFromTsConfigForESLint = getAliasesFromTsConfigForESLint
|
||||
|
||||
function getAliasesFromTsConfigForJest() {
|
||||
const tsConfigPaths = require('../tsconfig.base.json').compilerOptions.paths
|
||||
|
||||
const aliases = {}
|
||||
|
||||
for (let [key, value] of Object.entries(tsConfigPaths)) {
|
||||
if (key.match(/\/\*$/)) {
|
||||
key = key.replace(/\/\*$/, '/(.*)')
|
||||
} else {
|
||||
key = key + '$'
|
||||
}
|
||||
aliases[key] = path.join('<rootDir>', value[0].replace(/\/\*$/, '/$1'))
|
||||
}
|
||||
|
||||
return aliases
|
||||
}
|
||||
|
||||
module.exports.getAliasesFromTsConfigForJest = getAliasesFromTsConfigForJest
|
14
devEnv/typecheck-all-projects/tsconfig.all.json
Normal file
14
devEnv/typecheck-all-projects/tsconfig.all.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"rootDir": "../.."
|
||||
},
|
||||
"files": [],
|
||||
"references": [
|
||||
{"path": "../../theatre"},
|
||||
{"path": "../../packages/dataverse"},
|
||||
{"path": "../../packages/playground"},
|
||||
{"path": "../../packages/plugin-r3f"}
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue