Skip to content

Commit

Permalink
Strip typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Apr 22, 2022
1 parent 2d6df00 commit f386846
Show file tree
Hide file tree
Showing 72 changed files with 146 additions and 532 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/coverage/
/dist/
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ jobs:
node-version: 12.x
cache: yarn
- run: yarn install --frozen-lockfile
- run: yarn prepublishOnly
- run: yarn checkFormat
- run: yarn lint

Expand Down Expand Up @@ -79,7 +78,6 @@ jobs:
node-version: 12.x
cache: yarn
- run: yarn install --frozen-lockfile
- run: yarn run prepublishOnly
- run: yarn pack
# Windows runners don't have /tmp, let's make sure it exists
- run: mkdir -p /tmp
Expand Down Expand Up @@ -112,7 +110,6 @@ jobs:
node-version: 12.x
cache: yarn
- run: yarn install --frozen-lockfile
- run: yarn prepublishOnly
- run: gem build -o prettier.gem
- run: gem unpack prettier.gem
- run: prettier/exe/rbprettier --help
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
/test.rbs
/test.haml
*.gem
/dist/

# This is to better support the GitHub actions checking - since bundler changes
# to being shipped by default with Ruby starting on 2.6.
Expand Down
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/.bundle/
/coverage/
/dist/
/pkg/
/playground/
/vendor/
Expand Down
11 changes: 5 additions & 6 deletions bin/doc.ts → bin/doc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!./node_modules/.bin/ts-node
#!/usr/bin/env node

import fs from "fs";
import prettier from "prettier";
const fs = require("fs");
const prettier = require("prettier");
const plugin = require("../src/plugin");

import plugin from "../src/plugin";

const debug = (prettier as any).__debug;
const debug = prettier.__debug;

const code = fs.existsSync(process.argv[2])
? fs.readFileSync(process.argv[2], "utf-8")
Expand Down
6 changes: 6 additions & 0 deletions bin/pragma
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

const fs = require("fs");
const parser = require("../src/ruby/parser");

console.log(parser.hasPragma(fs.readFileSync(process.argv[2], "utf-8")));
6 changes: 0 additions & 6 deletions bin/pragma.ts

This file was deleted.

12 changes: 6 additions & 6 deletions bin/print.ts → bin/print
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!./node_modules/.bin/ts-node
#!/usr/bin/env node

import fs from "fs";
import prettier from "prettier";
const fs = require("fs");
const prettier = require("prettier");

import plugin from "../src/plugin";
const plugin = require("../src/plugin");

let parser = "ruby";
let contentIdx = 2;
Expand All @@ -26,8 +26,8 @@ if (fs.existsSync(process.argv[contentIdx])) {

const { formatted } = prettier.formatWithCursor(content, {
parser,
plugins: [plugin as any as string], // hacky, but it works
plugins: [plugin],
cursorOffset: 1
} as any);
});

console.log(formatted);
4 changes: 2 additions & 2 deletions lib/prettier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def self.run(args)
either install them yourself by cd-ing into the directory where this gem
is located (#{File.expand_path("..", __dir__)}) and running:
`yarn && yarn prepublishOnly`
`yarn install`
or
`npm install && npm run prepublishOnly`
`npm install`
or
you can change the source in your Gemfile to point directly to rubygems.
MSG
Expand Down
28 changes: 7 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"name": "@prettier/plugin-ruby",
"version": "2.1.0",
"description": "prettier plugin for the Ruby programming language",
"main": "dist/plugin.js",
"main": "src/plugin.js",
"scripts": {
"checkFormat": "prettier --check '**/*'",
"lint": "eslint --cache .",
"prepublishOnly": "tsc -p tsconfig.build.json && cp src/getInfo.js dist && cp src/netcat.js dist && cp src/server.rb dist",
"test": "jest"
},
"repository": {
Expand All @@ -23,46 +22,33 @@
"prettier": ">=2.3.0"
},
"devDependencies": {
"@types/jest": "^27.0.1",
"@types/node": "^17.0.0",
"@types/prettier": "^2.4.2",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.0.0",
"husky": "^7.0.0",
"jest": "^27.0.1",
"pretty-quick": "^3.1.2",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"typescript": "^4.5.2"
"pretty-quick": "^3.1.2"
},
"eslintConfig": {
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"env": {
"es6": true,
"jest": true,
"node": true
},
"parser": "@typescript-eslint/parser",
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off"
"no-unused-vars": "off"
}
},
"jest": {
"globalSetup": "./test/js/globalSetup.ts",
"globalTeardown": "./test/js/globalTeardown.ts",
"preset": "ts-jest",
"globalSetup": "./test/js/globalSetup.js",
"globalTeardown": "./test/js/globalTeardown.js",
"setupFilesAfterEnv": [
"./test/js/setupTests.ts"
"./test/js/setupTests.js"
],
"testRegex": ".test.ts$"
"testRegex": ".test.js$"
},
"husky": {
"hooks": {
Expand Down
2 changes: 1 addition & 1 deletion prettier.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
spec.files =
Dir.chdir(__dir__) do
%w[LICENSE bin/console package.json rubocop.yml] +
Dir["{{exe,lib,dist}/**/*,*.md}"] +
Dir["{{exe,lib,src}/**/*,*.md}"] +
Dir[
"node_modules/prettier/{package.json,index.js,cli.js,doc.js,bin-prettier.js,third-party.js,parser-*.js}"
]
Expand Down
1 change: 0 additions & 1 deletion src/getInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { existsSync, readFileSync } = require("fs");

// This is how long to wait for the parser to spin up. For the most part, 5
Expand Down
1 change: 0 additions & 1 deletion src/netcat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// On average, this is 2-3x slower than netcat, but still much faster than
// spawning a new Ruby process.

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { createConnection } = require("net");

const sock = process.argv[process.argv.length - 1];
Expand Down
37 changes: 18 additions & 19 deletions src/parseSync.ts → src/parseSync.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { spawn, spawnSync } from "child_process";
import {
const { spawn, spawnSync } = require("child_process");
const {
existsSync,
unlinkSync,
mkdtempSync,
copyFileSync,
mkdirSync,
rmdirSync
} from "fs";
import os from "os";
import path from "path";
import process from "process";
} = require("fs");
const os = require("os");
const path = require("path");
const process = require("process");

type ParserArgs = { cmd: string; args: string[] };
let parserArgs: undefined | ParserArgs;
let parserArgs;

if (process.env.PRETTIER_RUBY_HOST) {
const [cmd, ...args] = process.env.PRETTIER_RUBY_HOST.split(" ");
Expand All @@ -23,7 +22,7 @@ if (process.env.PRETTIER_RUBY_HOST) {
// parse using UTF-8. Unfortunately, the way that you accomplish this looks
// differently depending on your platform.
/* istanbul ignore next */
export function getLang() {
function getLang() {
const { env, platform } = process;
const envValue = env.LC_ALL || env.LC_CTYPE || env.LANG;

Expand Down Expand Up @@ -56,21 +55,21 @@ export function getLang() {

// Generate the filepath that should be used to communicate the connection
// information between this process and the parser server.
export function getInfoFilepath() {
function getInfoFilepath() {
return path.join(os.tmpdir(), `prettier-ruby-parser-${process.pid}.info`);
}

// Create a file that will act as a communication mechanism, spawn a parser
// server with that filepath as an argument, then spawn another process that
// will read that information in order to enable us to connect to it in the
// spawnSync function.
function spawnServer(): ParserArgs {
function spawnServer() {
const tempDir = mkdtempSync(path.join(os.tmpdir(), "prettier-plugin-ruby-"));
const filepath = getInfoFilepath();

let serverRbPath = path.join(__dirname, "./server.rb");
let getInfoJsPath = path.join(__dirname, "./getInfo.js");
let cleanupTempFiles: () => void | undefined;
let cleanupTempFiles;

if (runningInPnPZip()) {
// If we're running in a Yarn PnP environment inside a ZIP file, it's not possible to run
Expand Down Expand Up @@ -164,15 +163,11 @@ function runningInPnPZip() {
return process.versions.pnp && __dirname.includes(".zip");
}

// You can optionally return location information from the source string when
// raising an error that prettier will handle for you nicely.
type LocatedError = Error & { loc?: any };

// Formats and sends a request to the parser server. We use netcat (or something
// like it) here since Prettier requires the results of `parse` to be
// synchronous and Node.js does not offer a mechanism for synchronous socket
// requests.
function parseSync(parser: string, source: string) {
function parseSync(parser, source) {
if (!parserArgs) {
parserArgs = spawnServer();
}
Expand All @@ -195,7 +190,7 @@ function parseSync(parser: string, source: string) {
const parsed = JSON.parse(stdout);

if (parsed.error) {
const error: LocatedError = new Error(parsed.error);
const error = new Error(parsed.error);
if (parsed.loc) {
error.loc = parsed.loc;
}
Expand All @@ -206,4 +201,8 @@ function parseSync(parser: string, source: string) {
return parsed;
}

export default parseSync;
module.exports = {
getLang,
getInfoFilepath,
parseSync
};
15 changes: 3 additions & 12 deletions src/plugin.ts → src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import type { Plugin, SupportLanguage } from "prettier";
import parseSync from "./parseSync";

interface ExtendedSupportLanguage extends SupportLanguage {
interpreters?: string[];
}

interface ExtendedPlugin extends Omit<Plugin, "languages"> {
languages: ExtendedSupportLanguage[];
}
const { parseSync } = require("./parseSync");

/*
* metadata mostly pulled from linguist and rubocop:
* https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
* https://github.com/rubocop/rubocop/blob/master/spec/rubocop/target_finder_spec.rb
*/
const plugin: ExtendedPlugin = {
const plugin = {
languages: [
{
name: "Ruby",
Expand Down Expand Up @@ -167,4 +158,4 @@ const plugin: ExtendedPlugin = {
}
};

export = plugin;
module.exports = plugin;
11 changes: 5 additions & 6 deletions test/js/files.test.ts → test/js/files.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import path from "path";
import { FileInfoOptions, getFileInfo } from "prettier";
const path = require("path");
const prettier = require("prettier");

// eslint-disable-next-line @typescript-eslint/no-var-requires
const plugin = require("../../src/plugin");

function getInferredParser(filename: string) {
function getInferredParser(filename) {
const filepath = path.join(__dirname, filename);
const fileInfoOptions = { plugins: [plugin] } as any as FileInfoOptions;
const fileInfoOptions = { plugins: [plugin] };

return getFileInfo(filepath, fileInfoOptions).then(
return prettier.getFileInfo(filepath, fileInfoOptions).then(
({ inferredParser }) => inferredParser
);
}
Expand Down
10 changes: 5 additions & 5 deletions test/js/globalSetup.ts → test/js/globalSetup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { spawn, spawnSync } from "child_process";
import { unlinkSync } from "fs";
import path from "path";
const { spawn, spawnSync } = require("child_process");
const { unlinkSync } = require("fs");
const path = require("path");

import { getLang, getInfoFilepath } from "../../src/parseSync";
const { getLang, getInfoFilepath } = require("../../src/parseSync");

// This is somewhat similar to the spawnServer function in parseSync but
// slightly different in that it logs its information into environment variables
Expand Down Expand Up @@ -42,4 +42,4 @@ function globalSetup() {
server.unref();
}

export default globalSetup;
module.exports = globalSetup;
2 changes: 1 addition & 1 deletion test/js/globalTeardown.ts → test/js/globalTeardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ function globalTeardown() {
}
}

export default globalTeardown;
module.exports = globalTeardown;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { haml } from "../utils";
const { haml } = require("../utils");

describe("comment", () => {
test("single line", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { haml } from "../utils";
const { haml } = require("../utils");

describe("doctype", () => {
test("basic", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { haml } from "../utils";
const { haml } = require("../utils");

describe("filter", () => {
test("self", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { haml } from "../utils";
const { haml } = require("../utils");

describe("haml comment", () => {
test("empty", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/js/haml/plain.test.ts → test/js/haml/plain.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { haml } from "../utils";
const { haml } = require("../utils");

describe("plain", () => {
const specialChars = ["%", ".", "#", "/", "!", "=", "&", "~", "-", "\\", ":"];
Expand Down
Loading

0 comments on commit f386846

Please sign in to comment.