Skip to content

Commit

Permalink
style: use consistent-type-imports (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Jan 2, 2024
1 parent b8d32a4 commit 62a3def
Show file tree
Hide file tree
Showing 67 changed files with 159 additions and 136 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
{
"mode": "auto"
}
]
],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"source.fixAll.eslint": true
},
"typescript.preferences.preferTypeOnlyAutoImports": true
}
2 changes: 1 addition & 1 deletion .vscode/transform.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"import * as t from '@babel/types';",
"import * as m from '@codemod/matchers';",
// https://github.com/microsoft/vscode/issues/37570#issuecomment-672199343
"import { Transform } from '${TM_DIRECTORY/.*src(\\/[^\\/]*)(\\/[^\\/]*)?(\\/[^\\/]*)?(\\/[^\\/]*)?(\\/[^\\/]*)?(\\/[^\\/]*)?/${1:+../}${2:+../}${3:+../}${4:+../}${5:+../}${6:+../}/}ast-utils';",
"import type { Transform } from '${TM_DIRECTORY/.*src(\\/[^\\/]*)(\\/[^\\/]*)?(\\/[^\\/]*)?(\\/[^\\/]*)?(\\/[^\\/]*)?(\\/[^\\/]*)?/${1:+../}${2:+../}${3:+../}${4:+../}${5:+../}${6:+../}/}ast-utils';",
"",
"export default {",
" name: '${TM_FILENAME_BASE/[A-Z]/-${0:/downcase}/g}',",
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Choose any of these launch configurations:
- `Launch playground`: runs a dev server in the background and opens the playground in your browser.

<https://github.com/j4k0xb/webcrack/assets/55899582/8d6509c6-7ec2-43c8-8d1b-8aac5b279e45>

- `Deobfuscate tmp file`: runs the CLI locally.
1. Create the file `packages/webcrack/tmp/test.js` and paste your code
2. The output will be saved in `tmp/webcrack-out`
Expand Down
20 changes: 12 additions & 8 deletions packages/config-eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
* @type {import("eslint").Linter.Config}
*/
module.exports = {
parser: "@typescript-eslint/parser",
parser: '@typescript-eslint/parser',
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"prettier",
'eslint:recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'prettier',
],
ignorePatterns: [".eslintrc.cjs", "dist", "*.js", "*.snap"],
plugins: ["@typescript-eslint"],
ignorePatterns: ['.eslintrc.cjs', 'dist', '*.js', '*.snap'],
plugins: ['@typescript-eslint'],
parserOptions: {
sourceType: "module",
ecmaVersion: "latest",
sourceType: 'module',
ecmaVersion: 'latest',
project: true,
},
rules: {
'@typescript-eslint/consistent-type-exports': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
},
};
5 changes: 3 additions & 2 deletions packages/webcrack/src/ast-utils/generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import babelGenerate, { GeneratorOptions } from '@babel/generator';
import * as t from '@babel/types';
import type { GeneratorOptions } from '@babel/generator';
import babelGenerate from '@babel/generator';
import type * as t from '@babel/types';

const defaultOptions: GeneratorOptions = { jsescOption: { minimal: true } };

Expand Down
3 changes: 2 additions & 1 deletion packages/webcrack/src/ast-utils/inline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import traverse, { Binding, NodePath } from '@babel/traverse';
import type { Binding, NodePath } from '@babel/traverse';
import traverse from '@babel/traverse';
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { getPropName } from '.';
Expand Down
2 changes: 1 addition & 1 deletion packages/webcrack/src/ast-utils/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Binding, NodePath } from '@babel/traverse';
import type { Binding, NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import * as m from '@codemod/matchers';

Expand Down
2 changes: 1 addition & 1 deletion packages/webcrack/src/ast-utils/matchers.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Matcher } from '@codemod/matchers';
import type { Matcher } from '@codemod/matchers';

type MatcherType<T> = T extends Matcher<infer U> ? U : T;

Expand Down
3 changes: 2 additions & 1 deletion packages/webcrack/src/ast-utils/rename.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import traverse, { Binding, NodePath } from '@babel/traverse';
import type { Binding, NodePath } from '@babel/traverse';
import traverse from '@babel/traverse';
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { codePreview } from './generator';
Expand Down
10 changes: 3 additions & 7 deletions packages/webcrack/src/ast-utils/transform.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import traverse, {
Node,
TraverseOptions,
Visitor,
visitors,
} from '@babel/traverse';
import type { Node, TraverseOptions, Visitor } from '@babel/traverse';
import traverse, { visitors } from '@babel/traverse';
import debug from 'debug';

const logger = debug('webcrack:transforms');
Expand Down Expand Up @@ -85,7 +81,7 @@ export function mergeTransforms(options: {
return visitors.merge(
options.transforms.flatMap((t) => t.visitor?.() ?? []),
);
}
},
};
}

Expand Down
6 changes: 3 additions & 3 deletions packages/webcrack/src/deobfuscate/array-rotator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { callExpression } from '@codemod/matchers';
import { constMemberExpression, findParent, infiniteLoop } from '../ast-utils';
import { StringArray } from './string-array';
import type { StringArray } from './string-array';

export type ArrayRotator = NodePath<t.ExpressionStatement>;

Expand Down
6 changes: 3 additions & 3 deletions packages/webcrack/src/deobfuscate/control-flow-object.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Binding, NodePath } from '@babel/traverse';
import type { Binding, NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import { FunctionExpression } from '@babel/types';
import type { FunctionExpression } from '@babel/types';
import * as m from '@codemod/matchers';
import type { Transform } from '../ast-utils';
import {
Transform,
applyTransform,
constKey,
constMemberExpression,
Expand Down
3 changes: 2 additions & 1 deletion packages/webcrack/src/deobfuscate/control-flow-switch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform, constMemberExpression, infiniteLoop } from '../ast-utils';
import type { Transform } from '../ast-utils';
import { constMemberExpression, infiniteLoop } from '../ast-utils';

export default {
name: 'control-flow-switch',
Expand Down
5 changes: 3 additions & 2 deletions packages/webcrack/src/deobfuscate/dead-code.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NodePath, Scope } from '@babel/traverse';
import type { NodePath, Scope } from '@babel/traverse';
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform, renameFast } from '../ast-utils';
import type { Transform } from '../ast-utils';
import { renameFast } from '../ast-utils';

export default {
name: 'dead-code',
Expand Down
8 changes: 2 additions & 6 deletions packages/webcrack/src/deobfuscate/debug-protection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as m from '@codemod/matchers';
import { ifStatement } from '@codemod/matchers';
import {
Transform,
constMemberExpression,
findParent,
iife,
} from '../ast-utils';
import type { Transform } from '../ast-utils';
import { constMemberExpression, findParent, iife } from '../ast-utils';

// https://github.com/javascript-obfuscator/javascript-obfuscator/blob/d7f73935557b2cd15a2f7cd0b01017d9cddbd015/src/custom-code-helpers/debug-protection/templates/debug-protection-function-interval/DebugProtectionFunctionIntervalTemplate.ts

Expand Down
6 changes: 3 additions & 3 deletions packages/webcrack/src/deobfuscate/decoder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expression } from '@babel/template';
import { NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { findParent, inlineVariable, renameFast } from '../ast-utils';
import { StringArray } from './string-array';
import type { StringArray } from './string-array';

/**
* A function that is called with >= 1 numeric/string arguments
Expand Down
10 changes: 3 additions & 7 deletions packages/webcrack/src/deobfuscate/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import debug from 'debug';
import type { AsyncTransform } from '../ast-utils';
import {
AsyncTransform,
applyTransform,
applyTransformAsync,
applyTransforms,
Expand All @@ -15,12 +15,8 @@ import inlineDecodedStrings from './inline-decoded-strings';
import inlineDecoderWrappers from './inline-decoder-wrappers';
import inlineObjectProps from './inline-object-props';
import { findStringArray } from './string-array';
import {
Sandbox,
VMDecoder,
createBrowserSandbox,
createNodeSandbox,
} from './vm';
import type { Sandbox } from './vm';
import { VMDecoder, createBrowserSandbox, createNodeSandbox } from './vm';

export { createBrowserSandbox, createNodeSandbox, type Sandbox };

Expand Down
4 changes: 2 additions & 2 deletions packages/webcrack/src/deobfuscate/inline-decoded-strings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from '@babel/types';
import { AsyncTransform } from '../ast-utils';
import { VMDecoder } from './vm';
import type { AsyncTransform } from '../ast-utils';
import type { VMDecoder } from './vm';

/**
* Replaces calls to decoder functions with the decoded string.
Expand Down
11 changes: 4 additions & 7 deletions packages/webcrack/src/deobfuscate/inline-decoder-wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import {
Transform,
inlineFunctionAliases,
inlineVariableAliases,
} from '../ast-utils';
import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';
import type { Transform } from '../ast-utils';
import { inlineFunctionAliases, inlineVariableAliases } from '../ast-utils';

/**
* Replaces all references to `var alias = decode;` with `decode`
Expand Down
2 changes: 1 addition & 1 deletion packages/webcrack/src/deobfuscate/inline-object-props.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as m from '@codemod/matchers';
import type { Transform } from '../ast-utils';
import {
Transform,
constKey,
constMemberExpression,
inlineObjectProperties,
Expand Down
5 changes: 3 additions & 2 deletions packages/webcrack/src/deobfuscate/merge-object-assignments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Binding } from '@babel/traverse';
import type { Binding } from '@babel/traverse';
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform, constObjectProperty, safeLiteral } from '../ast-utils';
import type { Transform } from '../ast-utils';
import { constObjectProperty, safeLiteral } from '../ast-utils';

/**
* Merges object assignments into the object expression.
Expand Down
6 changes: 3 additions & 3 deletions packages/webcrack/src/deobfuscate/self-defending.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';
import * as m from '@codemod/matchers';
import type { Transform } from '../ast-utils';
import {
Transform,
constMemberExpression,
emptyIife,
falseMatcher,
Expand Down
5 changes: 3 additions & 2 deletions packages/webcrack/src/deobfuscate/string-array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import traverse, { NodePath } from '@babel/traverse';
import * as t from '@babel/types';
import type { NodePath } from '@babel/traverse';
import traverse from '@babel/traverse';
import type * as t from '@babel/types';
import * as m from '@codemod/matchers';
import {
inlineArrayElements,
Expand Down
2 changes: 1 addition & 1 deletion packages/webcrack/src/deobfuscate/var-functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform } from '../ast-utils';
import type { Transform } from '../ast-utils';

// Unsafe because variables may be used before they are declared, but functions are hoisted
// Example: `console.log(a); var a = function() {}` logs `undefined`
Expand Down
10 changes: 5 additions & 5 deletions packages/webcrack/src/deobfuscate/vm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NodePath } from '@babel/traverse';
import { CallExpression } from '@babel/types';
import type { NodePath } from '@babel/traverse';
import type { CallExpression } from '@babel/types';
import debug from 'debug';
import { generate } from '../ast-utils';
import { ArrayRotator } from './array-rotator';
import { Decoder } from './decoder';
import { StringArray } from './string-array';
import type { ArrayRotator } from './array-rotator';
import type { Decoder } from './decoder';
import type { StringArray } from './string-array';

export type Sandbox = (code: string) => Promise<unknown>;

Expand Down
17 changes: 10 additions & 7 deletions packages/webcrack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ParseResult, parse } from '@babel/parser';
import * as t from '@babel/types';
import type { ParseResult } from '@babel/parser';
import { parse } from '@babel/parser';
import type * as t from '@babel/types';
import type Matchers from '@codemod/matchers';
import * as m from '@codemod/matchers';
import debug from 'debug';
import { join, normalize } from 'node:path';
Expand All @@ -9,8 +11,8 @@ import {
applyTransforms,
generate,
} from './ast-utils';
import type { Sandbox } from './deobfuscate';
import deobfuscate, {
Sandbox,
createBrowserSandbox,
createNodeSandbox,
} from './deobfuscate';
Expand All @@ -28,11 +30,14 @@ import {
sequence,
splitVariableDeclarations,
} from './unminify/transforms';
import { Bundle, unpackAST } from './unpack';
import type { Bundle } from './unpack';
import { unpackAST } from './unpack';
import { isBrowser } from './utils/platform';

export { type Sandbox } from './deobfuscate';

type Matchers = typeof m;

export interface WebcrackResult {
code: string;
bundle: Bundle | undefined;
Expand Down Expand Up @@ -80,9 +85,7 @@ export interface Options {
* })
* ```
*/
mappings?: (
m: typeof import('@codemod/matchers'),
) => Record<string, m.Matcher<unknown>>;
mappings?: (m: Matchers) => Record<string, m.Matcher<unknown>>;
/**
* Function that executes a code expression and returns the result (typically from the obfuscator).
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'babel-plugin-minify-mangle-names' {
import { Visitor, traverse } from '@babel/traverse';
import * as t from '@babel/types';
import type { Visitor, traverse } from '@babel/traverse';
import type * as t from '@babel/types';

export default function mangle(babel: Babel): {
visitor: Visitor;
Expand Down
3 changes: 2 additions & 1 deletion packages/webcrack/src/transforms/jsx-new.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform, codePreview, constMemberExpression } from '../ast-utils';
import type { Transform } from '../ast-utils';
import { codePreview, constMemberExpression } from '../ast-utils';

const DEFAULT_PRAGMA_CANDIDATES = [
'jsx',
Expand Down
3 changes: 2 additions & 1 deletion packages/webcrack/src/transforms/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform, codePreview, constMemberExpression } from '../ast-utils';
import type { Transform } from '../ast-utils';
import { codePreview, constMemberExpression } from '../ast-utils';

export default {
name: 'jsx',
Expand Down
6 changes: 4 additions & 2 deletions packages/webcrack/src/transforms/mangle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { statement } from '@babel/template';
import traverse, { NodePath, Visitor, visitors } from '@babel/traverse';
import type { Visitor } from '@babel/traverse';
import traverse, { NodePath, visitors } from '@babel/traverse';
import * as t from '@babel/types';
import mangle from 'babel-plugin-minify-mangle-names';
import { Transform, safeLiteral } from '../ast-utils';
import type { Transform } from '../ast-utils';
import { safeLiteral } from '../ast-utils';

// See https://github.com/j4k0xb/webcrack/issues/41 and https://github.com/babel/minify/issues/1023
const fixDefaultParamError: Visitor = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform, isTemporaryVariable } from '../../ast-utils';
import type { Transform } from '../../ast-utils';
import { isTemporaryVariable } from '../../ast-utils';

export default {
name: 'logical-assignments',
Expand Down
Loading

0 comments on commit 62a3def

Please sign in to comment.