Skip to content

Commit

Permalink
Merge pull request #59 from vijaykrishna536/master
Browse files Browse the repository at this point in the history
Ruby detection issue resolved #48
  • Loading branch information
Reinaldy Rafli authored Oct 30, 2021
2 parents c49e19e + d4286d9 commit 3825915
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/languages/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
2 changes: 2 additions & 0 deletions src/languages/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
];
1 change: 1 addition & 0 deletions src/languages/julia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
3 changes: 3 additions & 0 deletions src/languages/lua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const Lua: LanguagePattern[] = [
{ pattern: /--(\[\[)?.*/, type: 'comment.line' },
// rest arguments
{ pattern: /\.\.\./, type: 'keyword.other' },
// module usage
{ pattern: /\bmodule\s*\(.*\)/, type: 'keyword.other' },

// invalid comments
{ pattern: /(\/\/|\/\*)/, type: 'not' },
Expand All @@ -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' },
];
5 changes: 5 additions & 0 deletions src/languages/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions tests/lua.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
58 changes: 58 additions & 0 deletions tests/ruby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,62 @@ p tukey_array
assert.equal(code.language, 'Ruby');
});

test('ruby conflicts with other language', () => {
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();

0 comments on commit 3825915

Please sign in to comment.