js-compiler: Add basic lint tests
ADD
packages/@romejs/js-compiler/__rtests__/__rsnapshots__/lint.ts.rsnap
+{
+ "empty file": "Object {\n diagnostics: Array []\n src: ''\n ast: Program {\n body: Array []\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 0\n index: 0\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n }\n}",
+ "undeclared variable": "Object {\n src: 'foobar;'\n diagnostics: Array [\n Object {\n category: 'lint/undeclaredVariables'\n filename: 'unknown'\n language: 'js'\n message: 'Undeclared variable <emphasis>foobar</emphasis>'\n mtime: undefined\n sourceType: 'module'\n origins: Array [Object {category: 'lint'}]\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n ]\n ast: Program {\n comments: Array []\n diagnostics: Array []\n directives: Array []\n filename: 'unknown'\n hasHoistedVars: false\n interpreter: undefined\n mtime: undefined\n sourceType: 'module'\n syntax: Array []\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 7\n index: 7\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n body: Array [\n ExpressionStatement {\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 7\n index: 7\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n expression: ReferenceIdentifier {\n name: 'foobar'\n loc: Object {\n filename: 'unknown'\n end: Object {\n column: 6\n index: 6\n line: 1\n }\n start: Object {\n column: 0\n index: 0\n line: 1\n }\n }\n }\n }\n ]\n }\n}"
+}
\ No newline at end of file

ADD
packages/@romejs/js-compiler/__rtests__/lint.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import test from '@romejs/test';
+import lint from '../api/lint';
+import {parseJS} from '@romejs/js-parser';
+import {createUnknownFilePath} from '@romejs/path';
+import {DEFAULT_PROJECT_CONFIG, ProjectConfig} from '@romejs/project';
+
+const LINT_ENABLED_FORMAT_DISABLED_CONFIG: ProjectConfig = {
+ ...DEFAULT_PROJECT_CONFIG,
+ lint: {
+ ...DEFAULT_PROJECT_CONFIG.lint,
+ enabled: true,
+ },
+ format: {
+ ...DEFAULT_PROJECT_CONFIG.format,
+ enabled: false,
+ },
+};
+
+const LINT_AND_FORMAT_ENABLED_CONFIG: ProjectConfig = {
+ ...LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ format: {
+ ...LINT_ENABLED_FORMAT_DISABLED_CONFIG.format,
+ enabled: true,
+ },
+};
+
+async function testLint(input: string, config: ProjectConfig) {
+ return await lint({
+ options: {},
+ ast: parseJS({
+ input,
+ sourceType: 'module',
+ path: createUnknownFilePath('unknown'),
+ }),
+ sourceText: input,
+ project: {
+ folder: undefined,
+ config,
+ },
+ });
+}
+
+test('empty file', async t => {
+ t.snapshot(await testLint('', LINT_ENABLED_FORMAT_DISABLED_CONFIG));
+});
+
+test('undeclared variable', async t => {
+ const res = await testLint('foobar;', LINT_ENABLED_FORMAT_DISABLED_CONFIG);
+ t.snapshot(res);
+
+ // Redundant because of the snapshot above, but this is what we actually care about
+ t.looksLike(res.diagnostics, [
+ {
+ category: 'lint/undeclaredVariables',
+ filename: 'unknown',
+ language: 'js',
+ message: 'Undeclared variable <emphasis>foobar</emphasis>',
+ mtime: undefined,
+ sourceType: 'module',
+ origins: [{category: 'lint'}],
+ end: {
+ column: 6,
+ index: 6,
+ line: 1,
+ },
+ start: {
+ column: 0,
+ index: 0,
+ line: 1,
+ },
+ },
+ ]);
+});
+
+test('format disabled in project config should not regenerate the file', async t => {
+ // Intentionally weird formatting
+ const sourceText = 'foobar ( "yes" );';
+ const res = await testLint(sourceText, LINT_ENABLED_FORMAT_DISABLED_CONFIG);
+ t.is(res.src, sourceText);
+});
+
+test('format enabled in project config should result in regenerated file', async t => {
+ const res = await testLint(
+ 'foobar ( "yes" );',
+ LINT_AND_FORMAT_ENABLED_CONFIG,
+ );
+ t.is(res.src, "foobar('yes');\n");
+});

MODIFY
packages/@romejs/js-compiler/api/lint.ts
packages/@romejs/js-compiler/api/lint.ts
@@ -49,14 +49,14 @@ export default async function lint(req: TransformRequest): Promise<LintResult> {
let formattedCode = src;
if (project.config.format.enabled) {
- const generated = generate(
+ const generator = generate(
newAst,
{
typeAnnotations: true,
},
src,
);
- formattedCode = generated.getCode() + '\n';
+ formattedCode = generator.getCode() + '\n';
}
const result: LintResult = {

MODIFY
packages/@romejs/js-compiler/transforms/lint/undeclaredVariables.ts
packages/@romejs/js-compiler/transforms/lint/undeclaredVariables.ts
@@ -52,8 +52,6 @@ export default {
const isDefined =
binding !== undefined ||
scope.getRootScope().isGlobal(name) ||
- // FIXME: This is such a bad way to determine global variables, we should store an array of variables
- name in global ||
BROWSER_VARIABLES.includes(name) ||
NODE_VARIABLES.includes(name);

js-compiler: Add basic lint tests