Skip to content

Instantly share code, notes, and snippets.

@zenparsing
Created February 1, 2019 18:50
Show Gist options
  • Save zenparsing/29ff706ac4c49bdd87b317ce6d4ccce0 to your computer and use it in GitHub Desktop.
Save zenparsing/29ff706ac4c49bdd87b317ce6d4ccce0 to your computer and use it in GitHub Desktop.
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