Skip to content

Commit d88a4ab

Browse files
authored
test(linter/plugins): cases for SourceCode#getLastToken() and SourceCode#getLastToken() (#16048)
- Part of #14829 (comment). - Follow up to #15861.
1 parent e232d35 commit d88a4ab

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

apps/oxlint/test/tokens.test.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,11 +1230,61 @@ describe('when calling getFirstToken & getTokenAfter', () => {
12301230
});
12311231
});
12321232

1233+
// https://github.com/eslint/eslint/blob/v9.39.1/tests/lib/languages/js/source-code/token-store.js#L1674-L1742
12331234
describe('when calling getLastToken & getTokenBefore', () => {
1234-
/* oxlint-disable-next-line no-disabled-tests expect-expect */
1235-
it('is to be implemented');
1236-
/* oxlint-disable-next-line no-unused-expressions */
1237-
getLastToken;
1238-
/* oxlint-disable-next-line no-unused-expressions */
1239-
getTokenBefore;
1235+
it('should retrieve all tokens and comments in the node', () => {
1236+
sourceText = '(function(a, /*b,*/ c){})';
1237+
const tokens = [];
1238+
// TODO: replace this verbatim range with `ast`
1239+
let token = getLastToken({ range: [0, 25] } as Node);
1240+
1241+
while (token) {
1242+
tokens.push(token);
1243+
token = getTokenBefore(token, {
1244+
includeComments: true,
1245+
});
1246+
}
1247+
1248+
expect(tokens.reverse().map((token) => token.value)).toEqual([
1249+
'(',
1250+
'function',
1251+
'(',
1252+
'a',
1253+
',',
1254+
'b,',
1255+
'c',
1256+
')',
1257+
'{',
1258+
'}',
1259+
')',
1260+
]);
1261+
});
1262+
1263+
it('should retrieve all tokens and comments in the node (no spaces)', () => {
1264+
sourceText = '(function(a,/*b,*/c){})';
1265+
const tokens = [];
1266+
// TODO: replace this verbatim range with `ast`
1267+
let token = getLastToken({ range: [0, 23] } as Node);
1268+
1269+
while (token) {
1270+
tokens.push(token);
1271+
token = getTokenBefore(token, {
1272+
includeComments: true,
1273+
});
1274+
}
1275+
1276+
expect(tokens.reverse().map((token) => token.value)).toEqual([
1277+
'(',
1278+
'function',
1279+
'(',
1280+
'a',
1281+
',',
1282+
'b,',
1283+
'c',
1284+
')',
1285+
'{',
1286+
'}',
1287+
')',
1288+
]);
1289+
});
12401290
});

0 commit comments

Comments
 (0)