Skip to content

Commit

Permalink
feature: goldstein: keyword-useless-comma: class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jun 3, 2024
1 parent 43d5a9b commit 5d11929
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,11 @@ const a = {
- b;
+ b,
};

const a = {
- b(){},
+ b(){}
};
```

### Assign from
Expand Down
4 changes: 2 additions & 2 deletions packages/goldstein/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {parse} from './parser.js';
export {convert} from '../convert/index.js';
export {print} from '../printer/index.js';
export * from './parser.js';
import * as renameUnnamedIdentifier from '../keyword-useless-comma/rename-unnamed-identifier/index.js';
import * as removeUnnamedObjectProperty from '../keyword-useless-comma/remove-unnamed-object-property/index.js';

export const compile = (source, options = {}) => {
const ast = parse(source, options);
Expand All @@ -21,7 +21,7 @@ export const compile = (source, options = {}) => {
['try-catch', tryCatchPlugin],
['declare', declarePlugin],
['logical-expressions', logicalExpressionsPlugin],
['remove-unnamed-identifiers', renameUnnamedIdentifier],
['remove-unnamed-identifiers', removeUnnamedObjectProperty],
],
});

Expand Down
21 changes: 20 additions & 1 deletion packages/goldstein/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,23 @@ test('goldstein: parse: useless comma', (t) => {
t.end();
});

test('goldstein: parse: useless comma: class', (t) => {
const result = compile(montag`
const a = class {
b() {},
};
`);

const expected = montag`
const a = class {
b() {}
};\n
`;

t.equal(result, expected);
t.end();
});

test('goldstein: parse: useless semicolon', (t) => {
const result = compile(montag`
const a = {
Expand Down Expand Up @@ -503,7 +520,9 @@ test('goldstein: compile: parse-maybe-array', (t) => {
`;

const [error] = tryCatch(compile, source, {
keywords: {},
keywords: {
keywordAddArray: false,
},
});

t.equal(error.message, `☝️Looks like 'keyword-add-array' is missing.`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const report = () => {};
export const fix = (path) => path.remove();
export const traverse = ({push}) => ({
ObjectProperty(path) {
'ObjectProperty|ClassProperty'(path) {
const keyPath = path.get('key');

if (keyPath.isIdentifier() && !keyPath.node.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {test} from 'supertape';
import montag from 'montag';
import {transform} from 'putout';
import * as renameUnnamedIdentifier from './index.js';
import * as removeUnnamedIdentifier from './index.js';
import {parse, print} from '../../goldstein/index.js';
import keywordUselessComma from '../index.js';

test('goldstein: keyword: useless-comma: rename-unnamed-identifier', (t) => {
test('goldstein: keyword: useless-comma: remove-unnamed-object-property', (t) => {
const source = montag`
const a = {
b,,
Expand All @@ -20,7 +20,7 @@ test('goldstein: keyword: useless-comma: rename-unnamed-identifier', (t) => {

transform(ast, source, {
plugins: [
['rename', renameUnnamedIdentifier],
['remove', removeUnnamedIdentifier],
],
});

Expand All @@ -36,7 +36,7 @@ test('goldstein: keyword: useless-comma: rename-unnamed-identifier', (t) => {
t.end();
});

test('goldstein: keyword: useless-comma: rename-unnamed-identifier: StringLiteral', (t) => {
test('goldstein: keyword: useless-comma: remove-unnamed-object-property: StringLiteral', (t) => {
const source = montag`
const a = {
"hello": "world",,
Expand All @@ -51,7 +51,7 @@ test('goldstein: keyword: useless-comma: rename-unnamed-identifier: StringLitera

transform(ast, source, {
plugins: [
['rename', renameUnnamedIdentifier],
['rename', removeUnnamedIdentifier],
],
});

Expand All @@ -66,3 +66,37 @@ test('goldstein: keyword: useless-comma: rename-unnamed-identifier: StringLitera
t.equal(code, expected);
t.end();
});

test('goldstein: keyword: useless-comma: remove-unnamed-class-property', (t) => {
const source = montag`
const a = class {
b() {},
c() {},
};
`;

const ast = parse(source, {
keywords: [
keywordUselessComma,
],
});

transform(ast, source, {
plugins: [
['remove', removeUnnamedIdentifier],
],
});

const code = print(ast);

const expected = montag`
const a = class {
b() {}
c() {}
};\n
`;

t.equal(code, expected);
t.end();
});

0 comments on commit 5d11929

Please sign in to comment.