Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Syntax/Language/ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {Match} from '../Match.js';

const language = new Language('ruby');

// Ruby-style function definitions and method calls (def foo, .bar)
// Method names can end with ? or !
// Ruby-style function definitions and method calls (def foo, .bar).
// Predicate and bang methods are also unambiguous without a receiver or arguments.
const rubyStyleFunction = {
pattern: /(?:def\s+|\.)([a-z_][a-z0-9_]*[?!]?)/i,
matches: Rule.extractMatches({type: 'function'})
pattern:
/(?:def\s+|\.)([a-z_][a-z0-9_]*[?!]?)|(^|[^\w.:])([a-z_][a-z0-9_]*[?!])(?!:)/i,
matches: Rule.extractMatches(
{index: 1, type: 'function'},
{index: 3, type: 'function'}
)
};

// Emulate negative lookbehind to avoid matching ::symbol (only match :symbol not ::symbol)
Expand Down
27 changes: 26 additions & 1 deletion test/Syntax/Language/ruby.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Syntax from '../../../Syntax.js';
import registerRuby from '../../../Syntax/Language/ruby.js';
import {strictEqual} from 'node:assert';
import {deepStrictEqual, strictEqual} from 'node:assert';
import test from 'node:test';

async function getTypesFor(code) {
Expand Down Expand Up @@ -68,3 +68,28 @@ test('Ruby: function detection', async () => {
const types = await getTypesFor('def compute\nend\nobject.method');
strictEqual(types.includes('function'), true);
});

test('Ruby: predicate and bang method detection', async () => {
const syntax = new Syntax();
registerRuby(syntax);
const language = await syntax.getLanguage('ruby');
const matches = await language.getMatches(
syntax,
'def valid?\nend\nobject.save!\nvalid?(value)\nsave! value\nready?'
);
const functions = matches
.filter(match => match.expression.type === 'function')
.map(match => match.value);

deepStrictEqual(functions, ['valid?', 'save!', 'valid?', 'save!', 'ready?']);
});

test('Ruby: predicate and bang symbols are not methods', async () => {
const syntax = new Syntax();
registerRuby(syntax);
const language = await syntax.getLanguage('ruby');
const matches = await language.getMatches(syntax, ':valid?\n:save!\nvalid?: true');
const functions = matches.filter(match => match.expression.type === 'function');

deepStrictEqual(functions, []);
});