Skip to content

Commit

Permalink
feat: ignore_comments flag
Browse files Browse the repository at this point in the history
adds a parameter to ignore imports inside comments

refs #16
  • Loading branch information
marabesi committed Dec 15, 2021
1 parent fc3d003 commit 50bcfcf
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 0.8.2

- Adds `ignore_comments` to ignore imports inside comments

### 0.8.1

- Removed duplicated code on matching unused imports
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ like:
"php.import.highlight": {
"color": "#EDF791",
"use_next_version": false
"ignore_comments": true
}
```

|Option|Type|Description|
|------|----|-----------|
|color|String| Uses the RGB color defined in this option to highlight the unused imports - helpful to match the color with your preferred theme.|
|use_next_version|Boolean| Opt-in to the latest changes to the extension without breaking current behavior. Note: if you are using this option and find anything wrong, please open an issue.|
|ignore_comments|Boolean|If set to true, does not take into account commented code - this option only works if **use_next_version** is set to true|

## Requirements

Expand Down
2 changes: 1 addition & 1 deletion mocha/unit/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('php import checker', () => {
path.join(__dirname + testFolderLocation + testCase.snippet)
);

const foundUnused = extractUnusedImports(phpFile.toString(), { use_next_version: true });
const foundUnused = extractUnusedImports(phpFile.toString(), { use_next_version: true, ignore_comments: true });

assert.equal(foundUnused.length, testCase.unused);
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "php-import-checker",
"displayName": "PHP import checker",
"description": "Know when you are importing a class and is not using it in the file, easy and fast, inspired by PHPStorm",
"version": "0.8.1",
"version": "0.8.2",
"publisher": "marabesi",
"icon": "arrows.png",
"engines": {
Expand Down
1 change: 1 addition & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const unusedNamespaceDecorationType = window.createTextEditorDecorationTy
export type PhpImportCheckerConfiguration = {
color?: string;
use_next_version?: Boolean;
ignore_comments?: Boolean;
}

export function setupConfiguration(configuration: PhpImportCheckerConfiguration): TextEditorDecorationType {
Expand Down
2 changes: 1 addition & 1 deletion src/core/extractUnusedImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { newExtractor } from './nextVersionExtractUnusedImports';

export function extractUnusedImports(text: string, configuration: PhpImportCheckerConfiguration) {
if (configuration.use_next_version) {
return newExtractor(text);
return newExtractor(text, configuration);
}

const regEx = /^\ {0,3}use (?:(?:function|const) )?(.*);/mg;
Expand Down
12 changes: 9 additions & 3 deletions src/core/nextVersionExtractUnusedImports.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { PhpImportCheckerConfiguration } from '../configuration';
import { Builder } from '../parser/Builder';
import { PhpUseItem } from '../parser/types/Nodes';

export const classUsed = (className: string) => new RegExp('\\b' + className + '\\b', 'g');

export function newExtractor(text: string) {
let matches = [];
export function newExtractor(originalText: string, configuration: PhpImportCheckerConfiguration) {
const matches = [];
let text: string = originalText;

try {
const builder = new Builder(text);
if (configuration.ignore_comments) {
text = text.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/, '');
}

const builder = new Builder(originalText);
const ast:PhpUseItem[] = builder.build().namespaces.normalizeUseStatements();

const allUsedClasses = ast.map(node => node.name).join(',');
Expand Down
8 changes: 4 additions & 4 deletions test/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export const dataProvider = [

export const dataProviderNextVersion = [
{ snippet: 'snippet4.php', unused: 5 },
// { snippet: 'snippet5.php', unused: 5 }, this will require a new options to ignore comments
{ snippet: 'snippet5.php', unused: 5 },
// { snippet: 'snippet7.php', unused: 1 }, this will require a new options to ignore comments
// { snippet: 'snippet13.php', unused: 1 }, this will require a new options to ignore comments
{ snippet: 'snippet14.php', unused: 3 },
// { snippet: 'snippet23.php', unused: 3 }, not supported yet group declaration #16
// { snippet: 'snippet23.php', unused: 3 }, not supported yet group declaration #16 - might be impossible to support that with regex?
// { snippet: 'snippet35.php', unused: 1 }, might be impossible to support that with regex?
{ snippet: 'snippet24.php', unused: 1 },
{ snippet: 'snippet25.php', unused: 2 },
// { snippet: 'snippet35.php', unused: 1 },
{ snippet: 'snippet42.php', unused: 0 }
{ snippet: 'snippet42.php', unused: 2 }
];
2 changes: 1 addition & 1 deletion test/integration/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ suite('php-import-checker extension behavior', () => {
const document = await vscode.workspace.openTextDocument(uri);
const editor = await vscode.window.showTextDocument(document);

const found = myExtension.findMatch(editor, editor.document.getText(), { use_next_version: true });
const found = myExtension.findMatch(editor, editor.document.getText(), { use_next_version: true, ignore_comments: true });

assert.equal(testCase.unused, found.length);
});
Expand Down

0 comments on commit 50bcfcf

Please sign in to comment.