forked from stencil-community/stencil-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(own-props-must-be-private): add new rule
- Loading branch information
Showing
9 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# own-props-must-be-private | ||
|
||
Ensures that all own class properties are private. | ||
|
||
## Config | ||
|
||
No config is needed | ||
|
||
## Usage | ||
|
||
```json | ||
{ "@d0whc3r/stencil/own-props-must-be-private": "error" } | ||
``` | ||
|
||
> Fix included |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Rule } from 'eslint'; | ||
import { isPrivate, stencilComponentContext, stencilDecorators } from '../utils'; | ||
|
||
const rule: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: 'This rule catches own class attributes marked as public.', | ||
category: 'Possible Errors', | ||
recommended: true | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
fixable: 'code' | ||
}, | ||
|
||
create(context): Rule.RuleListener { | ||
const stencil = stencilComponentContext(); | ||
|
||
const parserServices = context.parserServices; | ||
return { | ||
...stencil.rules, | ||
'ClassProperty': (node: any) => { | ||
if (!stencil.isComponent()) { | ||
return; | ||
} | ||
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node); | ||
const stencilDecorator = originalNode.decorators && originalNode.decorators.some( | ||
(dec: any) => stencilDecorators.includes(dec.expression.expression.escapedText)); | ||
if (!stencilDecorator && !isPrivate(originalNode)) { | ||
const text = String(originalNode.getFullText()); | ||
context.report({ | ||
node: node, | ||
message: `Own class properties cannot be public`, | ||
fix(fixer) { | ||
const varName = node.key.name; | ||
const result = text.replace('public ', '').replace(varName, `private ${varName}`); | ||
return fixer.replaceText(node, result); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
export default rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/lib/rules/own-props-must-be-private/own-props-must-be-private.good.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@Component({ tag: 'sample-tag' }) | ||
export class SampleTag { | ||
private internalProp: string; | ||
@OwnDecorator() private internalDecoratedProp: string; | ||
|
||
@Prop() readonly test?: string; | ||
@Prop({ mutable: true }) testMutable?: string; | ||
|
||
render() { | ||
return (<div>test</div>); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/lib/rules/own-props-must-be-private/own-props-must-be-private.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import rule from '../../../../src/rules/own-props-must-be-private'; | ||
import { ruleTester } from '../rule-tester'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
describe('stencil rules', () => { | ||
const files = { | ||
good: path.resolve(__dirname, 'own-props-must-be-private.good.tsx'), | ||
wrong: path.resolve(__dirname, 'own-props-must-be-private.wrong.tsx') | ||
}; | ||
ruleTester.run('own-props-must-be-private', rule, { | ||
valid: [ | ||
{ | ||
code: fs.readFileSync(files.good, 'utf8'), | ||
filename: files.good | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: fs.readFileSync(files.wrong, 'utf8'), | ||
filename: files.wrong, | ||
errors: 4 | ||
} | ||
] | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
tests/lib/rules/own-props-must-be-private/own-props-must-be-private.wrong.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@Component({ tag: 'sample-tag' }) | ||
export class SampleTag { | ||
internalProp: string; | ||
public internalProp2 = 1; | ||
@OwnDecorator() public internalDecoratedProp: string; | ||
@OwnDecorator() internalDecoratedProp2: string; | ||
|
||
@Prop() readonly test?: string; | ||
@Prop({ mutable: true }) testMutable?: string; | ||
|
||
|
||
render() { | ||
return (<div>test</div>); | ||
} | ||
} |