Skip to content

Commit

Permalink
feat(eslint): support generate mjs/cjs eslint config #488 (#494)
Browse files Browse the repository at this point in the history
Co-authored-by: Andy <andy.gui@gempoll.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people committed May 10, 2024
1 parent 87baffc commit 670688c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ AutoImport({
// eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
eslintrc: {
enabled: false, // Default `false`
// provide path ending with `.mjs` or `.cjs` to generate the file with the respective format
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
},
Expand Down
9 changes: 8 additions & 1 deletion src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,15 @@ ${dts}`.trim()}\n`
)
}
if (eslintrc.enabled && eslintrc.filepath) {
const filepath = eslintrc.filepath
promises.push(
generateESLint().then((content) => {
generateESLint().then(async (content) => {
if (filepath.endsWith('.cjs')) {

Check failure on line 199 in src/core/ctx.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary { after 'if' condition
content = `module.exports = ${content}`
}
else if (filepath.endsWith('.mjs') || filepath.endsWith('.js')) {

Check failure on line 202 in src/core/ctx.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary { after 'if' condition
content = `export default ${content}`
}
content = `${content}\n`
if (content.trim() !== lastESLint?.trim()) {
lastESLint = content
Expand Down
2 changes: 1 addition & 1 deletion src/core/eslintrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function generateESLintConfigs(
eslintrc: ESLintrc,
globals: Record<string, ESLintGlobalsPropValue> = {},
) {
const eslintConfigs = { globals }
const eslintConfigs: any = { globals }

imports
.map(i => i.as ?? i.name)
Expand Down

0 comments on commit 670688c

Please sign in to comment.