From d92be4ee07f3f6c823f6e5abe36d1811886d899e Mon Sep 17 00:00:00 2001 From: dbale-altoros Date: Tue, 1 Aug 2023 12:40:20 -0300 Subject: [PATCH] allow $ symbol as a naming letter --- lib/common/identifier-naming.js | 6 +- test/rules/naming/const-name-snakecase.js | 19 +++++ test/rules/naming/contract-name-camelcase.js | 57 +++++++++++++ test/rules/naming/event-name-camelcase.js | 19 +++++ test/rules/naming/func-name-mixedcase.js | 19 +++++ .../rules/naming/func-param-name-mixedcase.js | 37 +++++++++ test/rules/naming/immutable-vars-naming.js | 80 ++++++++----------- test/rules/naming/modifier-name-mixedcase.js | 19 +++++ test/rules/naming/var-name-mixedcase.js | 19 +++++ 9 files changed, 227 insertions(+), 48 deletions(-) diff --git a/lib/common/identifier-naming.js b/lib/common/identifier-naming.js index 2a969a89..1444d398 100644 --- a/lib/common/identifier-naming.js +++ b/lib/common/identifier-naming.js @@ -4,7 +4,7 @@ function match(text, regex) { module.exports = { isMixedCase(text) { - return match(text, /[_]*[a-z]+[a-zA-Z0-9$]*[_]?/) + return match(text, /[_]*[a-z$]+[a-zA-Z0-9$]*[_]?/) }, isNotMixedCase(text) { @@ -12,7 +12,7 @@ module.exports = { }, isCamelCase(text) { - return match(text, /[A-Z]+[a-zA-Z0-9$]*/) + return match(text, /[A-Z$]+[a-zA-Z0-9$]*/) }, isNotCamelCase(text) { @@ -20,7 +20,7 @@ module.exports = { }, isUpperSnakeCase(text) { - return match(text, /_{0,2}[A-Z0-9]+[_A-Z0-9]*/) + return match(text, /_{0,2}[A-Z0-9$]+[_A-Z0-9$]*/) }, isNotUpperSnakeCase(text) { diff --git a/test/rules/naming/const-name-snakecase.js b/test/rules/naming/const-name-snakecase.js index 125c5300..54357517 100644 --- a/test/rules/naming/const-name-snakecase.js +++ b/test/rules/naming/const-name-snakecase.js @@ -74,4 +74,23 @@ describe('Linter - const-name-snakecase', () => { assert.equal(report.errorCount, 0) }) + + describe('constant name with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('uint32 private constant $THE_CONSTANT = 10;'), + 'containing a $': contractWith('uint32 private constant THE_$_CONSTANT = 10;'), + 'ending with $': contractWith('uint32 private constant THE_CONSTANT$ = 10;'), + 'only with $': contractWith('uint32 private constant $ = 10;'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise error for ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'const-name-snakecase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) }) diff --git a/test/rules/naming/contract-name-camelcase.js b/test/rules/naming/contract-name-camelcase.js index 02f98696..787816c3 100644 --- a/test/rules/naming/contract-name-camelcase.js +++ b/test/rules/naming/contract-name-camelcase.js @@ -35,4 +35,61 @@ describe('Linter - contract-name-camelcase', () => { assert.equal(report.errorCount, 1) assert.ok(report.messages[0].message.includes('CamelCase')) }) + + describe('Struct name with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('struct $MyStruct {}'), + 'containing a $': contractWith('struct My$Struct {}'), + 'ending with $': contractWith('struct MyStruct$ {}'), + 'only with $': contractWith('struct $ {}'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise contract name error for Structs ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'contract-name-camelcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) + + describe('Enums name with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('enum $MyEnum {}'), + 'containing a $': contractWith('enum My$Enum {}'), + 'ending with $': contractWith('enum MyEnum$ {}'), + 'only with $': contractWith('enum $ {}'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise contract name error for Enums ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'contract-name-camelcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) + + describe('Contract name with $ character', () => { + const WITH_$ = { + 'starting with $': 'contract $MyContract {}', + 'containing a $': 'contract My$Contract {}', + 'ending with $': 'contract MyContract$ {}', + 'only with $': 'contract $ {}', + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise contract name error for Contracts ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'contract-name-camelcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) }) diff --git a/test/rules/naming/event-name-camelcase.js b/test/rules/naming/event-name-camelcase.js index 9c0dd2db..27bcd05c 100644 --- a/test/rules/naming/event-name-camelcase.js +++ b/test/rules/naming/event-name-camelcase.js @@ -13,4 +13,23 @@ describe('Linter - event-name-camelcase', () => { assert.equal(report.errorCount, 1) assert.ok(report.messages[0].message.includes('CamelCase')) }) + + describe('Event name with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('event $Event1(uint a);'), + 'containing a $': contractWith('event Eve$nt1(uint a);'), + 'ending with $': contractWith('event Event1$(uint a);'), + 'only with $': contractWith('event $(uint a);'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise event name error for Events ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'event-name-camelcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) }) diff --git a/test/rules/naming/func-name-mixedcase.js b/test/rules/naming/func-name-mixedcase.js index fe9d62c8..c468b26a 100644 --- a/test/rules/naming/func-name-mixedcase.js +++ b/test/rules/naming/func-name-mixedcase.js @@ -23,4 +23,23 @@ describe('Linter - func-name-mixedcase', () => { assert.equal(report.errorCount, 0) }) + + describe('Function names with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('function $aFunc1Nam23e () public {}'), + 'containing a $': contractWith('function aFunc$1Nam23e () public {}'), + 'ending with $': contractWith('function aFunc1Nam23e$ () public {}'), + 'only with $': contractWith('function $() public {}'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise func name error for Functions ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'func-name-mixedcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) }) diff --git a/test/rules/naming/func-param-name-mixedcase.js b/test/rules/naming/func-param-name-mixedcase.js index 5d7169da..72ef9beb 100644 --- a/test/rules/naming/func-param-name-mixedcase.js +++ b/test/rules/naming/func-param-name-mixedcase.js @@ -24,4 +24,41 @@ describe('Linter - func-param-name-mixedcase', () => { assert.equal(report.errorCount, 1) assert.ok(report.messages[0].message.includes('mixedCase')) }) + + describe('Function Paramters name with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('function funcName (uint $param) public {}'), + 'containing a $': contractWith('function funcName (uint pa$ram) public {}'), + 'ending with $': contractWith('function funcName (uint param$) public {}'), + 'only with $': contractWith('function funcName(uint $) public {}'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise func param name error for Function Parameters ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'func-param-name-mixedcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) + describe('Event parameters name with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('event Event1(uint $param);'), + 'containing a $': contractWith('event Event1(uint pa$ram);'), + 'ending with $': contractWith('event Event1(uint param$);'), + 'only with $': contractWith('event Event1(uint $);'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise func name error for Event Parameters ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'func-name-mixedcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) }) diff --git a/test/rules/naming/immutable-vars-naming.js b/test/rules/naming/immutable-vars-naming.js index 4b9cb7de..b695a7ec 100644 --- a/test/rules/naming/immutable-vars-naming.js +++ b/test/rules/naming/immutable-vars-naming.js @@ -65,53 +65,43 @@ describe('immutable-vars-naming', () => { assert.equal(report.errorCount, 0) }) - describe('immutable-vars-naming ==> warnings (same as above)', () => { - it('should raise warning when immutablesAsConstants = false and variable is in snake case', () => { - const code = contractWith('uint32 private immutable SNAKE_CASE;') - - const report = linter.processStr(code, { - rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: false }] }, - }) - - assert.equal(report.warningCount, 1) - assert.ok( - report.messages[0].message.includes('Immutable variables names are set to be in mixedCase') - ) - }) - - it('should NOT raise warning when immutablesAsConstants = false and variable is in snake case', () => { - const code = contractWith('uint32 private immutable SNAKE_CASE;') - - const report = linter.processStr(code, { - rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: true }] }, + describe('Immutable variable with $ character as mixedCase', () => { + const WITH_$ = { + 'starting with $': contractWith('uint32 immutable private $D;'), + 'containing a $': contractWith('uint32 immutable private testWith$Contained;'), + 'ending with $': contractWith('uint32 immutable private testWithEnding$;'), + 'only with $': contractWith('uint32 immutable private $;'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise immutable error for variables ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'immutable-vars-naming': ['error', { immutablesAsConstants: false }] }, + }) + + assert.equal(report.errorCount, 0) }) + } + }) - assert.equal(report.warningCount, 0) - }) - - it('should raise warning when immutablesAsConstants = true and variable is in mixedCase', () => { - const code = contractWith('uint32 private immutable mixedCase;') - - const report = linter.processStr(code, { - rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: true }] }, - }) - - assert.equal(report.warningCount, 1) - assert.ok( - report.messages[0].message.includes( - 'Immutable variables name are set to be in capitalized SNAKE_CASE' - ) - ) - }) - - it('should NOT raise warning when immutablesAsConstants = false and variable is in mixedCase', () => { - const code = contractWith('uint32 private immutable mixedCase;') - - const report = linter.processStr(code, { - rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: false }] }, + describe('Immutable variable with $ character as SNAKE_CASE', () => { + const WITH_$ = { + 'starting with $': contractWith('uint32 immutable private $_D;'), + 'starting with $D': contractWith('uint32 immutable private $D;'), + 'containing a $': contractWith('uint32 immutable private TEST_WITH_$_CONTAINED;'), + 'ending with $': contractWith('uint32 immutable private TEST_WITH_ENDING_$;'), + 'ending with D$': contractWith('uint32 immutable private TEST_WITH_ENDING_D$;'), + 'only with $': contractWith('uint32 immutable private $;'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise immutable error for variables ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'immutable-vars-naming': ['error', { immutablesAsConstants: true }] }, + }) + + assert.equal(report.errorCount, 0) }) - - assert.equal(report.warningCount, 0) - }) + } }) }) diff --git a/test/rules/naming/modifier-name-mixedcase.js b/test/rules/naming/modifier-name-mixedcase.js index a652c156..5dfd1a36 100644 --- a/test/rules/naming/modifier-name-mixedcase.js +++ b/test/rules/naming/modifier-name-mixedcase.js @@ -23,4 +23,23 @@ describe('Linter - modifier-name-mixedcase', () => { assert.equal(report.errorCount, 0) }) + + describe('with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('modifier $ownedBy(address a) { }'), + 'containing a $': contractWith('modifier owned$By(address a) { }'), + 'ending with $': contractWith('modifier ownedBy$(address a) { }'), + 'only with $': contractWith('modifier $(uint a) { }'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise func name error for Modifiers ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'modifier-name-mixedcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) }) diff --git a/test/rules/naming/var-name-mixedcase.js b/test/rules/naming/var-name-mixedcase.js index 635a7924..2e6730e3 100644 --- a/test/rules/naming/var-name-mixedcase.js +++ b/test/rules/naming/var-name-mixedcase.js @@ -66,4 +66,23 @@ describe('Linter - var-name-mixedcase', () => { assert.equal(report.errorCount, 0) }) + + describe('with $ character', () => { + const WITH_$ = { + 'starting with $': contractWith('uint32 private $D = 10;'), + 'containing a $': contractWith('uint32 private testWith$Contained = 10;'), + 'ending with $': contractWith('uint32 private testWithEnding$ = 10;'), + 'only with $': contractWith('uint32 private $;'), + } + + for (const [key, code] of Object.entries(WITH_$)) { + it(`should not raise var name error for variables ${key}`, () => { + const report = linter.processStr(code, { + rules: { 'var-name-mixedcase': 'error' }, + }) + + assert.equal(report.errorCount, 0) + }) + } + }) })