-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(linter): fix workspace-rule naming with flat config (#20782)
- Loading branch information
Showing
11 changed files
with
184 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
129 changes: 129 additions & 0 deletions
129
...plugin/src/migrations/update-17-2-6-rename-workspace-rules/rename-workspace-rules.spec.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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { Tree, readJson, writeJson } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports'; | ||
import { WORKSPACE_PLUGIN_DIR } from '../../constants'; | ||
import update from './rename-workspace-rules'; | ||
|
||
const rule1Name = 'test-rule'; | ||
const rule2Name = 'my-rule'; | ||
|
||
describe('update-17-2-6-rename-workspace-rules', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
|
||
const { lintWorkspaceRuleGenerator } = require('@nx/' + | ||
'eslint/src/generators/workspace-rule/workspace-rule'); | ||
|
||
lintWorkspaceRuleGenerator(tree, { | ||
name: rule1Name, | ||
}); | ||
lintWorkspaceRuleGenerator(tree, { | ||
name: rule2Name, | ||
}); | ||
|
||
jest.mock(WORKSPACE_PLUGIN_DIR, () => ({ | ||
rules: { | ||
[rule1Name]: {}, | ||
[rule2Name]: {}, | ||
}, | ||
})); | ||
}); | ||
|
||
it('should replace rules in config files', async () => { | ||
writeJson(tree, '.eslintrc.json', { | ||
plugins: ['@nx'], | ||
rules: { | ||
[`@nx/workspace/${rule1Name}`]: 'error', | ||
[`@nx/workspace/${rule2Name}`]: 'error', | ||
}, | ||
}); | ||
|
||
update(tree); | ||
|
||
expect(Object.keys(readJson(tree, '.eslintrc.json').rules)).toEqual([ | ||
'@nx/workspace-test-rule', | ||
'@nx/workspace-my-rule', | ||
]); | ||
}); | ||
|
||
it('should replace rules in random js files', async () => { | ||
tree.write( | ||
'custom.js', | ||
` | ||
export default { | ||
plugins: ['@nx'], | ||
rules: { | ||
"@nx/workspace/${rule1Name}": 'error', | ||
"@nx/workspace/${rule2Name}": 'error', | ||
}, | ||
} | ||
` | ||
); | ||
|
||
update(tree); | ||
|
||
expect(tree.read('custom.js', 'utf-8')).toContain( | ||
`@nx/workspace-test-rule` | ||
); | ||
expect(tree.read('custom.js', 'utf-8')).toContain(`@nx/workspace-my-rule`); | ||
expect(tree.read('custom.js', 'utf-8')).not.toContain( | ||
`@nx/workspace/test-rule` | ||
); | ||
expect(tree.read('custom.js', 'utf-8')).not.toContain( | ||
`@nx/workspace/my-rule` | ||
); | ||
}); | ||
|
||
it('should replace rules in comments', async () => { | ||
tree.write( | ||
'custom.js', | ||
`import { getSourceNodes } from '@nx/workspace/src/utilities/typescript'; | ||
// eslint-disable-next-line @nx/workspace/${rule1Name} | ||
import { something } from 'somewhere'; | ||
/* eslint-disable @nx/workspace/${rule2Name} */ | ||
// something that should remain the same @nx/workspace/unknown-rule | ||
/* eslint-enable @nx/workspace/${rule2Name} */ | ||
` | ||
); | ||
|
||
update(tree); | ||
|
||
expect(tree.read('custom.js', 'utf-8')).toMatchInlineSnapshot(` | ||
"import { getSourceNodes } from '@nx/workspace/src/utilities/typescript'; | ||
// eslint-disable-next-line @nx/workspace-test-rule | ||
import { something } from 'somewhere'; | ||
/* eslint-disable @nx/workspace-my-rule */ | ||
// something that should remain the same @nx/workspace/unknown-rule | ||
/* eslint-enable @nx/workspace-my-rule */ | ||
" | ||
`); | ||
}); | ||
|
||
it('should not replace unknown rules', async () => { | ||
tree.write( | ||
'custom.js', | ||
` | ||
export default { | ||
plugins: ['@nx'], | ||
rules: { | ||
"@nx/workspace/random-rule": 'error', | ||
}, | ||
} | ||
` | ||
); | ||
|
||
update(tree); | ||
|
||
expect(tree.read('custom.js', 'utf-8')).not.toContain( | ||
`@nx/workspace-random-rule` | ||
); | ||
expect(tree.read('custom.js', 'utf-8')).toContain( | ||
`@nx/workspace/random-rule` | ||
); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
...lint-plugin/src/migrations/update-17-2-6-rename-workspace-rules/rename-workspace-rules.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Tree, formatFiles, visitNotIgnoredFiles } from '@nx/devkit'; | ||
import { isBinaryPath } from '@nx/devkit/src/utils/binary-extensions'; | ||
import { WORKSPACE_PLUGIN_DIR, WORKSPACE_RULES_PATH } from '../../constants'; | ||
|
||
export default async function renameWorkspaceRule(tree: Tree): Promise<void> { | ||
if (!tree.exists(WORKSPACE_RULES_PATH)) { | ||
return; | ||
} | ||
let ruleNames: string[] = []; | ||
try { | ||
ruleNames = Object.keys(require(WORKSPACE_PLUGIN_DIR).rules); | ||
} catch (e) { | ||
return; | ||
} | ||
|
||
visitNotIgnoredFiles(tree, '.', (path) => { | ||
if (isBinaryPath(path)) { | ||
return; | ||
} | ||
|
||
let contents = tree.read(path, 'utf-8') as string; | ||
ruleNames.forEach((ruleName) => { | ||
contents = contents.replace( | ||
new RegExp(`@nx/workspace/${ruleName}`, 'g'), | ||
`@nx/workspace-${ruleName}` | ||
); | ||
}); | ||
tree.write(path, contents); | ||
}); | ||
|
||
await formatFiles(tree); | ||
} |
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
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
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
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
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
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