Skip to content

Commit 762fc99

Browse files
ctf0ctf0
ctf0
authored and
ctf0
committed
042
1 parent be09990 commit 762fc99

File tree

7 files changed

+58
-39
lines changed

7 files changed

+58
-39
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,7 @@ All notable changes to the "php-namespace-resolver" extension will be documented
7474
- if use statement already exists, no changes will be made
7575
- support importing/expanding class FQN when its called with a partial FQN ex.`Rules\Password` + `use Illuminate\Validation\Rules;`
7676
- you will get an error msg if a use statement already exists with a similar class name of what u r trying to import ex.`use Illuminate\Facade\Password;` + importing ex.`Rules\Password`
77+
78+
## 0.4.2
79+
80+
- fix not parsing of other types than class

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ NS_EXTENSION_PROVIDER.insertNamespace() // insert namespace in current active fi
3535
- make sure to run `composer dump` first & fix any reported issues.
3636
- run `PHP Namespace Resolver: Check for namespaces project wide`
3737
- note that commented out FQN will show up in the problems panel as well, the cmnd lists all the namespaces that are unknown regardless of its position
38+
- also you might get positive-negative results because of nested namespaces, which will need more work to check if the namespace correspond to an actual file path or not

package.json

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "php-namespace-resolver",
33
"displayName": "PHP Namespace Resolver",
44
"description": "Import and expand php namespaces",
5-
"version": "0.4.1",
5+
"version": "0.4.2",
66
"publisher": "ctf0",
77
"author": "ctf0",
88
"repository": "https://github.com/ctf0/PHP-Namespace-Resolver",
@@ -21,11 +21,6 @@
2121
],
2222
"activationEvents": [
2323
"workspaceContains:composer.json",
24-
"namespaceResolver.import",
25-
"namespaceResolver.importAll",
26-
"namespaceResolver.expand",
27-
"namespaceResolver.sort",
28-
"namespaceResolver.generateNamespace",
2924
"onStartupFinished"
3025
],
3126
"main": "./out/main",
@@ -225,7 +220,20 @@
225220
},
226221
"minItems": 1,
227222
"uniqueItems": true,
228-
"markdownDescription": "exclude directories from namespace use call search\n\n(.gitignore contents are already excluded)"
223+
"markdownDescription": "exclude directories from namespace import search\n\n(.gitignore contents are already excluded)"
224+
},
225+
"namespaceResolver.checkForNamespaces.rg.excludeFiles": {
226+
"type": "array",
227+
"default": [
228+
"_ide_helper.php",
229+
".phpstorm.meta.php"
230+
],
231+
"items": {
232+
"type": "string"
233+
},
234+
"minItems": 1,
235+
"uniqueItems": true,
236+
"markdownDescription": "exclude files from namespace import search\n\n(.gitignore contents are already excluded)"
229237
}
230238
}
231239
}
@@ -238,19 +246,19 @@
238246
"watch": "npm run esbuild"
239247
},
240248
"devDependencies": {
241-
"@types/fs-extra": "^9.0.13",
242-
"@types/node": "^18.11.4",
249+
"@types/fs-extra": "^11.0.1",
250+
"@types/node": "^18.11.18",
243251
"@types/vscode": "^1.68.0",
244-
"esbuild": "^0.15.13",
245-
"typescript": "^4.6.3"
252+
"esbuild": "^0.17.3",
253+
"typescript": "^4.9.4"
246254
},
247255
"dependencies": {
248256
"escape-string-regexp": "^5.0.0",
249257
"execa": "^6.1.0",
250258
"find-up": "^6.3.0",
251259
"fs-extra": "^11.1.0",
252260
"lodash.groupby": "^4.6.0",
253-
"node-natural-sort": "^0.8.6",
254-
"php-parser": "^3.1.2"
261+
"natural-orderby": "^3.0.2",
262+
"php-parser": "^3.1.3"
255263
}
256264
}

src/NamespaceCheck.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
* add the unknown statements to the diagnose panel
99
*/
1010

11-
import escapeStringRegexp from 'escape-string-regexp';
1211
import { execa } from 'execa';
12+
import groupBy from 'lodash.groupby';
1313
import * as vscode from 'vscode';
1414
import Resolver from './Resolver';
15-
const _groupBy = require('lodash.groupby');
1615

1716
export default async function checkForNamespaces(resolver: Resolver, createDiagnosticCollection: vscode.DiagnosticCollection) {
1817
if (resolver.CWD) {
@@ -85,24 +84,32 @@ async function getAutoloadFileData(resolver: Resolver): Promise<any> {
8584

8685
async function searchAllFilesForImports(resolver: Resolver): Promise<any> {
8786
const rgCommand = resolver.config('checkForNamespaces.rg.command');
88-
const rgExcludeDirs = resolver.config('checkForNamespaces.rg.excludeDirs').map((item) => escapeStringRegexp(item)).join(',');
89-
90-
if (!rgCommand || !rgExcludeDirs) {
91-
throw new Error('config required : rgCommand,rgExcludeDirs');
87+
const rgExcludeDirs = resolver
88+
.config('checkForNamespaces.rg.excludeDirs')
89+
.join(',');
90+
const rgExcludeFiles = resolver
91+
.config('checkForNamespaces.rg.excludeFiles')
92+
.join(',');
93+
94+
if (!rgCommand || !rgExcludeDirs || !rgExcludeFiles) {
95+
throw new Error('config required : rgCommand,excludeDirs,excludeFiles');
9296
}
9397

94-
const { stdout } = await execa(rgCommand, [
95-
"'((namespace|use) )?\\\\?(\\w+\\\\)+\\w+(?=[ ;:\\(])'",
98+
const args = [
99+
'\'((namespace|use) )?\\\\?(\\w+\\\\)+\\w+(?=[ ;:\\(])\'',
96100
'--mmap',
97101
'--pcre2',
98102
'--no-messages',
99103
'--line-number',
100104
'--only-matching',
101-
"--glob='!*blade.php'",
105+
'--glob=\'**/*.php\'',
106+
'--glob=\'!*.blade.php\'',
102107
`--glob='!{${rgExcludeDirs}}'`,
103-
"--glob='**/*.php'",
104-
"./",
105-
], {
108+
`--glob='!{${rgExcludeFiles}}'`,
109+
'./',
110+
];
111+
112+
const { stdout } = await execa(rgCommand, args, {
106113
cwd : resolver.CWD,
107114
shell : vscode.env.shell,
108115
});
@@ -164,7 +171,7 @@ async function searchAllFilesForImports(resolver: Resolver): Promise<any> {
164171
}
165172

166173
function findNonFoundNamespaces(appNamespaces, importsFiles) {
167-
importsFiles = _groupBy(importsFiles, 'import');
174+
importsFiles = groupBy(importsFiles, 'import');
168175
const list: any = [];
169176

170177
for (const namespace of Object.keys(importsFiles)) {

src/Parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export function buildClassASTFromContent(content: string) {
1616
try {
1717
const AST = Parser.parseCode(content, '*.php');
1818

19-
const _tag: any = AST.tokens?.find((item: any) => item[0] == "T_OPEN_TAG");
20-
const _declare: any = AST.children?.find((item: any) => item.kind == "declare");
19+
const _tag: any = AST.tokens?.find((item: any) => item[0] == 'T_OPEN_TAG');
20+
const _declare: any = AST.children?.find((item: any) => item.kind == 'declare');
2121
const _namespace: any = AST.children?.find((item: any) => item.kind == 'namespace');
2222
const _use: any = (_namespace || AST).children?.filter((item: any) => item.kind == 'usegroup');
23-
const _class: any = (_namespace || AST).children?.find((item: any) => item.kind == 'class');
24-
const _trait: any = _class?.body?.find((item: any) => item.kind == "traituse")?.traits;
23+
const _class: any = (_namespace || AST).children?.find((item: any) => ['class', 'enum', 'interface', 'trait'].includes(item.kind));
24+
const _trait: any = _class?.body?.find((item: any) => item.kind == 'traituse')?.traits;
2525

2626
return {
2727
_openTag: {

src/Resolver.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execaCommand } from 'execa';
22
import { findUp } from 'find-up';
33
import fs from 'fs-extra';
4-
import naturalSort from 'node-natural-sort';
4+
import { compare } from 'natural-orderby';
55
import path from 'node:path';
66
import * as vscode from 'vscode';
77
import BUILT_IN_CLASSES_FB from './classes';
@@ -77,7 +77,7 @@ export default class Resolver {
7777
if (_class?.implements !== null) {
7878
phpClasses = phpClasses.concat(
7979
_class.implements
80-
.filter((item) => item.resolution == "uqn")
80+
.filter((item) => item.resolution == 'uqn')
8181
.map((item) => item.name),
8282
);
8383
}
@@ -95,7 +95,7 @@ export default class Resolver {
9595
const methods = _class.body?.filter((item) => item.kind === 'method' && item.arguments.length);
9696

9797
return methods?.map((item) => item.arguments
98-
.filter((arg) => arg.type.kind == 'name' && arg.type.resolution == "uqn")
98+
.filter((arg) => arg.type.kind == 'name' && arg.type.resolution == 'uqn')
9999
.map((arg) => arg.type.name));
100100
}
101101

@@ -426,9 +426,8 @@ export default class Resolver {
426426
};
427427

428428
if (this.config('sort.natural')) {
429-
const natsort = naturalSort({
430-
caseSensitive : true,
431-
order : alpha ? 'ASC' : 'DESC',
429+
const natsort = compare({
430+
order: alpha ? 'asc' : 'desc',
432431
});
433432

434433
sortFunction = (a, b) => natsort(a.text, b.text);
@@ -537,7 +536,7 @@ export default class Resolver {
537536
}
538537

539538
resolving(selection) {
540-
if (typeof selection === "string") {
539+
if (typeof selection === 'string') {
541540
return selection;
542541
}
543542

@@ -571,7 +570,7 @@ export default class Resolver {
571570
/**
572571
* @param uri: ?vscode.Uri
573572
*/
574-
async generateNamespace(returnDontInsert = false, uri = null) {
573+
async generateNamespace(returnDontInsert = false, uri?: vscode.Uri) {
575574
if (!returnDontInsert) {
576575
this.setEditorAndAST();
577576
}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function activate(context) {
2727
);
2828

2929
return {
30-
getNamespace(uri = null) {
30+
getNamespace(uri?: vscode.Uri) {
3131
return resolver.generateNamespace(true, uri);
3232
},
3333
insertNamespace() {

0 commit comments

Comments
 (0)