diff --git a/lib/parse-plugin.js b/lib/parse-plugin.js index fc5ba9d..02b9c50 100644 --- a/lib/parse-plugin.js +++ b/lib/parse-plugin.js @@ -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]]; +} diff --git a/lib/parse-plugin.spec.js b/lib/parse-plugin.spec.js index b3e3ee8..25f1fb3 100644 --- a/lib/parse-plugin.spec.js +++ b/lib/parse-plugin.spec.js @@ -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(); +}); + diff --git a/lib/redput.js b/lib/redput.js index 60a6b73..6299c51 100644 --- a/lib/redput.js +++ b/lib/redput.js @@ -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, diff --git a/lib/write/index.js b/lib/write/index.js index ff5ef90..d573b27 100644 --- a/lib/write/index.js +++ b/lib/write/index.js @@ -14,7 +14,7 @@ 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) @@ -22,6 +22,7 @@ export const writePlugin = async (name, {rule, fixture, report}) => { rule, fixture, report, + options, }); await writeSimple(name, { @@ -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); }; diff --git a/lib/write/nested/add-rule/add-rule.js b/lib/write/nested/add-rule/add-rule.js index b717b06..77f403e 100644 --- a/lib/write/nested/add-rule/add-rule.js +++ b/lib/write/nested/add-rule/add-rule.js @@ -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: [ diff --git a/lib/write/nested/add-rule/plugin-insert-get-rule/fixture/off-fix.js b/lib/write/nested/add-rule/plugin-insert-get-rule/fixture/off-fix.js new file mode 100644 index 0000000..a4fdc00 --- /dev/null +++ b/lib/write/nested/add-rule/plugin-insert-get-rule/fixture/off-fix.js @@ -0,0 +1,8 @@ +const getRule = (a) => ({ + [a]: require(`./${a}`), +}); + +module.exports.rules = { + ...getRule('add-function', 'off'), + ...getRule('hello', 'off'), +}; diff --git a/lib/write/nested/add-rule/plugin-insert-get-rule/fixture/off.js b/lib/write/nested/add-rule/plugin-insert-get-rule/fixture/off.js new file mode 100644 index 0000000..01d342b --- /dev/null +++ b/lib/write/nested/add-rule/plugin-insert-get-rule/fixture/off.js @@ -0,0 +1,7 @@ +const getRule = (a) => ({ + [a]: require(`./${a}`), +}); + +module.exports.rules = { + ...getRule('add-function', 'off'), +}; diff --git a/lib/write/nested/add-rule/plugin-insert-get-rule/index.js b/lib/write/nested/add-rule/plugin-insert-get-rule/index.js index 8fbfee5..a03e14d 100644 --- a/lib/write/nested/add-rule/plugin-insert-get-rule/index.js +++ b/lib/write/nested/add-rule/plugin-insert-get-rule/index.js @@ -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}) => { @@ -20,6 +21,9 @@ export const match = ({options}) => ({ [`getRule('${name}')`]: () => { exists = true; }, + [`getRule('${name}', 'off')`]: () => { + exists = true; + }, }); return !exists; @@ -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), })); diff --git a/lib/write/nested/add-rule/plugin-insert-get-rule/index.spec.js b/lib/write/nested/add-rule/plugin-insert-get-rule/index.spec.js index f1ae28b..8abef66 100644 --- a/lib/write/nested/add-rule/plugin-insert-get-rule/index.spec.js +++ b/lib/write/nested/add-rule/plugin-insert-get-rule/index.spec.js @@ -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(); +}); diff --git a/lib/write/nested/nested.js b/lib/write/nested/nested.js index 5155589..288f2f1 100644 --- a/lib/write/nested/nested.js +++ b/lib/write/nested/nested.js @@ -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); }; diff --git a/package.json b/package.json index 40d1a45..1e50d15 100644 --- a/package.json +++ b/package.json @@ -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": {