Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tyb-talks committed Jul 17, 2024
1 parent d535f02 commit 39ab8e7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 162 deletions.
4 changes: 0 additions & 4 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-syntax-jsx",
// "@babel/plugin-syntax-typescript",
["@babel/plugin-proposal-decorators", { "version": "2023-11" }],
"babel-plugin-ember-template-compilation",
// "ember-template-imports/lib/babel-plugin"
]
}
90 changes: 0 additions & 90 deletions core_extracted_ids.txt

This file was deleted.

6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
"private": true,
"devDependencies": {
"@babel/plugin-proposal-decorators": "^7.24.7",
"@babel/plugin-syntax-jsx": "^7.24.7",
"@discourse/lint-configs": "1.3.9",
"babel-plugin-ember-template-compilation": "^2.2.5",
"content-tag": "^2.0.1",
"ember-template-lint": "6.0.0",
"eslint": "8.57.0",
"prettier": "2.8.8",
"update-browserslist-db": "^1.1.0"
},
"dependencies": {
"update-browserslist-db": "^1.1.0",
"@babel/cli": "^7.24.8",
"@babel/core": "^7.24.8",
"@babel/parser": "^7.24.8",
Expand Down
88 changes: 45 additions & 43 deletions scripts/update_core_discourse_deprecations.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import fs from "fs";
import path from "path";
import yaml from "js-yaml";
import parser from "@babel/parser";
import _traverse from "@babel/traverse";
import * as t from "@babel/types";
import { promisify } from "util";
import { Preprocessor } from "content-tag";

const DEBUG_FILE =
"/Users/kelvintyb/code/discourse/app/assets/javascripts/discourse/app/instance-initializers/discovery-controller-shims.js";

const traverse = _traverse.default;
const readdir = promisify(fs.readdir);
const readFile = promisify(fs.readFile);
Expand All @@ -31,29 +29,29 @@ async function isExcludedDir(filePath) {
return EXCLUDED_DIR_PATTERNS.some((pattern) => filePath.includes(pattern));
}

function extractId(node, scope, shouldLog = false, ast = null) {
function extractId(node, scope, ast = null) {
if (t.isObjectExpression(node)) {
for (const prop of node.properties) {
if (
t.isObjectProperty(prop) &&
t.isIdentifier(prop.key, {name: "id"})
t.isIdentifier(prop.key, { name: "id" })
) {
if (t.isStringLiteral(prop.value)) {
return [prop.value.value];
} else if (t.isIdentifier(prop.value)) {
// discovery-controller-shim is here
return resolveIdentifier(prop.value.name, scope, shouldLog, ast);
return resolveIdentifier(prop.value.name, scope, ast);
}
}
}
}
return [];
}

function resolveIdentifier(name, scope, shouldLog = false, ast = null) {
function resolveIdentifier(name, scope, ast = null) {
const binding = scope.getBinding(name);
if (!binding) {
return []
return [];
}

if (t.isVariableDeclarator(binding.path.node)) {
Expand All @@ -72,37 +70,26 @@ function resolveIdentifier(name, scope, shouldLog = false, ast = null) {
}
}
if (t.isIdentifier(binding.path.node) && binding.kind === "param") {
if (shouldLog) {
let argIndex;
const calleeFunctionName = binding
.path
.findParent(p => {
if (p.isFunctionDeclaration()) {
const matchingArg = p.node.params.find(p => p.name === name)
if (matchingArg) {
argIndex = p.node.params.findIndex(p => p.name === name)
return true
}
}
})
.node
.id.name
let argIndex;
const calleeFunctionName = binding.path.findParent((p) => {
if (p.isFunctionDeclaration()) {
const matchingArg = p.node.params.find((p) => p.name === name);
if (matchingArg) {
argIndex = p.node.params.findIndex((p) => p.name === name);
return true;
}
}
})?.node?.id?.name;

return traverseForDeprecationId(ast, name, calleeFunctionName, argIndex, shouldLog)
}
return traverseForDeprecationId(ast, name, calleeFunctionName, argIndex);
}
return [];
}

async function parseFile(filePath) {
let shouldLog = false;
let hasDeprecatedFunction = false;
let code = await readFile(filePath, "utf8");

if (filePath == DEBUG_FILE) {
shouldLog = true;
}

try {
if (filePath.endsWith(".gjs")) {
code = GJSPreprocessor.process(code);
Expand All @@ -121,7 +108,7 @@ async function parseFile(filePath) {
hasDeprecatedFunction = true;
for (const arg of path.node.arguments) {
if (t.isObjectExpression(arg)) {
const extractedIds = extractId(arg, path.scope, shouldLog, ast);
const extractedIds = extractId(arg, path.scope, ast);
if (extractedIds) {
ids.push(...extractedIds);
}
Expand Down Expand Up @@ -184,21 +171,24 @@ async function parseDirectory(directoryPath) {
return ids;
}

function traverseForDeprecationId(ast, deprecationIdName, calleeFunctionName, argIndex, shouldLog) {
function traverseForDeprecationId(
ast,
deprecationIdName,
calleeFunctionName,
argIndex
) {
const ids = [];
if (!ast) {
return null
}
if (shouldLog) {
console.log(deprecationIdName)
return null;
}

traverse(ast, {
CallExpression(path) {
if (t.isIdentifier(path.node.callee, { name: calleeFunctionName })) {
console.dir(path.node.arguments[argIndex])
const id = path.node.arguments[argIndex].value;
if (id) { ids.push(id) }
if (id) {
ids.push(id);
}
//TODO else raise a missing id error
}
},
Expand All @@ -207,7 +197,6 @@ function traverseForDeprecationId(ast, deprecationIdName, calleeFunctionName, ar
return ids;
}


// Main script
(async () => {
if (process.argv.length < 3) {
Expand All @@ -218,8 +207,21 @@ function traverseForDeprecationId(ast, deprecationIdName, calleeFunctionName, ar
const directoryPath = process.argv[2];
const ids = [...new Set(await parseDirectory(directoryPath))].sort();

// Save the extracted ids to a text file
fs.writeFileSync("core_extracted_ids.txt", ids.join("\n"), "utf8");
console.log(`${ids.length} Extracted IDs saved to core_extracted_ids.txt`);
// const yamlFilePath = path.join(__dirname, '..', 'lib', 'deprecation_collector', 'deprecation-ids.yml');
const deprecationIdsFilePath = path.join(
".",
"lib",
"deprecation_collector",
"deprecation-ids.yml"
);
const deprecationIds = yaml.load(
fs.readFileSync(deprecationIdsFilePath, "utf8")
);
deprecationIds["discourse_deprecation_ids"] = ids;
fs.writeFileSync(
deprecationIdsFilePath,
yaml.dump(deprecationIds, { "---": true, noArrayIndent: true }),
"utf8"
);
console.log(`${ids.length} Extracted IDs saved to ${deprecationIdsFilePath}`);
})();

20 changes: 0 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,6 @@
dependencies:
"@babel/helper-plugin-utils" "^7.8.0"

"@babel/plugin-syntax-jsx@^7.24.7":
version "7.24.7"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-jsx/-/plugin-syntax-jsx-7.24.7.tgz#39a1fa4a7e3d3d7f34e2acc6be585b718d30e02d"
integrity sha512-6ddciUPe/mpMnOKv/U+RSd2vvVy+Yw/JfBB0ZHYjEZt9NLHmCUylNYlsbqCCS1Bffjlb0fCwC9Vqz+sBz6PsiQ==
dependencies:
"@babel/helper-plugin-utils" "^7.24.7"

"@babel/plugin-syntax-logical-assignment-operators@^7.10.4":
version "7.10.4"
resolved "https://registry.npmjs.org/@babel/plugin-syntax-logical-assignment-operators/-/plugin-syntax-logical-assignment-operators-7.10.4.tgz"
Expand Down Expand Up @@ -1549,11 +1542,6 @@ babel-import-util@^2.0.0:
resolved "https://registry.npmjs.org/babel-import-util/-/babel-import-util-2.0.1.tgz"
integrity sha512-N1ZfNprtf/37x0R05J0QCW/9pCAcuI+bjZIK9tlu0JEkwEST7ssdD++gxHRbD58AiG5QE5OuNYhRoEFsc1wESw==

babel-import-util@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/babel-import-util/-/babel-import-util-3.0.0.tgz#5814c6a58e7b80e64156b48fdfd34d48e6e0b1df"
integrity sha512-4YNPkuVsxAW5lnSTa6cn4Wk49RX6GAB6vX+M6LqEtN0YePqoFczv1/x0EyLK/o+4E1j9jEuYj5Su7IEPab5JHQ==

babel-plugin-ember-modules-api-polyfill@^3.5.0:
version "3.5.0"
resolved "https://registry.npmjs.org/babel-plugin-ember-modules-api-polyfill/-/babel-plugin-ember-modules-api-polyfill-3.5.0.tgz"
Expand All @@ -1569,14 +1557,6 @@ babel-plugin-ember-template-compilation@^2.0.0:
"@glimmer/syntax" "^0.84.3"
babel-import-util "^2.0.0"

babel-plugin-ember-template-compilation@^2.2.5:
version "2.2.5"
resolved "https://registry.yarnpkg.com/babel-plugin-ember-template-compilation/-/babel-plugin-ember-template-compilation-2.2.5.tgz#9f00cc88eeefdc7d228cfac63c2a40e59691fbf3"
integrity sha512-NQ2DT0DsYyHVrEpFQIy2U8S91JaKSE8NOSZzMd7KZFJVgA6KodJq3Uj852HcH9LsSfvwppnM+dRo1G8bzTnnFw==
dependencies:
"@glimmer/syntax" "^0.84.3"
babel-import-util "^3.0.0"

babel-plugin-htmlbars-inline-precompile@^5.3.0:
version "5.3.1"
resolved "https://registry.npmjs.org/babel-plugin-htmlbars-inline-precompile/-/babel-plugin-htmlbars-inline-precompile-5.3.1.tgz"
Expand Down

0 comments on commit 39ab8e7

Please sign in to comment.