-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4632b24
commit d156ff8
Showing
2 changed files
with
18 additions
and
54 deletions.
There are no files selected for viewing
65 changes: 15 additions & 50 deletions
65
packages/linter/src/migrations/update-14-1-9/add-swc-deps-if-needed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,17 @@ | ||
import { | ||
formatFiles, | ||
installPackagesTask, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
|
||
export default async function addSwcNodeIfNeeded(tree: Tree) { | ||
try { | ||
const existingJestConfigPath = normalizePath( | ||
'tools/eslint-rules/jest.config.js' | ||
import { addDependenciesToPackageJson, formatFiles, Tree } from '@nrwl/devkit'; | ||
import { swcCoreVersion, swcNodeVersion } from 'nx/src/utils/versions'; | ||
import { WORKSPACE_PLUGIN_DIR } from '../../generators/workspace-rules-project/workspace-rules-project'; | ||
|
||
export default async function addSwcNodeIfNeeded(tree: Tree) { | ||
try { | ||
if (tree.exists(WORKSPACE_PLUGIN_DIR)) { | ||
addDependenciesToPackageJson( | ||
tree, | ||
{}, | ||
{ '@swc-node/register': swcNodeVersion, '@swc/core': swcCoreVersion } | ||
); | ||
|
||
// Add extra config to the jest.config.js file to allow ESLint 8 exports mapping to work with jest | ||
addPropertyToJestConfig(tree, existingJestConfigPath, 'moduleNameMapper', { | ||
'@eslint/eslintrc': '@eslint/eslintrc/dist/eslintrc-universal.cjs', | ||
}); | ||
|
||
visitNotIgnoredFiles(tree, 'tools/eslint-rules', (path) => { | ||
if (!path.endsWith('.ts')) { | ||
return; | ||
} | ||
const fileContents = tree.read(path).toString('utf-8'); | ||
const fileAst = tsquery.ast(fileContents); | ||
const isESLintRuleFile = | ||
tsquery( | ||
fileAst, | ||
'PropertyAccessExpression[expression.escapedText=ESLintUtils][name.escapedText=RuleCreator]' | ||
).length > 0; | ||
if (!isESLintRuleFile) { | ||
return; | ||
} | ||
const categoryPropertyAssignmentNode = tsquery( | ||
fileAst, | ||
'PropertyAssignment[name.escapedText=meta] PropertyAssignment[name.escapedText=docs] PropertyAssignment[name.escapedText=category]' | ||
)[0]; | ||
if (!categoryPropertyAssignmentNode) { | ||
return; | ||
} | ||
let end = categoryPropertyAssignmentNode.getEnd(); | ||
if (fileContents.substring(end, end + 1) === ',') { | ||
end++; | ||
} | ||
const updatedContents = | ||
fileContents.slice(0, categoryPropertyAssignmentNode.getFullStart()) + | ||
fileContents.slice(end); | ||
tree.write(path, updatedContents); | ||
}); | ||
|
||
await formatFiles(tree); | ||
} catch {} | ||
} | ||
|
||
return; | ||
} | ||
} catch {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters