Skip to content

Commit

Permalink
Merge branch 'expressionfetch' of github.com:Item21/lowcode into widg…
Browse files Browse the repository at this point in the history
…etpropsexpressions
  • Loading branch information
Item21 committed Aug 17, 2021
2 parents f4a54f7 + e4234b2 commit 6112924
Show file tree
Hide file tree
Showing 40 changed files with 6,638 additions and 13,581 deletions.
10 changes: 7 additions & 3 deletions packages/react-lowcode/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module.exports = {
"transform": {
"^.+\\.tsx?$": "ts-jest"
"globals": {
"extensionsToTreatAsEsm": ['.ts', '.js'],
'ts-jest': {
useESM: true
}
},
"preset": 'ts-jest/presets/js-with-ts-esm',
"transformIgnorePatterns": [
"node_modules/(@iteria-app)"
'node_modules/(?!(@iteria-app)/)'
]
};
2,704 changes: 2,429 additions & 275 deletions packages/react-lowcode/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/react-lowcode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@
"refactor"
],
"author": "iteria",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@iteria-app/graphql-lowcode": "^0.1.3",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.20",
"@types/jest": "^26.0.24",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"code-block-writer": "^10.1.1",
Expand All @@ -64,13 +63,14 @@
"web-vitals": "^0.2.4"
},
"devDependencies": {
"@types/jest": "^26.0.20",
"@types/jest": "^26.0.24",
"@types/node": "^12.20.10",
"esbuild": "^0.8.34",
"esbuild-jest": "^0.3.0",
"esbuild-node-tsc": "^1.2.0",
"nodemon": "^2.0.7",
"ts-jest": "^26.4.4",
"jest": "^27.0.6",
"ts-jest": "^27.0.4",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
},
Expand Down
105 changes: 62 additions & 43 deletions packages/react-lowcode/src/ast/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,68 @@ export function createAst(
scriptTarget,
true,
scriptKind
)
const program = getBindingResult()
return program.getSourceFile(filePath)
);
const program = getBindingResult(filePath, sourceFile, scriptTarget);
return program.getSourceFile(filePath);
}

function getBindingResult() {
const options: CompilerOptions = {
strict: true,
target: scriptTarget,
allowJs: true,
module: ts.ModuleKind.ESNext,
jsx: ts.JsxEmit.React,
}
const files: { [name: string]: SourceFile | undefined } = {
[filePath]: sourceFile,
}
export function createProgram(
code: string,
scriptTarget = ScriptTarget.ESNext,
scriptKind = ScriptKind.TSX,
filePath = `/ts-ast-viewer.tsx`
) {
const sourceFile = ts.createSourceFile(
filePath,
code,
scriptTarget,
true,
scriptKind
);
const program = getBindingResult(filePath, sourceFile, scriptTarget);
return program;
}

const compilerHost: CompilerHost = {
getSourceFile: (
fileName: string /*, languageVersion: ScriptTarget, onError?: (message: string) => void*/
) => {
return files[fileName]
},
// getSourceFileByPath: (...) => {}, // not providing these will force it to use the file name as the file path
// getDefaultLibLocation: (...) => {},
getDefaultLibFileName: (/*defaultLibOptions: CompilerOptions*/) => "/lib",
writeFile: () => {
// do nothing
},
getCurrentDirectory: () => "/",
getDirectories: (/*path: string*/) => [],
fileExists: (fileName: string) => files[fileName] != null,
readFile: (fileName: string) =>
files[fileName] != null ? files[fileName]!.getFullText() : undefined,
getCanonicalFileName: (fileName: string) => fileName,
useCaseSensitiveFileNames: () => true,
getNewLine: () => "\n",
getEnvironmentVariable: () => "",
}
const program = ts.createProgram(
[...Object.keys(files)],
options,
compilerHost
)
return program
function getBindingResult(filePath: string, sourceFile: ts.SourceFile, scriptTarget: ScriptTarget) {
const options: CompilerOptions = {
strict: true,
target: scriptTarget,
allowJs: true,
module: ts.ModuleKind.ESNext,
jsx: ts.JsxEmit.React,
}
}
const files: { [name: string]: SourceFile | undefined } = {
[filePath]: sourceFile,
};

const compilerHost: CompilerHost = {
getSourceFile: (
fileName: string /*, languageVersion: ScriptTarget, onError?: (message: string) => void*/
) => {
return files[fileName]
},
// getSourceFileByPath: (...) => {}, // not providing these will force it to use the file name as the file path
// getDefaultLibLocation: (...) => {},
getDefaultLibFileName: (/*defaultLibOptions: CompilerOptions*/) => "/lib",
writeFile: () => {
// do nothing
},
getCurrentDirectory: () => "/",
getDirectories: (/*path: string*/) => [],
fileExists: (fileName: string) => files[fileName] != null,
readFile: (fileName: string) =>
files[fileName] != null ? files[fileName]!.getFullText() : undefined,
getCanonicalFileName: (fileName: string) => fileName,
useCaseSensitiveFileNames: () => true,
getNewLine: () => "\n",
getEnvironmentVariable: () => "",
};

const program = ts.createProgram(
[...Object.keys(files)],
options,
compilerHost
);

return program;
}
4 changes: 2 additions & 2 deletions packages/react-lowcode/src/ast/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const getAstAndNodeFromSource = (
return { node, ast }
}

export const findByCondition = <T>(node: Node | SourceFile, condition: (node: Node) => boolean): T | undefined => {
export const findByCondition = <T>(node: Node | SourceFile, condition: (node: Node) => boolean | undefined): T | undefined => {
if(condition(node)) {
return node as unknown as T;
};
Expand All @@ -112,7 +112,7 @@ export const findByCondition = <T>(node: Node | SourceFile, condition: (node: No
});
}

export const findAllByCondition = <T>(node: Node | SourceFile, output: T[], condition: (node: Node) => boolean): void => {
export const findAllByCondition = <T>(node: Node | SourceFile, output: T[], condition: (node: Node) => boolean | undefined): void => {
if(condition(node)) {
output.push(node as unknown as T);
};
Expand Down
8 changes: 3 additions & 5 deletions packages/react-lowcode/src/ast/identifierLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { SourceFile } from "ts-morph"

export const getJsxIdentifierLength = (code: string, source: SourceLineCol) => {
const pos = startOfJsxIdentifier(code, source)
if (pos) {
const node = astFindStart(code, pos)
if (!node) throw new Error("Unable to find node in AST")
return node.end - node.pos
}
const node = astFindStart(code, pos!)
if (!node) throw new Error("Unable to find node in AST")
return node.end - node.pos
}
Loading

0 comments on commit 6112924

Please sign in to comment.