Skip to content

Commit

Permalink
feat: restructure dep and bundling process, use ast-types-x and jscod…
Browse files Browse the repository at this point in the history
…eshift-x
  • Loading branch information
pionxzh committed Aug 10, 2024
1 parent 3b4294f commit 59e38d5
Show file tree
Hide file tree
Showing 32 changed files with 850 additions and 2,513 deletions.
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,19 @@
"@vitest/ui": "^1.5.0",
"eslint": "^8.57.0",
"esno": "^0.17.0",
"globby": "^11.1.0",
"prettier": "^2.8.8",
"taze": "^0.13.6",
"turbo": "^1.13.2",
"typescript": "^5.5.3",
"typescript": "^5.5.4",
"vitest": "^1.5.0"
},
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]",
"@clack/[email protected]": "patches/@[email protected]",
"@clack/[email protected]": "patches/@[email protected]",
"[email protected]": "patches/[email protected]"
"@clack/[email protected]": "patches/@[email protected]"
}
},
"resolutions": {
"ast-types": "^0.16.1"
"ast-types": "npm:[email protected]",
"recast": "npm:[email protected]"
}
}
11 changes: 6 additions & 5 deletions packages/ast-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@
"lint": "eslint src --max-warnings=0",
"lint:fix": "eslint src --fix --max-warnings=0"
},
"dependencies": {
"@babel/helper-validator-identifier": "^7.24.7",
"ast-types": "npm:[email protected]",
"jscodeshift": "npm:[email protected]"
},
"devDependencies": {
"@babel/helper-validator-identifier": "^7.22.20",
"@babel/types": "^7.24.0",
"@types/jscodeshift": "^0.11.11",
"@wakaru/ds": "workspace:*",
"@wakaru/shared": "workspace:*",
"@wakaru/test-utils": "workspace:*",
"ast-types": "^0.16.1",
"jscodeshift": "^0.15.2",
"typescript": "^5.5.3"
"typescript": "^5.5.4"
}
}
30 changes: 28 additions & 2 deletions packages/ast-utils/src/identifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-expect-error no types
import { isIdentifierName, isKeyword, isStrictReservedWord } from '@babel/helper-validator-identifier'
import { toIdentifier } from '@babel/types'
import { isIdentifierChar, isIdentifierName, isKeyword, isStrictReservedWord } from '@babel/helper-validator-identifier'
import { isDeclared } from './scope'
import type { Scope } from 'ast-types/lib/scope'

Expand Down Expand Up @@ -74,3 +73,30 @@ function getUniqueName(name: string, scope: Scope | null = null, existedNames: s
}
return `${name}_${i}`
}

/**
* Fork from https://github.com/babel/babel/blob/main/packages/babel-types/src/converters/toIdentifier.ts
*/
function toIdentifier(input: string): string {
input = `${input}`

// replace all non-valid identifiers with dashes
let name = ''
for (const c of input) {
name += isIdentifierChar(c.codePointAt(0)) ? c : '-'
}

// remove all dashes and numbers from start of name
name = name.replace(/^[-0-9]+/, '')

// camel case
name = name.replace(/[-\s]+(.)?/g, (_match, c) => {
return c ? c.toUpperCase() : ''
})

if (!isValidIdentifier(name)) {
name = `_${name}`
}

return name || '_'
}
36 changes: 3 additions & 33 deletions packages/ast-utils/src/imports.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
import { MultiMap } from '@wakaru/ds'
import { isTopLevel } from './isTopLevel'
import type { NodePath } from 'ast-types/lib/node-path'
import type { ASTNode, CallExpression, Collection, ImportDeclaration, JSCodeshift, StringLiteral, VariableDeclaration, VariableDeclarator } from 'jscodeshift'

type Source = string
type Imported = string
type Local = string

export interface DefaultImport {
type: 'default'
name: string
source: Source
}

export interface NamespaceImport {
type: 'namespace'
name: string
source: Source
}

export interface NamedImport {
type: 'named'
name: string
local: Local
source: Source
}

export interface BareImport {
type: 'bare'
source: Source
}

export type ImportInfo = DefaultImport | NamespaceImport | NamedImport | BareImport
import type { BareImport, DefaultImport, ImportInfo, Imported, Local, NamedImport, NamespaceImport, Source } from '@wakaru/shared/imports'
import type { ASTNode, ASTPath, CallExpression, Collection, ImportDeclaration, JSCodeshift, StringLiteral, VariableDeclaration, VariableDeclarator } from 'jscodeshift'

export class ImportManager {
private importSourceOrder = new Set<Source>()
Expand All @@ -40,7 +10,7 @@ export class ImportManager {
namedImports = new Map<Source, MultiMap<Imported, Local>>()
bareImports = new Set<Source>()

private importDecls: Array<NodePath<ImportDeclaration>> = []
private importDecls: Array<ASTPath<ImportDeclaration>> = []

get importMap() {
/**
Expand Down
3 changes: 1 addition & 2 deletions packages/ast-utils/src/matchers/isIIFE.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { fromPaths } from 'jscodeshift/src/Collection'
import type { ASTNode, ASTPath, CallExpression, Collection, ExpressionStatement, JSCodeshift, Statement } from 'jscodeshift'

/**
Expand Down Expand Up @@ -80,5 +79,5 @@ export function findIIFEs(
.map(path => (path.get('expression', 'argument') as ASTPath<CallExpression>))
.paths()

return fromPaths([...collection1, ...collection2])
return j([...collection1, ...collection2])
}
2 changes: 1 addition & 1 deletion packages/ast-utils/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function findReferences(

if (!path.scope) return false

let scope = path.scope
let scope: Scope | null = path.scope
// we don't use `scope.lookup` here to avoid
// traversing the whole scope chain
while (scope && scope !== targetScope) {
Expand Down
11 changes: 11 additions & 0 deletions packages/ast-utils/src/renameFunctionParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export function renameFunctionParameters(j: JSCodeshift, node: FunctionDeclarati
const newName = parameters[index]
if (!newName || oldName === newName) return

/**
* Skip if the old name is declared multiple times
* it means the parameter is shadowed by another variable
* in the same scope
*/
const bindings = targetScope.getBindings()
if (bindings[oldName]?.length > 1) {
param.name = newName
return
}

renameIdentifier(j, targetScope, oldName, newName)
}
})
Expand Down
7 changes: 3 additions & 4 deletions packages/ast-utils/src/scope.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { fromPaths } from 'jscodeshift/src/Collection'
import { mergeComments } from './comments'
import { findReferences } from './reference'
import type { Scope } from 'ast-types/lib/scope'
import type { ASTPath, Collection, Identifier, ImportDeclaration, JSCodeshift, Statement, VariableDeclaration } from 'jscodeshift'

export function isDeclared(scope: Scope, name: string) {
export function isDeclared(scope: Scope | null, name: string) {
while (scope) {
if (scope.declares(name)) return true
scope = scope.parent
Expand All @@ -17,8 +16,8 @@ export function findDeclaration(scope: Scope, name: string): ASTPath<Identifier>
return scope.lookup(name)?.getBindings()[name]?.[0]
}

export function findDeclarations(scope: Scope, name: string): Collection<Identifier> {
return fromPaths(scope.lookup(name)?.getBindings()[name] ?? [])
export function findDeclarations(j: JSCodeshift, scope: Scope, name: string): Collection<Identifier> {
return j(scope.lookup(name)?.getBindings()[name] ?? [])
}

export function removeDeclarationIfUnused(j: JSCodeshift, path: ASTPath, name: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ast-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ImportInfo } from './imports'
import type { ImportInfo } from '@wakaru/shared/imports'

export type ModuleMapping = Record<number | string, string>

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@wakaru/unpacker": "workspace:*",
"fs-extra": "^11.2.0",
"globby": "^11.1.0",
"picocolors": "^1.0.0",
"picocolors": "^1.0.1",
"poolifier": "^3.1.30",
"yargs": "^17.7.2"
},
Expand All @@ -42,6 +42,6 @@
"@wakaru/shared": "workspace:*",
"@wakaru/test-utils": "workspace:*",
"tsup": "^8.0.2",
"typescript": "^5.5.3"
"typescript": "^5.5.4"
}
}
2 changes: 0 additions & 2 deletions packages/cli/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default defineConfig({
},
minify: true,
noExternal: [
'jscodeshift',
'ast-types',
'@clack/core', // patched
'@clack/prompts', // patched
],
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"postcss": "^8.4.38",
"rollup-plugin-node-polyfills": "^0.2.1",
"tailwindcss": "^3.4.3",
"typescript": "^5.5.3",
"typescript": "^5.5.4",
"vite": "^5.2.10",
"vitest": "^1.5.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"postcss": "^8.4.38",
"rollup-plugin-node-polyfills": "^0.2.1",
"tailwindcss": "^3.4.3",
"typescript": "^5.5.3",
"typescript": "^5.5.4",
"vite": "^5.2.6",
"vue-tsc": "^2.0.7"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/atoms/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { atom, getDefaultStore } from 'jotai/vanilla'
import { KEY_FILE_PREFIX, KEY_MODULE_MAPPING, KEY_MODULE_META } from '../const'
import { unminify } from '../worker'
import { enabledRuleIdsAtom, prettifyRules } from './rule'
import type { ImportInfo } from '@wakaru/ast-utils/imports'
import type { ImportInfo } from '@wakaru/shared/imports'
import type { ModuleMapping, ModuleMeta } from '@wakaru/shared/types'

export type ModuleId = number | string
Expand Down
13 changes: 7 additions & 6 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"sideEffects": false,
"exports": {
"./array": "./src/array.ts",
"./imports": "./src/imports.ts",
"./jscodeshift": "./src/jscodeshift.ts",
"./rule": "./src/rule.ts",
"./runner": "./src/runner.ts",
Expand All @@ -29,14 +30,14 @@
"lint:fix": "eslint src --fix --max-warnings=0"
},
"dependencies": {
"@babel/parser": "^7.24.1",
"pathe": "^1.1.2"
"@babel/parser": "^7.25.0",
"ast-types": "npm:[email protected]",
"jscodeshift": "npm:[email protected]",
"pathe": "^1.1.2",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/jscodeshift": "^0.11.11",
"ast-types": "^0.16.1",
"jscodeshift": "^0.15.2",
"typescript": "^5.3.3",
"zod": "^3.22.4"
"typescript": "^5.5.4"
}
}
5 changes: 3 additions & 2 deletions packages/shared/src/imports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Source = string
type Local = string
export type Source = string
export type Imported = string
export type Local = string

export interface DefaultImport {
type: 'default'
Expand Down
4 changes: 2 additions & 2 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"devDependencies": {
"@types/jscodeshift": "^0.11.11",
"@wakaru/shared": "workspace:*",
"jscodeshift": "^0.15.2",
"typescript": "^5.5.3"
"jscodeshift": "npm:[email protected]",
"typescript": "^5.5.4"
},
"publishConfig": {
"exports": {
Expand Down
26 changes: 13 additions & 13 deletions packages/unminify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@
"lint:fix": "eslint src --fix --max-warnings=0"
},
"dependencies": {
"@babel/core": "^7.23.9",
"@babel/helper-validator-identifier": "^7.22.20",
"@babel/preset-env": "^7.23.9",
"@babel/types": "^7.23.9",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@babel/helper-validator-identifier": "^7.24.7",
"@babel/parser": "^7.25.0",
"ast-types": "npm:[email protected]",
"jscodeshift": "npm:[email protected]",
"lebab": "^3.2.4",
"picocolors": "^1.0.0",
"pathe": "^1.1.2",
"prettier": "^2.8.8",
"zod": "^3.23.4"
"zod": "^3.23.8"
},
"devDependencies": {
"@swc/core": "^1.6.13",
"@babel/core": "^7.24.9",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@swc/core": "^1.7.2",
"@types/jscodeshift": "^0.11.11",
"@types/prettier": "^2.7.3",
"@types/yargs": "^17.0.32",
"@wakaru/ast-utils": "workspace:*",
"@wakaru/ds": "workspace:*",
"@wakaru/shared": "workspace:*",
"@wakaru/test-utils": "workspace:*",
"ast-types": "^0.16.1",
"jscodeshift": "^0.15.2",
"rollup": "^4.18.1",
"picocolors": "^1.0.1",
"rollup": "^4.19.1",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-swc3": "^0.11.2",
"typescript": "^5.5.3"
"typescript": "^5.5.4"
},
"publishConfig": {
"exports": {
Expand Down
Loading

0 comments on commit 59e38d5

Please sign in to comment.