Skip to content

Commit 480c80b

Browse files
committed
feature: @putout/operate: extract: TSAsExpression: add support
1 parent 1ada843 commit 480c80b

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

packages/operate/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Extract node value according to it's type::
8686
- if it is `TSTypeParameter` return `name`;
8787
- if it is `MemberExpression` return `object.property`;
8888
- if it is `ArrayExpression` return `element1,element2,...,elementN`;
89+
- if it is `TSAsExpression` return `key.expression`;
8990
- `throw` in other cases
9091

9192
### `insertAfter(path, node)`

packages/operate/lib/extract.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
isJSXIdentifier,
1616
isTSTypeReference,
1717
isTSTypeParameter,
18+
isTSAsExpression,
1819
} = types;
1920

2021
module.exports.extract = extract;
@@ -61,6 +62,9 @@ function extract(node) {
6162
if (isTSTypeParameter(node))
6263
return extract(node.name);
6364

65+
if (isTSAsExpression(node))
66+
return extract(node.expression);
67+
6468
const nodeTypes = [
6569
'Literals',
6670
'Identifiers',
@@ -73,6 +77,7 @@ function extract(node) {
7377
'JSXAttribute',
7478
'JSXText',
7579
'TSTypeParameter',
80+
'TSAsExpression',
7681
].join(', ');
7782

7883
throw Error(`'operator.extract(node)' understands only ${nodeTypes} and TSTypeReference🤷, found: ${node.type}`);

packages/operate/lib/extract.spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const tryCatch = require('try-catch');
44
const {test} = require('supertape');
5+
const {parse} = require('putout');
56

67
const {extract} = require('./extract');
78

@@ -248,8 +249,28 @@ test('operate: extract: unknown', (t) => {
248249
};
249250

250251
const [error] = tryCatch(extract, node);
251-
const expected = `'operator.extract(node)' understands only Literals, Identifiers, TemplateLiteral, TemplateElement, RegExpLiteral, ArrayExpression, MemberExpression, JSXIdentifier, JSXAttribute, JSXText, TSTypeParameter and TSTypeReference🤷, found: UnknownStatement`;
252+
const expected = `'operator.extract(node)' understands only Literals, Identifiers, TemplateLiteral, TemplateElement, RegExpLiteral, ArrayExpression, MemberExpression, JSXIdentifier, JSXAttribute, JSXText, TSTypeParameter, TSAsExpression and TSTypeReference🤷, found: UnknownStatement`;
252253

253254
t.equal(error.message, expected);
254255
t.end();
255256
});
257+
258+
test('operate: extract: TSAsExpression', (t) => {
259+
const source = `
260+
const a = {
261+
[x as keyof typeof y]: 1,
262+
};
263+
`;
264+
265+
const ast = parse(source, {
266+
isTS: true,
267+
});
268+
269+
const {key} = ast.program.body[0].declarations[0].init.properties[0];
270+
271+
const result = extract(key);
272+
const expected = 'x';
273+
274+
t.equal(result, expected);
275+
t.end();
276+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const a = {
2+
[x as keyof typeof y]: 1,
3+
[Y[x as keyof typeof Y]]: 1,
4+
};

packages/plugin-remove-duplicate-keys/test/remove-duplicate-keys.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ test('remove duplicate-keys: no report: object-pattern-nested', (t) => {
8181
t.noReport('object-pattern-nested');
8282
t.end();
8383
});
84+
85+
test('remove duplicate-keys: no report: as', (t) => {
86+
t.noReport('as');
87+
t.end();
88+
});

0 commit comments

Comments
 (0)