Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created October 25, 2023 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retorquere/88fc56f2586444a35d25ac67af6268a1 to your computer and use it in GitHub Desktop.
Save retorquere/88fc56f2586444a35d25ac67af6268a1 to your computer and use it in GitHub Desktop.
'use strict';
const espree = require('espree');
module.exports = {
preprocess: function (text, filename) {
if (text[0] !== '{') return [{ text, filename }]
// detect header
const prefix = `const ZoteroTranslator${Date.now()} = `;
let ast = espree.parse(`${prefix}${text}`, { ecmaVersion: 2015 });
if (ast.body[0]?.declarations[0]?.init?.type !== 'ObjectExpression') throw new Error('No header in translator')
const headerLength = ast.body[0].end - prefix.length;
const re = new RegExp(`^([\\s\\S]{${headerLength}}[ \r\n]*)([\\s\\S]+?)(/[*][*] BEGIN TEST CASES [*][*]/[\\s\\S]*)?$`, 'i')
const [ , header, body, testcases ] = text.match(re)
const chunks = [
{ text: `(${header})`, filename: 'header.js', start: 0 },
{ text: body, filename: 'body.js', start: header.length },
]
if (testcases) {
chunks.push({ text: testcases, filename: 'tests.js', start: header.length + body.length })
}
return chunks
},
postprocess: function (messages, filename) {
console.log(messages.length)
console.log(messages.map(l => l.length))
messages = [].concat(...messages)
if (filename.match(/_(header|tests)[.]js$/)) {
return messages.filter(message => !['no-unused-vars', 'quote-props', 'no-unused-expressions'].includes(message.ruleId))
}
else {
return messages
}
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment