From 951865d02a0a6f11565b8bca6d346b390dd68fa8 Mon Sep 17 00:00:00 2001 From: vijaykrishna Date: Sat, 30 Oct 2021 01:52:56 +0530 Subject: [PATCH 1/2] Ruby detection issue resolved #48 --- src/languages/cpp.ts | 1 + src/languages/cs.ts | 2 ++ src/languages/julia.ts | 1 + src/languages/lua.ts | 3 +++ src/languages/ruby.ts | 5 ++++ tests/lua.test.ts | 35 +++++++++++++++++++++++++ tests/ruby.test.ts | 58 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 105 insertions(+) diff --git a/src/languages/cpp.ts b/src/languages/cpp.ts index 8baf9dc..128fdce 100644 --- a/src/languages/cpp.ts +++ b/src/languages/cpp.ts @@ -40,6 +40,7 @@ export const CPP: LanguagePattern[] = [ // Avoiding Ruby confusion { pattern: /def\s+\w+\s*(\(.+\))?\s*\n/, type: 'not' }, { pattern: /puts\s+("|').+("|')/, type: 'not' }, + { pattern: /\bmodule\s\S/, type: 'not' }, // Avoiding C# confusion { pattern: /Console\.(WriteLine|Write)(\s*)?\(/, type: 'not' }, { pattern: /(using\s)?System(\..*)?(;)?/, type: 'not' }, diff --git a/src/languages/cs.ts b/src/languages/cs.ts index afcfaf3..aecdcb2 100644 --- a/src/languages/cs.ts +++ b/src/languages/cs.ts @@ -37,4 +37,6 @@ export const CS: LanguagePattern[] = [ // Avoiding Java confusion { pattern: /(extends|throws|@Attribute)/, type: 'not' }, { pattern: /System\.(in|out)\.\w+/, type: 'not' }, + // Avoiding Ruby confusion + { pattern: /\bmodule\s\S/, type: 'not' }, ]; diff --git a/src/languages/julia.ts b/src/languages/julia.ts index 9b86b28..b2b9ece 100644 --- a/src/languages/julia.ts +++ b/src/languages/julia.ts @@ -31,6 +31,7 @@ export const Julia: LanguagePattern[] = [ { pattern: /class\s/, type: 'not' }, // Avoiding Lua confusion { pattern: /local\s(function|\w+)/, type: 'not' }, + { pattern: /\bmodule\(.*\)/, type: 'not' }, // Avoiding Kotlin confusion { pattern: /fun main\((.*)?\) {/, type: 'not' }, { pattern: /fun(\s+)([A-Za-z0-9_])(\s+)?\((.*)\)(\s+){/, type: 'not' }, diff --git a/src/languages/lua.ts b/src/languages/lua.ts index a78a06f..1195ae4 100644 --- a/src/languages/lua.ts +++ b/src/languages/lua.ts @@ -43,6 +43,8 @@ export const Lua: LanguagePattern[] = [ { pattern: /--(\[\[)?.*/, type: 'comment.line' }, // rest arguments { pattern: /\.\.\./, type: 'keyword.other' }, + // module usage + { pattern: /\bmodule\(.*\)/, type: 'keyword.other' }, // invalid comments { pattern: /(\/\/|\/\*)/, type: 'not' }, @@ -58,6 +60,7 @@ export const Lua: LanguagePattern[] = [ { pattern: /(SELECT|FROM|INSERT|ALTER)/, type: 'not' }, // avoid confusion with Ruby { pattern: /(puts)/, type: 'not' }, + { pattern: /\bmodule\s\S/, type: 'not' }, // avoid confusion Julia { pattern: /(([a-zA-Z0-9]+)::([a-zA-Z0-9]+)|using|(.*)!\(.*\)|(\|\|))/, type: 'not' }, ]; diff --git a/src/languages/ruby.ts b/src/languages/ruby.ts index b27a215..9194d04 100644 --- a/src/languages/ruby.ts +++ b/src/languages/ruby.ts @@ -19,6 +19,11 @@ export const Ruby: LanguagePattern[] = [ { pattern: /\w+\.new\s+/, type: 'keyword' }, // elsif keyword { pattern: /elsif/, type: 'keyword.control' }, + // module + { pattern: /\bmodule\s\S/, type: 'keyword.other' }, + // BEGIN and END + { pattern: /\bBEGIN\s\{.*\}/, type: 'keyword.other' }, + { pattern: /\bEND\s\{.*\}/, type: 'keyword.other' }, // do { pattern: /do\s*[|]\w+(,\s*\w+)*[|]/, type: 'keyword.control' }, // for loop diff --git a/tests/lua.test.ts b/tests/lua.test.ts index c1aac1c..410ba29 100644 --- a/tests/lua.test.ts +++ b/tests/lua.test.ts @@ -357,4 +357,39 @@ end`); assert.equal(code.language, 'Lua'); }); +test('modules detection', () => { + const code = detectLang(`module("mymath", package.seeall) + + function mymath.add(a,b) + print(a+b) + end + + function mymath.sub(a,b) + print(a-b) + end + + function mymath.mul(a,b) + print(a*b) + end + + function mymath.div(a,b) + print(a/b) + end`); + + assert.equal(code.language, 'Lua'); +}); + +test('craete user defined modules', () => { + const code = detectLang(`module("mymodule", package.seeall) + + function foo() -- create it as if it's a global function + print("Hello World!") + end + + require "mymodule" + mymodule.foo()`); + + assert.equal(code.language, 'Lua'); +}); + test.run(); diff --git a/tests/ruby.test.ts b/tests/ruby.test.ts index c2650e3..a0aab19 100644 --- a/tests/ruby.test.ts +++ b/tests/ruby.test.ts @@ -205,4 +205,62 @@ p tukey_array assert.equal(code.language, 'Ruby'); }); +test('ludic numbers', () => { + const code = detectLang(`module XYZ + class A + end + class B < A + end + end`); + assert.equal(code.language, 'Ruby'); +}); + +test('testing BEGIN and END', () => { + const code = detectLang(`#!/usr/bin/ruby + + puts "This is main Ruby Program" + + END { + puts "Terminating Ruby Program" + } + BEGIN { + puts "Initializing Ruby Program" + }`); + assert.equal(code.language, 'Ruby'); +}); + +test('testing BEGIN and END other test case', () => { + const code = detectLang(`# Ruby Program of BEGIN and END Block + + # BEGIN block + BEGIN { + + a = 4 + b = 3 + c = a + b + + # BEGIN block code + puts "This is BEGIN block code" + puts c + + } + + # END block + END { + + a = 4 + b = 3 + c = a * b + + # END block code + puts "This is END block code" + puts c + } + + # Code will execute before END block + puts "Main Block" + `); + assert.equal(code.language, 'Ruby'); +}); + test.run(); From d4286d927b93a207be7930fb080848542d9c8299 Mon Sep 17 00:00:00 2001 From: vijaykrishna Date: Sat, 30 Oct 2021 10:46:25 +0530 Subject: [PATCH 2/2] Change in test cases and regex case lua --- src/languages/lua.ts | 2 +- tests/ruby.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/languages/lua.ts b/src/languages/lua.ts index 1195ae4..2fce496 100644 --- a/src/languages/lua.ts +++ b/src/languages/lua.ts @@ -44,7 +44,7 @@ export const Lua: LanguagePattern[] = [ // rest arguments { pattern: /\.\.\./, type: 'keyword.other' }, // module usage - { pattern: /\bmodule\(.*\)/, type: 'keyword.other' }, + { pattern: /\bmodule\s*\(.*\)/, type: 'keyword.other' }, // invalid comments { pattern: /(\/\/|\/\*)/, type: 'not' }, diff --git a/tests/ruby.test.ts b/tests/ruby.test.ts index a0aab19..fb6c4c6 100644 --- a/tests/ruby.test.ts +++ b/tests/ruby.test.ts @@ -205,7 +205,7 @@ p tukey_array assert.equal(code.language, 'Ruby'); }); -test('ludic numbers', () => { +test('ruby conflicts with other language', () => { const code = detectLang(`module XYZ class A end