Skip to content

Instantly share code, notes, and snippets.

@zenparsing zenparsing/a.js
Created Feb 1, 2019

Embed
What would you like to do?
Built-time macros strawman
import { deprecated } from './macros/deprecated.js';
// Using Rust-like syntax only to make it clear these
// are macros and not "decorators"
#[deprecated]
function dontUseMeAnymore() {
eval('You wrote a bad song, Petey!');
}
/*
Built-time tooling (e.g. a Webpack plugin) would
transform this into:
function dontUseMeAnymore() {
console.warn('This function is deprecated');
eval('You wrote a bad song Petey!');
}
*/
export function deprecated(ast) {
// In practice, macros would use a library to give
// AST mutation and construction better DX
ast.body.statements.unshift({
type: 'ExpressionStatement',
expression: {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'Identifier',
value: 'console',
},
property: {
type: 'Identifier',
value: 'warn',
}
},
arguments: [{
type: 'StringLiteral',
value: 'This function is deprecated',
}],
},
});
return ast;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.