Skip to content

Commit

Permalink
Merge pull request #1571 from DanielXMoore/typecheck-rewrite-imports
Browse files Browse the repository at this point in the history
Fix unplugin `typecheck`: source mapping and `rewriteCivetImports` combination
  • Loading branch information
edemaine authored Nov 4, 2024
2 parents 18b94bd + 43ff2ef commit cca62c7
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 336 deletions.
2 changes: 1 addition & 1 deletion integration/unplugin-examples/esbuild/src/main.civet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{a} from "./module"
{a} from "./module.civet"

console.log a

Expand Down
16 changes: 11 additions & 5 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -5699,8 +5699,8 @@ ModuleExportName

# https://262.ecma-international.org/#prod-ModuleSpecifier
ModuleSpecifier
UnprocessedModuleSpecifier ImportAssertion?:a ->
let { token } = $1
UnprocessedModuleSpecifier:module ImportAssertion?:assertion ->
let { token } = module
if (config.rewriteTsImports) {
// Workaround to fix:
// https://github.com/microsoft/TypeScript/issues/42151
Expand All @@ -5714,10 +5714,16 @@ ModuleSpecifier
`${config.rewriteCivetImports.replace(/\$/g, '$$')}$1`)
}

if (a)
return [{ ...$1, token }, a]
if (token !== module.token) {
module = { ...module, token, input: module.token }
}

return { ...$1, token }
return {
type: "ModuleSpecifier",
module,
assertion,
children: [ module, assertion ],
}

UnprocessedModuleSpecifier
StringLiteral
Expand Down
31 changes: 15 additions & 16 deletions source/parser/declaration.civet
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {

import {
convertOptionalType
insertTrimmingSpace
isExit
makeLeftHandSideExpression
makeNode
Expand All @@ -61,7 +62,6 @@ import {

import {
convertNamedImportsToObject
insertTrimmingSpace
processCallMemberExpression
} from ./lib.civet

Expand Down Expand Up @@ -223,7 +223,7 @@ function processDeclarationCondition(condition, rootCondition, parent: ASTNodePa
simple := ref is expression
let children
if simple
ref = insertTrimmingSpace ref, ""
ref = trimFirstSpace ref
children = [ref]
else
children = [ref, initializer]
Expand Down Expand Up @@ -406,9 +406,9 @@ function processDeclarationConditionStatement(s: IfStatement | IterationStatemen
// Convert FromClause into arguments for dynamic import
function dynamizeFromClause(from)
from = from[1..] // remove 'from'
from = insertTrimmingSpace from, ""
if from.-1?.type is "ImportAssertion"
assert := from.pop()
from = trimFirstSpace from
if assert := from.-1?.assertion
from.-1.children |>= .filter (is not assert)
from.push ", {", assert.keyword, ":", assert.object, "}"
["(", ...from, ")"]

Expand Down Expand Up @@ -490,22 +490,21 @@ function dynamizeImportDeclarationExpression($0)
[imp, ws1, named, ws2, from] := $0
object := convertNamedImportsToObject(named)
dot := "."
processCallMemberExpression {
processCallMemberExpression
type: "CallExpression",
children: [
{ type: "Await", children: "await" }, " ",
imp,
insertTrimmingSpace(ws2, ""),
dynamizeFromClause(from),
{ type: "Await", children: "await" }, " "
imp
trimFirstSpace(ws2)
dynamizeFromClause(from)
{
type: "PropertyGlob",
dot,
object,
children: [ws1, dot, object],
reversed: true,
type: "PropertyGlob"
dot
object
children: [ws1, dot, object]
reversed: true
}
]
}

function convertWithClause(withClause: WithClause, extendsClause?: [ASTLeaf, WSNode, ExpressionNode])
let extendsToken, extendsTarget, ws: WSNode
Expand Down
19 changes: 19 additions & 0 deletions source/parser/types.civet
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ export type OtherNode =
| FieldDefinition
| FinallyClause
| ForDeclaration
| ImportAssertion
| Index
| Initializer
| Label
| ModuleSpecifier
| NonNullAssertion
| NormalCatchParameter
| ObjectBindingPattern
Expand Down Expand Up @@ -567,6 +569,20 @@ export type ExportDeclaration
ts?: boolean
declaration?: ASTNode

export type ModuleSpecifier
type: "ModuleSpecifier"
children: Children
parent?: Parent
module: ASTLeaf | StringLiteral
assertion?: ImportAssertion

export type ImportAssertion
type: "ImportAssertion"
children: Children
parent?: Parent
keyword: "with" | "assert"
object: ASTNode

export type DeclarationStatement =
type: "Declaration"
children: Children
Expand Down Expand Up @@ -1008,6 +1024,9 @@ export type LiteralContentNode =
| ASTLeaf
| ASTLeafWithType "NumericLiteral" | "StringLiteral"

export type NumericLiteral = ASTLeafWithType "NumericLiteral"
export type StringLiteral = ASTLeafWithType "StringLiteral"

export type RangeExpression
type: "RangeExpression"
children: Children
Expand Down
1 change: 1 addition & 0 deletions source/unplugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ interface PluginOptions {
- Specifying `true` aborts the build (with an error code) on TypeScript errors.
- Alternatively, you can specify a string with any combination of `error`, `warning`, `suggestion`, or `message` to specify which diagnostics abort the build. For example, `"none"` ignores all diagnostics, `"error+warning"` aborts on errors and warnings, and `"all"` aborts on all diagnostics.
- `implicitExtension`: Whether to allow importing `filename.civet` via `import "filename"`. Default: `true`.
- *Note*: Incompatible with `typecheck: true` (TypeScript needs an explicit `.civet` extension)
- `outputExtension`: JavaScript or TypeScript extension to append to `.civet` for internal purposes. Default: `".jsx"`, or `".tsx"` if `ts` is `"preserve"`.
- `ts`: Mode for transpiling TypeScript features into JavaScript. Default: `"civet"`. Options:
- `"civet"`: Use Civet's JS mode. (Not all TS features supported.)
Expand Down
Loading

0 comments on commit cca62c7

Please sign in to comment.