forked from stenciljs/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-methods-must-be-private): add new rule
- Loading branch information
Showing
9 changed files
with
155 additions
and
0 deletions.
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-methods-must-be-private | ||
|
||
Ensures that all own class properties are private. | ||
|
||
## Config | ||
|
||
No config is needed | ||
|
||
## Usage | ||
|
||
```json | ||
{ "@d0whc3r/stencil/own-methods-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,48 @@ | ||
import { Rule } from 'eslint'; | ||
import ts from 'typescript'; | ||
import { isPrivate, stencilComponentContext, stencilDecorators, stencilLifecycle } from '../utils'; | ||
|
||
const rule: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: 'This rule catches own class methods 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, | ||
'MethodDefinition': (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)); | ||
const stencilCycle = stencilLifecycle.includes(originalNode.name.escapedText); | ||
if (!stencilDecorator && !stencilCycle && !isPrivate(originalNode)) { | ||
const text = String(originalNode.getFullText()); | ||
context.report({ | ||
node: node, | ||
message: `Own class methods cannot be public`, | ||
fix(fixer) { | ||
const methodName = node.key.name; | ||
const result = text.replace('public ', '').replace(methodName, `private ${methodName}`); | ||
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
18 changes: 18 additions & 0 deletions
18
tests/lib/rules/own-methods-must-be-private/own-methods-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,18 @@ | ||
@Component({ tag: 'sample-tag' }) | ||
export class SampleTag { | ||
@Prop() readonly test?: string; | ||
@Prop({ mutable: true }) testMutable?: string; | ||
|
||
private internalMethod() { | ||
return 'ok'; | ||
} | ||
|
||
@OwnDecorator() | ||
private internalDecoratedMethod() { | ||
return 'ok'; | ||
}; | ||
|
||
render() { | ||
return (<div>test</div>); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/lib/rules/own-methods-must-be-private/own-methods-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-methods-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-methods-must-be-private.good.tsx'), | ||
wrong: path.resolve(__dirname, 'own-methods-must-be-private.wrong.tsx') | ||
}; | ||
ruleTester.run('own-methods-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 | ||
} | ||
] | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
tests/lib/rules/own-methods-must-be-private/own-methods-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,27 @@ | ||
@Component({ tag: 'sample-tag' }) | ||
export class SampleTag { | ||
@Prop() readonly test?: string; | ||
@Prop({ mutable: true }) testMutable?: string; | ||
|
||
internalMethod() { | ||
return 'ok'; | ||
} | ||
|
||
public internalMethod2() { | ||
return 'ok'; | ||
} | ||
|
||
@OwnDecorator() | ||
internalDecoratedMethod() { | ||
return 'ok'; | ||
}; | ||
|
||
@OwnDecorator() | ||
public internalDecoratedMethod2() { | ||
return 'ok'; | ||
}; | ||
|
||
render() { | ||
return (<div>test</div>); | ||
} | ||
} |