Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add astx transformer #683

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ developers who want to create AST transforms. In fact, following transformers
are included so you can prototype your own plugins:

- JavaScript
- [astx][]
- [babel][] (v5, v6)
- [ESLint][] (v1, v2, v3)
- [jscodeshift][]
Expand Down Expand Up @@ -138,6 +139,7 @@ are included so you can prototype your own plugins:
[acorn]: https://github.com/ternjs/acorn
[@angular/compiler]: https://angular.io/
[@angular-eslint/template-parser]: https://github.com/angular-eslint/angular-eslint/tree/master/packages/template-parser
[astx]: https://github.com/codemodsquad/astx
[babel-eslint]: https://github.com/babel/babel-eslint
[babel]: https://babeljs.io/docs/advanced/plugins/
[babylon]: https://babeljs.io/
Expand Down Expand Up @@ -183,7 +185,7 @@ are included so you can prototype your own plugins:
[handlebars]: http://handlebarsjs.com/
[icu]: https://formatjs.io/docs/intl-messageformat-parser/
[json]: https://github.com/vtrushin/json-to-ast
[Momoa]: https://github.com/humanwhocodes/momoa
[momoa]: https://github.com/humanwhocodes/momoa
[sqlite-parser]: https://github.com/codeschool/sqlite-parser
[yaml]: https://github.com/eemeli/yaml
[yaml-ast-parser]: https://github.com/mulesoft-labs/yaml-ast-parser
Expand Down
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"astexplorer-go": "^1.0.0",
"astexplorer-refmt": "^1.0.1",
"astexplorer-syn": "^1.0.48",
"astx": "^2.1.2",
"babel-core": "npm:babel-core@^6",
"babel-eslint": "^7.2.3",
"babel-eslint8": "npm:babel-eslint@^8",
Expand Down
14 changes: 14 additions & 0 deletions website/src/parsers/js/transformers/astx/codeExample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// learn more at https://github.com/codemodsquad/astx

exports.astx = ({astx}) => {
astx.find`
function $f($$args) {
$$body
}
`().replace`
function $f($$args) {
console.log('calling', $f, [$$args]);
$$body
}
`()
}
130 changes: 130 additions & 0 deletions website/src/parsers/js/transformers/astx/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import compileModule from '../../../utils/compileModule';
import pkg from 'astx/package.json';

const ID = 'astx';

export default {
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage || 'https://github.com/codemodsquad/astx',

defaultParserID: 'babylon7',
compatibleParserIDs: new Set(['recast', 'babylon7']),

formatCodeExample(codeExample) {
return codeExample;
},

loadTransformer(callback) {
require([
'../../../transpilers/babel',
'astx/cjs/Astx',
'astx/cjs/babel/BabelBackend',
'astx/cjs/recast/RecastBackend',
'astx/cjs/util/CodeFrameError',
'@babel/parser',
], (
transpile,
Astx,
BabelBackend,
RecastBackend,
CodeFrameError,
babelParser
) => {
callback({
transpile: transpile.default,
Astx: Astx.default,
BabelBackend: BabelBackend.default,
RecastBackend: RecastBackend.default,
CodeFrameError: CodeFrameError.default,
babelParser,
});
});
},

async transform(props, transformCode, source, { parser, parserSettings }) {
const {
transpile,
Astx,
BabelBackend,
RecastBackend,
CodeFrameError,
babelParser,
} = props;
const file = 'file.js';
try {
transformCode = transpile(transformCode);
const transform = compileModule(transformCode);

let transformFn = transform.astx;

const { find, replace } = transform;
if (typeof transformFn !== 'function' && find) {
transformFn = ({ astx }) => {
const result = astx.find(find, { where: transform.where });
if (replace) result.replace(replace);
if (!result.size) return null;
};
}
if (typeof transformFn === 'function') {
const backend =
parser === 'recast'
? new RecastBackend({
wrapped: new BabelBackend({
parserOptions: parserSettings.babylon7,
}),
parseOptions: { ...parserSettings },
})
: new BabelBackend({
parserOptions: parser === 'babylon7' ? parserSettings : null,
});
const { t } = backend;

let ast, root;
try {
ast = backend.parse(source);
root = new backend.t.NodePath(ast);
} catch (error) {
if (error instanceof Error) {
CodeFrameError.rethrow(error, { filename: file, source });
}
throw error;
}

const astx = new Astx(backend, [root]);

const options = {
source,
file,
root,
t,
report: (msg) => {
transform.onReport?.({ file, report: msg });
console.log('astx report:', msg);
},
...backend.template,
astx,
};

const _result = await transformFn(options);
let transformed;

if (transform.astx || transform.replace) {
transformed = _result;
if (transformed === undefined) {
transformed = backend.generate(ast).code;
}
if (transformed === null) transformed = undefined;
}

return transformed;
}
} catch (error) {
if (error instanceof CodeFrameError) {
throw new Error(error.format({ highlightCode: true }));
}
throw error;
}
},
};
40 changes: 29 additions & 11 deletions website/src/store/transformerMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {getTransformer, getTransformCode, getCode, showTransformer} from './selectors';
import {SourceMapConsumer} from 'source-map/lib/source-map-consumer';
import {
getTransformer,
getTransformCode,
getCode,
showTransformer,
} from './selectors';
import { SourceMapConsumer } from 'source-map/lib/source-map-consumer';
import isEqual from 'lodash.isequal';

async function transform(transformer, transformCode, code) {
async function transform(transformer, transformCode, code, options) {
// Transforms may make use of Node's __filename global. See GitHub issue #420.
// So we define a dummy one.
if (!global.__filename) {
Expand All @@ -13,7 +19,12 @@ async function transform(transformer, transformCode, code) {
let realTransformer;
try {
realTransformer = await transformer._promise;
let result = await transformer.transform(realTransformer, transformCode, code);
let result = await transformer.transform(
realTransformer,
transformCode,
code,
options
);
let map = null;
if (typeof result !== 'string') {
if (result.map) {
Expand All @@ -22,23 +33,23 @@ async function transform(transformer, transformCode, code) {
result = result.code;
}
return { result, map, version: realTransformer.version, error: null };
} catch(error) {
} catch (error) {
return {
error,
version: realTransformer ? realTransformer.version : '',
};
}
}

export default store => next => async (action) => {
export default (store) => (next) => async (action) => {
const oldState = store.getState();
next(action);
const newState = store.getState();

const show = showTransformer(newState);

if (!show) {
return
return;
}

const newTransformer = getTransformer(newState);
Expand All @@ -50,7 +61,9 @@ export default store => next => async (action) => {
show != showTransformer(oldState) ||
getTransformer(oldState) !== newTransformer ||
getTransformCode(oldState) !== newTransformCode ||
getCode(oldState) !== newCode
getCode(oldState) !== newCode ||
oldState.parser !== newState.parser ||
!isEqual(oldState.parserSettings, newState.parserSettings)
) {
if (!newTransformer || newCode == null) {
return;
Expand All @@ -60,11 +73,16 @@ export default store => next => async (action) => {
console.clear();
}

const { parser, parserSettings } = newState.workbench;

let result;
try {
result = await transform(newTransformer, newTransformCode, newCode);
try {
result = await transform(newTransformer, newTransformCode, newCode, {
parser,
parserSettings,
});
} catch (error) {
result = {error}
result = { error };
}

// Did anything change in the meantime?
Expand Down
Loading