Skip to content

Commit

Permalink
Merge pull request #2 from de-don/feature/specify-patterns
Browse files Browse the repository at this point in the history
Add option angularOrganizePatterns
  • Loading branch information
de-don authored Apr 2, 2023
2 parents 951178a + a63d0cd commit 26d8fd9
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 36 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@ After that, configure it in `.prettierrc` or `prettierrc.js`
]
}
```

If you want prettify decorators only in some directories, define patterns in `angularOrganizePatterns`:

```json
{
"plugins": ["prettier-plugin-organize-angular-decorators"],
"componentDecoratorOrder": [
"standalone",
"selector",
"templateUrl",
"styleUrls",
"changeDetection",
"providers"
],
"angularOrganizePatterns": [
"./src/shared/**/*",
"./src/modules/**/shared/**/*"
]
}
```
150 changes: 117 additions & 33 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-organize-angular-decorators",
"version": "0.0.1",
"version": "0.0.2",
"description": "Plugin to organize properties in Angular decorators",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -34,5 +34,8 @@
"ts-jest": "28.0.8",
"ts-node": "10.9.1",
"typescript": "4.7.4"
},
"dependencies": {
"minimatch": "7.4.3"
}
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ export const options: {
description: 'Order of attributes in Directive decorator.',
default: [{value: []}],
},
angularOrganizePatterns: {
type: 'string',
category: 'Global',
array: true,
description: 'Optional list of globe patterns to apply the plugin.',
default: [{value: []}],
},
};
9 changes: 9 additions & 0 deletions src/lib/check-file-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import minimatch from 'minimatch';

export function checkFilePath(filePath: string, patterns: string[]): boolean {
if (!patterns.length) {
return true;
}

return patterns.some(pattern => minimatch(filePath, pattern));
}
1 change: 1 addition & 0 deletions src/lib/types/plugin-options.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type PluginOptions = {
componentDecoratorOrder: string[];
directiveDecoratorOrder: string[];
angularOrganizePatterns: string[];
};
11 changes: 9 additions & 2 deletions src/lib/wrap-parser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import {Parser, ParserOptions} from 'prettier';
import {transformNode} from './transform-node';
import {PluginOptions} from './types/plugin-options.type';
import {checkFilePath} from './check-file-path';

export function wrapParser(parser: Parser): Parser {
return {
...parser,
parse: (text, parsers, options) => {
parse: (text, parsers, opts) => {
const options = opts as ParserOptions & PluginOptions;
const parsedNode = parser.parse(text, parsers, options);
return transformNode(parsedNode, options as ParserOptions & PluginOptions);

if (!checkFilePath(options.filepath, options.angularOrganizePatterns)) {
return parsedNode;
}

return transformNode(parsedNode, options);
},
};
}

0 comments on commit 26d8fd9

Please sign in to comment.