Skip to content

Commit

Permalink
feat: add eslint rule about use build-in import node module (#48)
Browse files Browse the repository at this point in the history
* feat: add eslint rule about use build-in import node module

* feat: update pnpm-lock.yaml

* fix: fix plugin-copy.ts module by eslint config

---------

Co-authored-by: winches <329487092@qq.com>
  • Loading branch information
kent99821 and winchesHe committed Apr 23, 2024
1 parent ce57f30 commit 2971a49
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 16 deletions.
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"unused-imports",
"sort-keys-fix",
"sort-destructure-keys",
"prettier"
"prettier",
"unicorn"
],
"parserOptions": {
"sourceType": "module",
Expand Down Expand Up @@ -98,6 +99,8 @@
"caseInsensitive": true
}
}
]
],
"unicorn/better-regex": "error",
"unicorn/prefer-node-protocol": "error"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-sort-destructure-keys": "1.5.0",
"eslint-plugin-sort-keys-fix": "1.1.2",
"eslint-plugin-unicorn": "52.0.0",
"eslint-plugin-unused-imports": "3.0.0",
"husky": "8.0.3",
"lint-staged": "14.0.1",
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-copy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Options} from 'tsup';

import {copyFileSync} from 'fs';
import {copyFileSync} from 'node:fs';

import {resolver} from 'src/constants/path';

Expand Down
139 changes: 139 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/constants/path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {resolve} from 'path';
import {fileURLToPath} from 'url';
import {resolve} from 'node:path';
import {fileURLToPath} from 'node:url';

export const ROOT = process.cwd();
export const resolver = (path: string) => resolve(ROOT, path);
Expand Down
2 changes: 1 addition & 1 deletion src/constants/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const DOCS_PROVIDER_SETUP = 'https://nextui.org/docs/guide/installation#p

// Record the required content of tailwind.config file
export const tailwindRequired = {
checkPluginsRegex: /nextui(([\w\W]+)?)/,
checkPluginsRegex: /nextui(([\W\w]+)?)/,
content: './node_modules/@nextui-org/theme/dist/**/*.{js,ts,jsx,tsx}',
darkMode: 'darkMode: "class"',
importContent: (isTypescript = false) => {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/exec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {AppendKeyValue} from './type';

import {type CommonExecOptions, execSync} from 'child_process';
import {type CommonExecOptions, execSync} from 'node:child_process';

import {Logger} from './logger';
import {omit} from './utils';
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Readable} from 'stream';
import {pipeline} from 'stream/promises';
import {Readable} from 'node:stream';
import {pipeline} from 'node:stream/promises';

import retry from 'async-retry';
import tar from 'tar';
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function fixProvider(appPath: string, options: FixProvider) {
}

function wrapWithNextUIProvider(content: string) {
const returnRegex = /return\s*\(([\s\S]*?)\);/g;
const returnRegex = /return\s*\(([\S\s]*?)\);/g;
const wrappedCode = content.replace(returnRegex, (_, p1) => {
return `return (
<NextUIProvider>
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param str
*/
export function getMatchImport(str: string) {
const importRegexAll = /import {?\s*([\w\W]+?)\s*}? from ['"](.+)['"]/g;
const importRegexAll = /import {?\s*([\W\w]+?)\s*}? from ["'](.+)["']/g;

const matchAll = str.match(importRegexAll) ?? [];
const result: string[][] = [];
Expand All @@ -16,7 +16,7 @@ export function getMatchImport(str: string) {
return result.length ? result : [];

function matchImport(itemImport: string) {
const importRegex = /import {?\s*([\w\W]+?)\s*}? from ['"](.+)['"]/;
const importRegex = /import {?\s*([\W\w]+?)\s*}? from ["'](.+)["']/;
const match = itemImport.match(importRegex) ?? [];

return [match[1] ?? '', match[2] ?? ''];
Expand All @@ -37,7 +37,7 @@ export function getMatchArray(key: string, target: string) {
target
.match(mixinReg)?.[1]
?.split(/,\n/)
.map((i) => i.trim().replace(/[`'"]/g, ''))
.map((i) => i.trim().replace(/["'`]/g, ''))
.filter(Boolean) ?? []
);

Expand Down Expand Up @@ -77,7 +77,7 @@ export function replaceMatchArray(
: targetArray.splice(
insertIndex + 1,
0,
` ${key}: [${value.map((v) => v.replace(/['"`]/g, ''))}],`
` ${key}: [${value.map((v) => v.replace(/["'`]/g, ''))}],`
);

return targetArray.join('\n');
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function removeTailwind(
tailwindContent = replaceMatchArray('plugins', tailwindContent, pluginsMatch);

// Remove the import nextui content
tailwindContent = tailwindContent.replace(/(const|var|let|import)[\w\W]+?nextui.*?;\n/, '');
tailwindContent = tailwindContent.replace(/(const|var|let|import)[\W\w]+?nextui.*?;\n/, '');
}

// If there are already have all nextui content include then don't need to remove the content
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {exec} from 'child_process';
import {exec} from 'node:child_process';
import {existsSync, readFileSync, writeFileSync} from 'node:fs';

import retry from 'async-retry';
Expand Down

0 comments on commit 2971a49

Please sign in to comment.