-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add disallow-references rule
#6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1836575
Add noAssignRef rule
IshayMaya 1f0b957
fix readme
IshayMaya 91e70d1
update type
IshayMaya 896c659
removed redundant type
IshayMaya 2297fa8
format
IshayMaya dd7912c
Merge branch 'main' into feat/no-assign-ref
IshayMaya ad40128
added missing cases
IshayMaya ade3a6d
fix lint
IshayMaya cf461a6
fix test name
IshayMaya 8e96427
add changeset
IshayMaya f4d8768
update selectors and texts
IshayMaya d5bdc24
Merge branch 'main' into feat/no-assign-ref
IshayMaya 3e37b50
Merge branch 'main' into feat/no-assign-ref
StyleShit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| 'eslint-plugin-php': minor | ||
| --- | ||
|
|
||
| Add `disallow-references` rule |
This file contains hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
| import { RuleTester, type Rule } from 'eslint'; | ||
| import php from '../../index'; | ||
| import { disallowReferences } from '../disallow-references'; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| plugins: { | ||
| php, | ||
| }, | ||
| language: 'php/php', | ||
| }); | ||
|
|
||
| // TODO: Fix the types. | ||
| ruleTester.run( | ||
| 'disallow-references', | ||
| disallowReferences as unknown as Rule.RuleModule, | ||
| { | ||
| valid: [ | ||
| '<?php $a = [1, 2, 3]; $my_var = $a;', | ||
| '<?php function myFunc($var) { return []; }', | ||
| '<?php $used = function() use($var) {};', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: '<?php $a = [1, 2, 3]; $my_var = &$a;', | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowReferences', | ||
| line: 1, | ||
| column: 33, | ||
| endLine: 1, | ||
| endColumn: 36, | ||
| suggestions: [ | ||
| { | ||
| messageId: 'removeAmp', | ||
| output: '<?php $a = [1, 2, 3]; $my_var = $a;', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<?php function myFunc(&$var) { return []; }', | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowReferences', | ||
| line: 1, | ||
| column: 23, | ||
| endLine: 1, | ||
| endColumn: 28, | ||
| suggestions: [ | ||
| { | ||
| messageId: 'removeAmp', | ||
| output: '<?php function myFunc($var) { return []; }', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<?php function &myFunc($var) { return []; }', | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowReferences', | ||
| line: 1, | ||
| column: 16, | ||
| endLine: 1, | ||
| endColumn: 23, | ||
| suggestions: [ | ||
| { | ||
| messageId: 'removeAmp', | ||
| output: '<?php function myFunc($var) { return []; }', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: '<?php $used = function() use(&$var) {};', | ||
| errors: [ | ||
| { | ||
| messageId: 'disallowReferences', | ||
| line: 1, | ||
| column: 30, | ||
| endLine: 1, | ||
| endColumn: 35, | ||
| suggestions: [ | ||
| { | ||
| messageId: 'removeAmp', | ||
| output: '<?php $used = function() use($var) {};', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ); |
This file contains hidden or 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,57 @@ | ||
| import { createRule } from '../utils/create-rule'; | ||
|
|
||
| type MessageIds = 'disallowReferences' | 'removeAmp'; | ||
| type Options = []; | ||
|
|
||
| export const disallowReferences = createRule<MessageIds, Options>({ | ||
| meta: { | ||
| type: 'suggestion', | ||
| fixable: 'code', | ||
| hasSuggestions: true, | ||
| docs: { | ||
| description: 'Disallow the use of references', | ||
| }, | ||
| messages: { | ||
| disallowReferences: 'Do not use references (&).', | ||
| removeAmp: 'Remove the reference operator (&).', | ||
| }, | ||
| schema: [], | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| 'assignref > .right, parameter[byref="true"], function[byref="true"] > .name, variable[byref="true"]'( | ||
| node, | ||
| ) { | ||
| const ampKeyWord = context.sourceCode.findClosestKeyword( | ||
| node, | ||
| '&', | ||
| ); | ||
|
|
||
| if (!ampKeyWord) { | ||
| return; | ||
| } | ||
|
|
||
| context.report({ | ||
| node, | ||
| loc: { | ||
| start: ampKeyWord.start, | ||
| end: context.sourceCode.getLoc(node).end, | ||
| }, | ||
| messageId: 'disallowReferences', | ||
| suggest: [ | ||
| { | ||
| messageId: 'removeAmp', | ||
| fix(fixer) { | ||
| return fixer.removeRange([ | ||
| ampKeyWord.start.offset, | ||
| ampKeyWord.end.offset, | ||
| ]); | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.