Skip to content

Commit e4ae5c9

Browse files
committed
refactor: revert codemod features and options
1 parent 44b4899 commit e4ae5c9

File tree

13 files changed

+32
-268
lines changed

13 files changed

+32
-268
lines changed

README.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,9 @@ This plugin enforces its own set of opinionated rules:
5252

5353
Keys in `engines` are ordered alphabetically.
5454

55-
#### Options
56-
57-
`enginesNode`<br>
58-
Type: `string`
59-
60-
Asserts an `engines.node` property value. e.g. `{ enginesNode: '>= 10.0.0' }`. If this option is set and no `node` key exists in `engines`, it will be created.
61-
62-
`enginesNpm`<br>
63-
Type: `string`
64-
65-
Asserts an `engines.npm` property value. e.g. `{ enginesNpm: '>= 10.0.0' }`. If this option is set and no `npm` key exists in `engines`, it will be created.
66-
6755
### Files
6856

69-
Keys in `files` are ordered alphabetically. Additionally, `LICENSE` and `README.md` are added to `files` if they are missing. This plugin prefers implicit declaration of those files, even though they are included in a package automatically by `npm`.
70-
71-
#### Options
72-
73-
`filesLicense`<br>
74-
Type: `boolean`
75-
76-
To prevent `LICENSE` from being added to `files`, set this option to `false`.
77-
78-
`filesReadme`<br>
79-
Type: `boolean`
57+
Keys in `files` are ordered alphabetically.
8058

8159
To prevent `README.md` from being added to `files`, set this option to `false`.
8260

lib/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@ const { parsers } = require('prettier/parser-babylon');
1212

1313
const { engines } = require('./rules/engines');
1414
const { files } = require('./rules/files');
15-
const { options: pluginOptions } = require('./options');
1615
const { scripts } = require('./rules/scripts');
1716
const { sort } = require('./rules/sort');
1817

1918
const { 'json-stringify': parser } = parsers;
2019
const { parse } = parser;
2120
const rePkg = /package\.json$/;
2221

23-
const format = (properties, options) => {
22+
const format = (properties) => {
2423
let props = sort(properties);
25-
props = engines(props, options);
26-
props = files(props, options);
24+
props = engines(props);
25+
props = files(props);
2726
props = scripts(props);
2827

2928
return props;
3029
};
3130

3231
module.exports = {
3332
name: 'prettier-plugin-package',
34-
options: pluginOptions,
3533
parsers: {
3634
'json-stringify': {
3735
...parser,
@@ -42,7 +40,7 @@ module.exports = {
4240

4341
if (rePkg.test(filepath)) {
4442
const { properties } = ast;
45-
ast.properties = format(properties, options);
43+
ast.properties = format(properties);
4644
}
4745

4846
return ast;

lib/options.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

lib/rules/engines.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
The above copyright notice and this permission notice shall be
99
included in all copies or substantial portions of this Source Code Form.
1010
*/
11-
const process = (props, { enginesNode, enginesNpm }) => {
11+
const process = (props) => {
1212
const enginesIndex = props.findIndex((prop) => prop.key.value === 'engines');
1313

1414
if (enginesIndex >= 0) {
@@ -17,34 +17,6 @@ const process = (props, { enginesNode, enginesNpm }) => {
1717

1818
properties.sort((a, b) => (a.key.value > b.key.value ? 1 : a.key.value < b.key.value ? -1 : 0));
1919

20-
if (enginesNode) {
21-
const nodeIndex = properties.findIndex((prop) => prop.key.value === 'node');
22-
23-
if (nodeIndex >= 0) {
24-
properties[nodeIndex].value.value = enginesNode;
25-
} else {
26-
properties.push({
27-
type: 'ObjectProperty',
28-
key: { type: 'StringLiteral', value: 'node' },
29-
value: { type: 'StringLiteral', value: enginesNode }
30-
});
31-
}
32-
}
33-
34-
if (enginesNpm) {
35-
const npmIndex = properties.findIndex((prop) => prop.key.value === 'npm');
36-
37-
if (npmIndex >= 0) {
38-
properties[npmIndex].value.value = enginesNpm;
39-
} else {
40-
properties.push({
41-
type: 'ObjectProperty',
42-
key: { type: 'StringLiteral', value: 'npm' },
43-
value: { type: 'StringLiteral', value: enginesNpm }
44-
});
45-
}
46-
}
47-
4820
engines.value.properties = properties;
4921

5022
props.splice(enginesIndex, 0, engines);

lib/rules/files.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,41 @@
88
The above copyright notice and this permission notice shall be
99
included in all copies or substantial portions of this Source Code Form.
1010
*/
11-
const process = (props, { filesReadme, filesLicense }) => {
11+
const process = (props) => {
1212
const filesIndex = props.findIndex((prop) => prop.key.value === 'files');
1313

1414
if (filesIndex >= 0) {
1515
const [filesNode] = props.splice(filesIndex, 1);
1616

17+
let readme;
18+
let license;
1719
let { elements } = filesNode.value;
1820

1921
elements = elements
2022
.filter((node) => {
2123
const value = node.value.toLowerCase();
22-
return value !== 'license' && value !== 'readme.md';
24+
25+
// remove LICENSE and README and add to the end later on
26+
if (value === 'license') {
27+
license = node;
28+
return false;
29+
}
30+
31+
if (value === 'readme' || value === 'readme.md') {
32+
readme = node;
33+
return false;
34+
}
35+
36+
return true;
2337
})
2438
.sort((a, b) => (a.value > b.value ? 1 : a.value < b.value ? -1 : 0));
2539

26-
if (filesLicense) {
27-
elements.push({ type: 'StringLiteral', value: 'LICENSE' });
40+
if (readme) {
41+
elements.push(readme);
2842
}
2943

30-
if (filesReadme) {
31-
elements.push({ type: 'StringLiteral', value: 'README.md' });
44+
if (license) {
45+
elements.push(license);
3246
}
3347

3448
filesNode.value.elements = elements;

test/engines.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,3 @@ test('default', (t) => {
1919

2020
t.snapshot(output);
2121
});
22-
23-
test('enginesNode', (t) => {
24-
const options = {
25-
enginesNode: '>= batcave',
26-
filepath: 'package.json',
27-
parser: 'json-stringify',
28-
plugins: ['.']
29-
};
30-
const fixture = {
31-
engines: {
32-
npm: 'joker',
33-
node: 'batman'
34-
}
35-
};
36-
37-
const input = JSON.stringify(fixture, null, 2);
38-
const output = prettier.format(input, options);
39-
40-
t.snapshot(output);
41-
});
42-
43-
test('enginesNpm', (t) => {
44-
const options = {
45-
enginesNpm: '>= harley',
46-
filepath: 'package.json',
47-
parser: 'json-stringify',
48-
plugins: ['.']
49-
};
50-
const fixture = {
51-
engines: {
52-
npm: 'joker',
53-
node: 'batman'
54-
}
55-
};
56-
57-
const input = JSON.stringify(fixture, null, 2);
58-
const output = prettier.format(input, options);
59-
60-
t.snapshot(output);
61-
});
62-
63-
test('missing', (t) => {
64-
const options = {
65-
enginesNode: '>= batcave',
66-
enginesNpm: '>= harley',
67-
filepath: 'package.json',
68-
parser: 'json-stringify',
69-
plugins: ['.']
70-
};
71-
const fixture = {
72-
engines: {}
73-
};
74-
75-
const input = JSON.stringify(fixture, null, 2);
76-
const output = prettier.format(input, options);
77-
78-
t.snapshot(output);
79-
});

test/files.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,7 @@ test('default', (t) => {
88
plugins: ['.']
99
};
1010
const fixture = {
11-
files: ['lib/']
12-
};
13-
14-
const input = JSON.stringify(fixture, null, 2);
15-
const output = prettier.format(input, options);
16-
17-
t.snapshot(output);
18-
});
19-
20-
test('filesLicense', (t) => {
21-
const options = {
22-
filepath: 'package.json',
23-
filesLicense: false,
24-
parser: 'json-stringify',
25-
plugins: ['.']
26-
};
27-
const fixture = {
28-
files: ['lib/']
29-
};
30-
31-
const input = JSON.stringify(fixture, null, 2);
32-
const output = prettier.format(input, options);
33-
34-
t.snapshot(output);
35-
});
36-
37-
test('filesReadme', (t) => {
38-
const options = {
39-
filepath: 'package.json',
40-
filesReadme: false,
41-
parser: 'json-stringify',
42-
plugins: ['.']
43-
};
44-
const fixture = {
45-
files: ['lib/']
11+
files: ['README.md', 'LICENSE', 'lib/']
4612
};
4713

4814
const input = JSON.stringify(fixture, null, 2);

test/snapshots/engines.js.md

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,3 @@ Generated by [AVA](https://ava.li).
1515
}␊
1616
}␊
1717
`
18-
19-
## enginesNode
20-
21-
> Snapshot 1
22-
23-
`{␊
24-
"engines": {␊
25-
"node": ">= batcave",␊
26-
"npm": "joker"␊
27-
}␊
28-
}␊
29-
`
30-
31-
## enginesNpm
32-
33-
> Snapshot 1
34-
35-
`{␊
36-
"engines": {␊
37-
"node": "batman",␊
38-
"npm": ">= harley"␊
39-
}␊
40-
}␊
41-
`
42-
43-
## missing
44-
45-
> Snapshot 1
46-
47-
`{␊
48-
"engines": {␊
49-
"node": ">= batcave",␊
50-
"npm": ">= harley"␊
51-
}␊
52-
}␊
53-
`

test/snapshots/engines.js.snap

-105 Bytes
Binary file not shown.

test/snapshots/files.js.md

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,7 @@ Generated by [AVA](https://ava.li).
1111
`{␊
1212
"files": [␊
1313
"lib/",␊
14-
"LICENSE",␊
15-
"README.md"␊
16-
]␊
17-
}␊
18-
`
19-
20-
## filesLicense
21-
22-
> Snapshot 1
23-
24-
`{␊
25-
"files": [␊
26-
"lib/",␊
27-
"README.md"␊
28-
]␊
29-
}␊
30-
`
31-
32-
## filesReadme
33-
34-
> Snapshot 1
35-
36-
`{␊
37-
"files": [␊
38-
"lib/",␊
14+
"README.md",␊
3915
"LICENSE"␊
4016
]␊
4117
}␊

0 commit comments

Comments
 (0)