Skip to content

Commit

Permalink
feature: redput: add ability to pass options
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Nov 28, 2023
1 parent fcfcd47 commit daae14d
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 19 deletions.
22 changes: 19 additions & 3 deletions lib/parse-plugin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import tryCatch from 'try-catch';

const {parse} = JSON;

export const parsePlugin = (raw) => {
const [comment, ...lines] = raw.split('\n');
const name = comment
.replace('//', '')
.trimStart();
const [options, name] = parseComment(comment);

return {
name,
lines,
options,
};
};

function parseComment(comment) {
const raw = comment
.replace('//', '')
.trimStart();

const [error, data] = tryCatch(parse, raw);

if (error)
return ['', raw];

return [data[0], data[1]];
}
21 changes: 21 additions & 0 deletions lib/parse-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ test('redput: parse-plugin: lines', (t) => {
t.deepEqual(lines, expected);
t.end();
});

test('redput: parse-plugin: lines: name: disabled', (t) => {
const {name} = parsePlugin(montag`
// ["off", "hello.js"]
export const report = () => 'hello';
`);

t.equal(name, 'hello.js');
t.end();
});

test('redput: parse-plugin: lines: options: disabled', (t) => {
const {options} = parsePlugin(montag`
// ["off", "hello.js"]
export const report = () => 'hello';
`);

t.equal(options, 'off');
t.end();
});

13 changes: 6 additions & 7 deletions lib/redput.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ export const redput = async (link, {token}) => {
const raw = result.data.files['transform.js'].content;
const fixture = result.data.files['source.js'].content;

const {name, lines} = parsePlugin(raw);

if (name.includes(':') || name.includes('{'))
return [
Error(`Bad name: ${name}`),
];

const {
name,
options,
lines,
} = parsePlugin(raw);
const rule = lines.join('\n');
const report = getReport(rule);

const [errorWrite] = await tryToCatch(writePlugin, name, {
options,
rule,
fixture,
report,
Expand Down
12 changes: 7 additions & 5 deletions lib/write/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import {
} from './simple.js';
import tryToCatch from 'try-to-catch';

export const writePlugin = async (name, {rule, fixture, report}) => {
export const writePlugin = async (name, {rule, fixture, report, options}) => {
const [isNested] = await tryToCatch(access, './package.json');

if (isNested)
return await writeNested(name, {
rule,
fixture,
report,
options,
});

await writeSimple(name, {
Expand All @@ -31,18 +32,19 @@ export const writePlugin = async (name, {rule, fixture, report}) => {
});
};

export const writeNested = async (name, {rule, fixture, report}) => {
await writeNestedRule(name, rule);
export const writeNested = async (name, {rule, fixture, report, options}) => {
await writeNestedRule(name, rule, options);
await writeNestedFixtures(name, fixture);
await writeNestedTests(name, report);

await tryToCatch(writeNestedOptional, name, {
options,
fixture,
});
};

export const writeNestedOptional = async (name, {fixture}) => {
await updateNestedIndex(name);
export const writeNestedOptional = async (name, {options, fixture}) => {
await updateNestedIndex(name, options);
await updateOverallNestedFixtures(name, fixture);
await updateOverallNestedTest(name);
};
Expand Down
3 changes: 2 additions & 1 deletion lib/write/nested/add-rule/add-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import * as insertGetRulePlugin from './plugin-insert-get-rule/index.js';
import pluginPutout from '@putout/plugin-putout';
import removeUnusedVariables from '@putout/plugin-remove-unused-variables';

export const addRule = (name, source) => {
export const addRule = (name, source, ruleOptions) => {
const {code} = putout(source, {
rules: {
'add-rule': ['on', {
name,
ruleOptions,
}],
},
plugins: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const getRule = (a) => ({
[a]: require(`./${a}`),
});

module.exports.rules = {
...getRule('add-function', 'off'),
...getRule('hello', 'off'),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const getRule = (a) => ({
[a]: require(`./${a}`),
});

module.exports.rules = {
...getRule('add-function', 'off'),
};
16 changes: 15 additions & 1 deletion lib/write/nested/add-rule/plugin-insert-get-rule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {traverse} = operator;
export const report = () => `Insert 'getRule()'`;

const createRule = template('getRule(%%name%%)');
const createRuleWithOptions = template('getRule(%%name%%, "off")');

export const match = ({options}) => ({
'module.exports.rules = __object': ({__object}) => {
Expand All @@ -20,6 +21,9 @@ export const match = ({options}) => ({
[`getRule('${name}')`]: () => {
exists = true;
},
[`getRule('${name}', 'off')`]: () => {
exists = true;
},
});

return !exists;
Expand All @@ -28,7 +32,17 @@ export const match = ({options}) => ({

export const replace = ({options}) => ({
'module.exports.rules = __object': ({__object}, path) => {
const {name} = options;
const {name, ruleOptions} = options;

if (ruleOptions) {
const node = SpreadElement(createRuleWithOptions({
name: StringLiteral(name),
}));

__object.properties.push(node);
return path;
}

const node = SpreadElement(createRule({
name: StringLiteral(name),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ test('redput: insert-get-rule: transform', (t) => {
});
t.end();
});

test('redput: insert-get-rule: transform: options', (t) => {
t.transformWithOptions('off', {
name: 'hello',
ruleOptions: 'off',
});
t.end();
});
4 changes: 2 additions & 2 deletions lib/write/nested/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export const writeNestedTests = async (name, report) => {
}));
};

export const updateNestedIndex = async (name) => {
export const updateNestedIndex = async (name, options) => {
const source = await readFile('./index.js', 'utf8');
const code = addRule(name, source);
const code = addRule(name, source, options);
await writeFile(`./index.js`, code);
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"octokit": "^3.1.0",
"putout": "^33.9.1",
"rendy": "^4.1.3",
"try-catch": "^3.0.1",
"try-to-catch": "^3.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit daae14d

Please sign in to comment.