Skip to content

Commit 8b03a05

Browse files
committed
refactor: replace yaml package for reducing the extension size
1 parent 8641e36 commit 8b03a05

File tree

9 files changed

+60
-69
lines changed

9 files changed

+60
-69
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@
143143
"workspaceContains:**/phpstan.neon.dist"
144144
],
145145
"dependencies": {
146-
"yaml": "^2.1.1"
146+
"js-yaml": "^4.1.0"
147147
},
148148
"devDependencies": {
149149
"@changesets/changelog-github": "^0.4.6",
150150
"@changesets/cli": "^2.24.4",
151151
"@trivago/prettier-plugin-sort-imports": "^3.3.0",
152+
"@types/js-yaml": "^4.0.5",
152153
"@types/node": "^18.7.18",
153154
"@types/vscode": "^1.50.0",
154155
"@typescript-eslint/eslint-plugin": "^5.37.0",
@@ -167,4 +168,4 @@
167168
"vscode": "^1.50.0"
168169
},
169170
"icon": "assets/logo.png"
170-
}
171+
}

pnpm-lock.yaml

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/findPHPStanConfigPath.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ export default async function findPHPStanConfigPath(ext: Ext) {
88
? isAbsolute(settings.configPath)
99
? settings.configPath
1010
: join(rootPath, settings.configPath)
11-
: await find({
12-
cwd: ext.cwd,
13-
binPath: ext.settings.path,
14-
});
11+
: await find(ext.cwd);
1512

1613
if (!configPath) throw new Error(`Config path not found.`);
1714
outputChannel.appendLine(`# Config path: ${configPath}`);

src/commands/loadPHPStanConfig.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Ext } from "../extension";
2-
import { parsePHPStanConfig } from "../utils/phpstan";
2+
import { parsePHPStanConfigFile } from "../utils/phpstan";
3+
import { dirname, join, normalize } from "path";
34

45
export default async function loadPHPStanConfig(ext: Ext) {
56
if (!ext.store.phpstan.configPath) throw new Error("Config path is required");
6-
const config = await parsePHPStanConfig(ext.store.phpstan.configPath, {
7-
cwd: ext.cwd,
8-
binPath: ext.settings.path,
7+
const config = await parsePHPStanConfigFile(ext.store.phpstan.configPath, {
8+
currentWorkingDirectory: ext.cwd,
9+
rootDir: normalize(dirname(join(ext.cwd, ext.settings.path))),
910
});
1011
ext.outputChannel.appendLine(`# Config:\n${JSON.stringify(config, null, 2)}`);
1112
return config;

src/utils/neon.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { readFile } from "fs/promises";
2+
import { load } from "js-yaml";
3+
4+
export function resolveNeon(contents: string, env: Record<string, string>) {
5+
return contents.replace(/(?:%(\w+)%)/g, (_, name) => env[name] ?? "");
6+
}
7+
8+
export async function parseNeonFile<T = unknown>(
9+
path: string,
10+
env: Record<string, string> = {}
11+
): Promise<T> {
12+
const contents = (await readFile(path)).toString();
13+
const yaml = resolveNeon(contents.replace(/\t/g, " "), env);
14+
return load(yaml) as Promise<T>;
15+
}

src/utils/path.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isAbsolute, join, normalize } from "path";
2+
13
/**
24
* @link https://github.com/microsoft/vscode/blob/84a3473d/src/vs/workbench/contrib/terminal/common/terminalEnvironment.ts#L227
35
*/
@@ -8,3 +10,8 @@ export function sanitizeFsPath(path: string) {
810
return path;
911
}
1012
}
13+
14+
export function resolvePath(path: string, cwd: string): string {
15+
if (!isAbsolute(path)) path = join(cwd, path);
16+
return normalize(path);
17+
}

src/utils/phpstan.ts

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { checkFile } from "./fs";
2-
import { parseYamlFile } from "./yaml";
3-
import { isAbsolute, join, normalize } from "path";
2+
import { parseNeonFile } from "./neon";
3+
import { resolvePath } from "./path";
4+
import { join } from "path";
45

56
export type PHPStanAnalyseResult = {
67
totals: {
@@ -25,74 +26,53 @@ export type PHPStanConfig = {
2526
paths?: string[];
2627
excludes_analyse?: string[];
2728
fileExtensions?: string[];
29+
bootstrapFiles?: string[];
2830
};
2931
};
3032

31-
export type PHPStanSettings = {
32-
binPath: string;
33-
cwd: string;
33+
export type PHPStanConfigEnv = {
34+
rootDir: string;
35+
currentWorkingDirectory: string;
3436
};
3537

36-
function resolveConfigItemValue(
37-
value: string,
38-
settings: PHPStanSettings
39-
): string {
40-
const rootDir = join(settings.cwd, settings.binPath);
41-
return value
42-
.replace(/%currentWorkingDirectory%/g, settings.cwd)
43-
.replace(/%rootDir%/g, rootDir);
44-
}
45-
46-
function resolveConfigItemPath(
47-
value: string,
48-
settings: PHPStanSettings
49-
): string {
50-
value = resolveConfigItemValue(value, settings);
51-
if (!isAbsolute(value)) value = join(settings.cwd, value);
52-
return normalize(value);
53-
}
54-
5538
export function parsePHPStanAnalyseResult(
5639
stdout: string
5740
): PHPStanAnalyseResult {
5841
return JSON.parse(stdout);
5942
}
6043

61-
export async function parsePHPStanConfig(
44+
export async function parsePHPStanConfigFile(
6245
path: string,
63-
settings: PHPStanSettings,
64-
resolve = true
46+
env: PHPStanConfigEnv
6547
): Promise<PHPStanConfig> {
66-
const config = await parseYamlFile<PHPStanConfig>(path);
67-
return resolve ? resolvePHPStanConfig(config, settings) : config;
48+
const config = await parseNeonFile<PHPStanConfig>(path, env);
49+
return normalizePHPStanConfig(config, env.currentWorkingDirectory);
6850
}
6951

7052
export async function findPHPStanConfigPath(
71-
settings: PHPStanSettings
53+
cwd: string
7254
): Promise<string | undefined> {
73-
const dir = settings.cwd;
7455
const baseNames = ["phpstan.neon", "phpstan.neon.dist"];
7556
for (const basename of baseNames) {
76-
const path = join(dir, basename);
57+
const path = join(cwd, basename);
7758
if (await checkFile(path)) {
7859
return path;
7960
}
8061
}
8162
}
8263

83-
export function resolvePHPStanConfig(
64+
export function normalizePHPStanConfig(
8465
config: PHPStanConfig,
85-
settings: PHPStanSettings
66+
cwd: string
8667
): PHPStanConfig {
8768
config = Object.assign({}, config);
8869
config.parameters = Object.assign({}, config.parameters);
8970
const params = config.parameters;
90-
if (Array.isArray(params.paths))
91-
params.paths = params.paths.map((v) => resolveConfigItemPath(v, settings));
92-
if (Array.isArray(params.excludes_analyse))
93-
params.excludes_analyse = params.excludes_analyse.map((v) =>
94-
resolveConfigItemPath(v, settings)
95-
);
71+
const resolve = (v: string) => resolvePath(v, cwd);
72+
73+
params.paths = params.paths?.map(resolve);
74+
params.excludes_analyse = params.excludes_analyse?.map(resolve);
75+
params.bootstrapFiles = params.bootstrapFiles?.map(resolve);
9676

9777
return config;
9878
}

src/utils/yaml.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default defineConfig({
2525
],
2626
output: {
2727
manualChunks: {
28-
vendor: ["yaml"],
28+
vendor: ["js-yaml"],
2929
},
3030
},
3131
},

0 commit comments

Comments
 (0)